2022-11-24 18:04:38 +00:00
|
|
|
/*
|
|
|
|
* File: array_stack.cpp
|
2022-11-28 14:23:27 +00:00
|
|
|
* Created Time: 2022-11-28
|
|
|
|
* Author: qualifier1024 (2539244001@qq.com)
|
2022-11-24 18:04:38 +00:00
|
|
|
*/
|
|
|
|
|
2022-11-26 20:19:16 +00:00
|
|
|
#include "../include/include.hpp"
|
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
class ArrayStack {
|
2022-11-28 16:26:18 +00:00
|
|
|
private:
|
2022-11-29 18:27:26 +00:00
|
|
|
vector<int> stack;
|
|
|
|
|
2022-11-28 16:26:18 +00:00
|
|
|
public:
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 获取栈的长度 */
|
|
|
|
int size() {
|
2022-11-29 18:27:26 +00:00
|
|
|
return stack.size();
|
2022-11-28 14:23:27 +00:00
|
|
|
}
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 判断栈是否为空 */
|
|
|
|
bool empty() {
|
2022-11-29 18:27:26 +00:00
|
|
|
return stack.empty();
|
2022-11-28 14:23:27 +00:00
|
|
|
}
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 入栈 */
|
|
|
|
void push(int num) {
|
2022-11-29 18:27:26 +00:00
|
|
|
stack.push_back(num);
|
2022-11-28 14:23:27 +00:00
|
|
|
}
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 出栈 */
|
|
|
|
int pop() {
|
2022-11-29 18:27:26 +00:00
|
|
|
int oldTop = stack.back();
|
|
|
|
stack.pop_back();
|
2022-11-28 14:23:27 +00:00
|
|
|
return oldTop;
|
|
|
|
}
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 访问栈顶元素 */
|
|
|
|
int top() {
|
2022-11-29 18:27:26 +00:00
|
|
|
return stack.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 访问索引 index 处元素 */
|
|
|
|
int get(int index) {
|
|
|
|
return stack[index];
|
2022-11-28 14:23:27 +00:00
|
|
|
}
|
2022-11-28 16:26:18 +00:00
|
|
|
|
|
|
|
/* 返回 Vector */
|
|
|
|
vector<int> toVector() {
|
2022-11-29 18:27:26 +00:00
|
|
|
return stack;
|
2022-11-28 16:26:18 +00:00
|
|
|
}
|
2022-11-28 14:23:27 +00:00
|
|
|
};
|
|
|
|
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化栈 */
|
|
|
|
ArrayStack* stack = new ArrayStack();
|
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
stack->push(1);
|
|
|
|
stack->push(3);
|
|
|
|
stack->push(2);
|
|
|
|
stack->push(5);
|
|
|
|
stack->push(4);
|
|
|
|
cout << "栈 stack = ";
|
2022-11-29 18:27:26 +00:00
|
|
|
PrintUtil::printVector(stack->toVector());
|
2022-11-28 14:23:27 +00:00
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
int top = stack->top();
|
|
|
|
cout << "栈顶元素 top = " << top << endl;
|
|
|
|
|
2022-11-29 19:46:53 +00:00
|
|
|
/* 访问索引 index 处元素 */
|
|
|
|
int num = stack->get(3);
|
|
|
|
cout << "栈索引 3 处的元素为 num = " << num << endl;
|
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
/* 元素出栈 */
|
|
|
|
int pop = stack->pop();
|
|
|
|
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
2022-11-29 18:27:26 +00:00
|
|
|
PrintUtil::printVector(stack->toVector());
|
2022-11-28 14:23:27 +00:00
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
int size = stack->size();
|
|
|
|
cout << "栈的长度 size = " << size << endl;
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
|
|
|
bool empty = stack->empty();
|
|
|
|
cout << "栈是否为空 = " << empty << endl;
|
2022-11-28 16:26:18 +00:00
|
|
|
|
2022-11-28 14:23:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|