#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;
}
c++ 栈的顺序表示
最新推荐文章于 2021-09-22 09:21:59 发布