#include<iostream>
using namespace std;
class Stack{
private:
int* data;
int top;
int base;
int size;
public:
Stack();
Stack(int);
~Stack();
int InitStack();
int getTop(int &);
int isEmpty();
int isFull();
int Push(int);
int Pop(int &);
int Clear();
int Print();
};
Stack::Stack(){
size=100;
InitStack();
}
Stack::Stack(int Size){
this->size=Size;
InitStack();
}
Stack::~Stack(){
delete data;
}
int Stack::InitStack(){
top=base=-1;
data=new int[size];
}
int Stack::getTop(int &e){
e=data[top];
}
int Stack::isEmpty(){
return (top==-1)?0:-1;
}
int Stack::isFull(){
return (top==size)?0:-1;
}
int Stack::Push(int e){
if(top<size) data[++top]=e;
else return -1;
}
int Stack::Pop(int &e){
if(top!=base) e=data[top--];
else return -1;
}
int Stack::Clear(){
top=-1;
return 0;
}
int Stack::Print(){
if(top==base) return -1;
else{
int p=base;
while(p!=top){
cout<<data[++p]<<" ";
}
}
}
int main(){
Stack s;
s.Push(11);
s.Push(22);
s.Print();
}
c++类实现栈
最新推荐文章于 2023-05-26 09:31:26 发布