2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2022-11-25 02:04:38 +08:00
|
|
|
* File: array_stack.cpp
|
2022-11-28 22:23:27 +08:00
|
|
|
* Created Time: 2022-11-28
|
|
|
|
* Author: qualifier1024 (2539244001@qq.com)
|
2022-11-25 02:04:38 +08:00
|
|
|
*/
|
|
|
|
|
2023-04-24 04:11:18 +08:00
|
|
|
#include "../utils/common.hpp"
|
2022-11-27 04:19:16 +08:00
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 基于数组实现的栈 */
|
|
|
|
class ArrayStack {
|
2023-04-14 03:44:02 +08:00
|
|
|
private:
|
2022-11-30 02:27:26 +08:00
|
|
|
vector<int> stack;
|
2023-04-14 03:44:02 +08:00
|
|
|
|
|
|
|
public:
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 获取栈的长度 */
|
|
|
|
int size() {
|
2022-11-30 02:27:26 +08:00
|
|
|
return stack.size();
|
2022-11-28 22:23:27 +08:00
|
|
|
}
|
2022-11-29 00:26:18 +08:00
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 判断栈是否为空 */
|
2023-09-14 03:36:31 +08:00
|
|
|
bool isEmpty() {
|
|
|
|
return stack.size() == 0;
|
2022-11-28 22:23:27 +08:00
|
|
|
}
|
2022-11-29 00:26:18 +08:00
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 入栈 */
|
|
|
|
void push(int num) {
|
2022-11-30 02:27:26 +08:00
|
|
|
stack.push_back(num);
|
2022-11-28 22:23:27 +08:00
|
|
|
}
|
2022-11-29 00:26:18 +08:00
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 出栈 */
|
2023-01-05 01:45:08 +08:00
|
|
|
void pop() {
|
2022-12-20 14:13:21 +08:00
|
|
|
int oldTop = top();
|
2022-11-30 02:27:26 +08:00
|
|
|
stack.pop_back();
|
2022-11-28 22:23:27 +08:00
|
|
|
}
|
2022-11-29 00:26:18 +08:00
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
/* 访问栈顶元素 */
|
|
|
|
int top() {
|
2023-09-14 03:36:31 +08:00
|
|
|
if (isEmpty())
|
2022-12-20 14:13:21 +08:00
|
|
|
throw out_of_range("栈为空");
|
2022-11-30 02:27:26 +08:00
|
|
|
return stack.back();
|
|
|
|
}
|
|
|
|
|
2022-11-29 00:26:18 +08:00
|
|
|
/* 返回 Vector */
|
|
|
|
vector<int> toVector() {
|
2022-11-30 02:27:26 +08:00
|
|
|
return stack;
|
2022-11-29 00:26:18 +08:00
|
|
|
}
|
2022-11-28 22:23:27 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
int main() {
|
|
|
|
/* 初始化栈 */
|
2023-04-14 03:44:02 +08:00
|
|
|
ArrayStack *stack = new ArrayStack();
|
2022-11-28 22:23:27 +08:00
|
|
|
|
|
|
|
/* 元素入栈 */
|
|
|
|
stack->push(1);
|
|
|
|
stack->push(3);
|
|
|
|
stack->push(2);
|
|
|
|
stack->push(5);
|
|
|
|
stack->push(4);
|
|
|
|
cout << "栈 stack = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printVector(stack->toVector());
|
2022-11-28 22:23:27 +08:00
|
|
|
|
|
|
|
/* 访问栈顶元素 */
|
|
|
|
int top = stack->top();
|
|
|
|
cout << "栈顶元素 top = " << top << endl;
|
|
|
|
|
|
|
|
/* 元素出栈 */
|
2023-01-05 01:45:08 +08:00
|
|
|
stack->pop();
|
|
|
|
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
2023-04-14 03:44:02 +08:00
|
|
|
printVector(stack->toVector());
|
2022-11-28 22:23:27 +08:00
|
|
|
|
|
|
|
/* 获取栈的长度 */
|
|
|
|
int size = stack->size();
|
|
|
|
cout << "栈的长度 size = " << size << endl;
|
|
|
|
|
|
|
|
/* 判断是否为空 */
|
2023-09-14 03:36:31 +08:00
|
|
|
bool empty = stack->isEmpty();
|
2022-11-28 22:23:27 +08:00
|
|
|
cout << "栈是否为空 = " << empty << endl;
|
2022-11-29 00:26:18 +08:00
|
|
|
|
2023-01-14 19:52:11 +08:00
|
|
|
// 释放内存
|
|
|
|
delete stack;
|
|
|
|
|
2022-11-28 22:23:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|