本程序主要体现了线性表的链式存储结构,主要实现了以下几个功能:
//压栈
//出栈
//取栈顶
//判断栈是否为空
下面是代码,如有不足的地方还请各位大佬多多指正:
//数据结构学习笔记(C++):栈的顺序存储结构 #include#define MAXSIZE 10 using namespace std; class seqStack{ public: seqStack();//构造函数 ~seqStack();//析构函数 public: void Push(int x);//压栈 int Pop();//弹栈 int getTop();//取栈顶元素 bool isEmpty();//判断栈是否为空 private: int data[MAXSIZE];//数据存放在数组中 int top;//栈顶指针(栈帧) }; //1 seqStack::seqStack()//构造函数 { top = -1; } //2 seqStack::~seqStack()//析构函数 { //顺序栈是静态存储分配,在顺序栈变量退出作用域时自动释放顺序栈所占的存储单元,因此,顺序栈无需销毁,析构函数为空 } //3 void seqStack::Push(int x)//定义压栈函数 { if(top == MAXSIZE-1) throw "上溢";//不能在压栈了已经满了 data[++top] = x;//++top:先让栈帧上移,假如原来是 -1,那么现在就是 0,然后以top为下标,在该位置的数组中填入x } //4 int seqStack::Pop()//定义出栈函数 { if(top == -1) throw "下溢"; int x = data[top];//暂存弹出值 top--;//栈帧下移 return x; } //5 int seqStack::getTop()//取栈顶函数 { return data[top]; } //6 bool seqStack::isEmpty()//判断函数是否为空 { if(top == -1) return true; else return false; } int main() { seqStack stack; char command; int x; try{ while(cin>>command) { if(command == 'Q') return 0; switch(command) { case 'P'://压栈 cout<<"输入要压入的元素"; cin>>x; stack.Push(x); cout<<"压栈完成。"<



