1.类实现栈
class Stack{
private:
int top{-1};
int *eles{0};
int stack_size{0};
public:
Stack(int _size){
this->stack_size = _size;
eles = new int[_size]{0};
}
bool Empty(){return this->top==-1;}
bool Full(){return top==stack_size-1;}
void Push(int v){
if (Full()) {
perror("stack is full!\n");
return;
}
eles[++top]=v;
}
int Pop(){
if (Empty()) {
perror("stack is empty!\n");
return -1;
}
int v = eles[top];
eles[top]=0;
--top;
return v;
}
int Peek(){
if (Empty()) {
perror("stack is empty!\n");
return -1;
}
return eles[top]
本文通过类的方式实现C++中的栈功能,包括栈的基本操作,并提供了详细的测试过程,帮助理解栈的操作和C++面向对象编程的应用。
订阅专栏 解锁全文
9963

被折叠的 条评论
为什么被折叠?



