c++ 栈的顺序表示

来源:互联网 发布:php防止表单模拟提交 编辑:程序博客网 时间:2024/04/29 09:56
#include<iostream>using namespace std;class SqStack{private:   int *elem;   int stacksize;   int top;public:  SqStack(int n);  ~SqStack();  void push(int i);  int  pop();  void StackTrav();  void GetTop();};SqStack::SqStack(int n){elem = new int[n];if(elem == NULL){cout<<"error"<<endl;}stacksize = n;top = -1;}SqStack::~SqStack(){delete[] elem;}void  SqStack::GetTop(){cout<<elem[top]<<endl;}void SqStack::push(int i){   top++;   elem[top] = i;}int SqStack::pop(){int w = elem[top];top -- ;return w;}void SqStack::StackTrav(){int m = top ;while(m >= 0){cout<<elem[m]<<" ";m--;}cout<<endl;}int main(){SqStack stack(10);stack.push(3);stack.StackTrav();stack.pop();stack.push(10);stack.push(14);stack.GetTop();stack.StackTrav();system("pause");return 0;}

0 0