cpp-algo-cases/chapter_stack_and_queue/array_stack.cpp

94 lines
1.7 KiB
C++
Raw Normal View History

2024-02-21 09:31:34 +08:00
/**
* File: array_stack.cpp
* Created Time: 2022-11-28
* Author: qualifier1024 (2539244001@qq.com)
*/
#include "../utils/common.hpp"
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD>ջ */
class ArrayStack
{
private:
2024-02-21 09:31:34 +08:00
vector<int> stack;
2024-02-23 14:47:33 +08:00
public:
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
int size()
{
2024-02-21 09:31:34 +08:00
return stack.size();
}
2024-02-23 14:47:33 +08:00
/* <20>ж<EFBFBD>ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
bool isEmpty()
{
2024-02-21 09:31:34 +08:00
return stack.size() == 0;
}
2024-02-23 14:47:33 +08:00
/* <20><>ջ */
void push(int num)
{
2024-02-21 09:31:34 +08:00
stack.push_back(num);
}
2024-02-23 14:47:33 +08:00
/* <20><>ջ */
int pop()
{
int num = top(); // <20>Ȼ<EFBFBD>ȡջ<C8A1><D5BB>Ԫ<EFBFBD>ز<EFBFBD><D8B2><EFBFBD><EFBFBD><EFBFBD>
stack.pop_back(); // <20><>ɾ<EFBFBD><C9BE>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA>
return num; // <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> (<28><><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>ջԪ<D5BB><D4AA><EFBFBD><EFBFBD>ʲô)
2024-02-21 09:31:34 +08:00
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
int top()
{
2024-02-21 09:31:34 +08:00
if (isEmpty())
2024-02-23 14:47:33 +08:00
throw out_of_range("ջΪ<EFBFBD><EFBFBD>"); // <20><><EFBFBD><EFBFBD>ջΪ<D5BB>գ<EFBFBD><D5A3>׳<EFBFBD><D7B3>
return stack.back(); // <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA>
2024-02-21 09:31:34 +08:00
}
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD> Vector */
vector<int> toVector()
{
2024-02-21 09:31:34 +08:00
return stack;
}
};
/* Driver Code */
2024-02-23 14:47:33 +08:00
int main()
{
/* <20><>ʼ<EFBFBD><CABC>ջ */
2024-02-21 09:31:34 +08:00
ArrayStack *stack = new ArrayStack();
2024-02-23 14:47:33 +08:00
/* Ԫ<><D4AA><EFBFBD><EFBFBD>ջ */
stack->push(1);
2024-02-21 09:31:34 +08:00
stack->push(3);
stack->push(2);
stack->push(5);
stack->push(4);
2024-02-23 14:47:33 +08:00
cout << "ջ stack = ";
2024-02-21 09:31:34 +08:00
printVector(stack->toVector());
2024-02-23 14:47:33 +08:00
/* <20><><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
int top = stack->top();
2024-02-23 14:47:33 +08:00
cout << "ջ<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD> top = " << top << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* Ԫ<>س<EFBFBD>ջ */
2024-02-21 09:31:34 +08:00
top = stack->pop();
2024-02-23 14:47:33 +08:00
cout << "<EFBFBD><EFBFBD>ջԪ<EFBFBD><EFBFBD> pop = " << top << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD> stack = ";
2024-02-21 09:31:34 +08:00
printVector(stack->toVector());
2024-02-23 14:47:33 +08:00
/* <20><>ȡջ<C8A1>ij<EFBFBD><C4B3><EFBFBD> */
2024-02-21 09:31:34 +08:00
int size = stack->size();
2024-02-23 14:47:33 +08:00
cout << "ջ<EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD> size = " << size << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
/* <20>ж<EFBFBD><D0B6>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA> */
2024-02-21 09:31:34 +08:00
bool empty = stack->isEmpty();
2024-02-23 14:47:33 +08:00
cout << "ջ<EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD> = " << empty << endl;
2024-02-21 09:31:34 +08:00
2024-02-23 14:47:33 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
2024-02-21 09:31:34 +08:00
delete stack;
return 0;
}