mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 15:56:29 +08:00
85 lines
1.6 KiB
C++
85 lines
1.6 KiB
C++
/**
|
|
* File: array_stack.cpp
|
|
* Created Time: 2022-11-28
|
|
* Author: qualifier1024 (2539244001@qq.com)
|
|
*/
|
|
|
|
#include "../include/include.hpp"
|
|
|
|
/* 基于数组实现的栈 */
|
|
class ArrayStack {
|
|
private:
|
|
vector<int> stack;
|
|
|
|
public:
|
|
/* 获取栈的长度 */
|
|
int size() {
|
|
return stack.size();
|
|
}
|
|
|
|
/* 判断栈是否为空 */
|
|
bool empty() {
|
|
return stack.empty();
|
|
}
|
|
|
|
/* 入栈 */
|
|
void push(int num) {
|
|
stack.push_back(num);
|
|
}
|
|
|
|
/* 出栈 */
|
|
void pop() {
|
|
int oldTop = top();
|
|
stack.pop_back();
|
|
}
|
|
|
|
/* 访问栈顶元素 */
|
|
int top() {
|
|
if(empty())
|
|
throw out_of_range("栈为空");
|
|
return stack.back();
|
|
}
|
|
|
|
/* 返回 Vector */
|
|
vector<int> toVector() {
|
|
return stack;
|
|
}
|
|
};
|
|
|
|
|
|
/* 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 = ";
|
|
PrintUtil::printVector(stack->toVector());
|
|
|
|
/* 访问栈顶元素 */
|
|
int top = stack->top();
|
|
cout << "栈顶元素 top = " << top << endl;
|
|
|
|
/* 元素出栈 */
|
|
stack->pop();
|
|
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
|
PrintUtil::printVector(stack->toVector());
|
|
|
|
/* 获取栈的长度 */
|
|
int size = stack->size();
|
|
cout << "栈的长度 size = " << size << endl;
|
|
|
|
/* 判断是否为空 */
|
|
bool empty = stack->empty();
|
|
cout << "栈是否为空 = " << empty << endl;
|
|
|
|
// 释放内存
|
|
delete stack;
|
|
|
|
return 0;
|
|
}
|