#include<iostream> #include<fstream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define MAXSIZE 100 typedef int Status; typedef char SElemType; typedef struct{ SElemType *base;//栈底指针 SElemType *top;// 栈顶指针 int stacksize;// 栈可用的最大容量 }SqStack; //初始化栈 Status InitStack(SqStack &S){ //构造一个空栈 S.base=new SElemType[MAXSIZE];//为顺序栈动态分配一个最大空间为100的数组空间 if(!S.base) exit(OVERFLOW);//存储分配失败 S.top=S.base;//top 初始化为base ,空栈 S.stacksize=MAXSIZE;//stacksize置为栈的最大容量maxSize; return OK; } //顺序栈的入栈 Status Push(SqStack &S,SElemType e){ //插入元素e为新的栈顶元素 if(S.top-S.base==S.stacksize) return ERROR; *(S.top++)=e;//元素e压入栈顶,栈顶指针加1 return OK; } Status Pop(SqStack &S,SElemType &e){ //删除S的栈顶元素,用e返回其值 if(S.base==S.top) return ERROR;//栈空 e=*(--S.top);//栈顶指针减1,将栈顶元素赋给e return OK; } // 顺序栈的取栈顶元素 char GetTop(SqStack S){ if(S.top!=S.base) return *(S.top-1);//返回栈顶元素的值,栈顶指针不变; } int main(){ SqStack s; int choose,flag=0; SElemType j,e,t; cout<<" 1.初始化 "; cout<<" 2.入栈 "; cout<<" 3.读栈顶元素 "<<endl; cout<<" 4.出栈 "<<endl; cout<<" 0.退出 "<<endl; choose=-1; while(choose!=0){ cout<<" 请选择: "<<endl; cin>>choose; switch(choose){ case 1: if(InitStack(s)){ flag=1; cout<<" 成功对栈进行初始化 "; }else cout<<" 初始化栈失败 "; break; case 2:{ fstream file; file.open("SqStack.txt"); if(!file){ cout<<" 错误! 未找到文件! "<<endl; exit(ERROR); } if(flag){ flag=1; cout<<" 进栈元素依次为: "; while(!file.eof()){ file>>j; if(file.fail()) break; else{ Push(s,j); cout<<j<<" "; } } cout<<endl<<endl; }else cout<<" 栈未建立,请重新选择 "; file.close(); } break; case 3: if(flag!=-1&&flag!=0) cout<<" 栈顶元素为: "<<GetTop(s)<<endl<<endl; else cout<<" 栈中无元素,请重新选择 "<<endl; break; case 4: cout<<" 依次弹出的栈顶元素为: "; while(Pop(s,t)){ flag=-1; cout<<t<<" "; } cout <<endl <<endl; break; } } return 0; }
堆栈 实现的基本原理 先进后出