86 lines
1.5 KiB
C++
86 lines
1.5 KiB
C++
|
/**
|
|||
|
* File: array_stack.cpp
|
|||
|
* Created Time: 2022-11-28
|
|||
|
* Author: qualifier1024 (2539244001@qq.com)
|
|||
|
*/
|
|||
|
|
|||
|
#include "../utils/common.hpp"
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>ջ */
|
|||
|
class ArrayStack {
|
|||
|
private:
|
|||
|
vector<int> stack;
|
|||
|
|
|||
|
public:
|
|||
|
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
|
|||
|
int size() {
|
|||
|
return stack.size();
|
|||
|
}
|
|||
|
|
|||
|
/* <20>ж<EFBFBD>ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool isEmpty() {
|
|||
|
return stack.size() == 0;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ջ */
|
|||
|
void push(int num) {
|
|||
|
stack.push_back(num);
|
|||
|
}
|
|||
|
|
|||
|
/* <20><>ջ */
|
|||
|
int pop() {
|
|||
|
int num = top();
|
|||
|
stack.pop_back();
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
|
|||
|
int top() {
|
|||
|
if (isEmpty())
|
|||
|
throw out_of_range("ջΪ<EFBFBD><EFBFBD>");
|
|||
|
return stack.back();
|
|||
|
}
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD> Vector */
|
|||
|
vector<int> toVector() {
|
|||
|
return stack;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/* Driver Code */
|
|||
|
int main() {
|
|||
|
/* <20><>ʼ<EFBFBD><CABC>ջ */
|
|||
|
ArrayStack *stack = new ArrayStack();
|
|||
|
|
|||
|
/* Ԫ<><D4AA><EFBFBD><EFBFBD>ջ */
|
|||
|
stack->push(1);
|
|||
|
stack->push(3);
|
|||
|
stack->push(2);
|
|||
|
stack->push(5);
|
|||
|
stack->push(4);
|
|||
|
cout << "ջ stack = ";
|
|||
|
printVector(stack->toVector());
|
|||
|
|
|||
|
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
|
|||
|
int top = stack->top();
|
|||
|
cout << "ջ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> top = " << top << endl;
|
|||
|
|
|||
|
/* Ԫ<>س<EFBFBD>ջ */
|
|||
|
top = stack->pop();
|
|||
|
cout << "<EFBFBD><EFBFBD>ջԪ<EFBFBD><EFBFBD> pop = " << top << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD> stack = ";
|
|||
|
printVector(stack->toVector());
|
|||
|
|
|||
|
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
|
|||
|
int size = stack->size();
|
|||
|
cout << "ջ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
|
|||
|
|
|||
|
/* <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
|
|||
|
bool empty = stack->isEmpty();
|
|||
|
cout << "ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
|
|||
|
|
|||
|
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
|||
|
delete stack;
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|