顺序栈的基本操作

来源:互联网 发布:mac系统重装教程 编辑:程序博客网 时间:2024/04/29 05:20

数据结构的基础结构:顺序栈。

以下为顺序栈的基本操作,我尽量将可能涉及到的操作记录下来方便大家的学习:

#include<iostream>#include<cstdio>#include<algorithm>#include<cstdlib>using namespace std;#define MAXSIZE 64typedef int datatype;typedef struct{datatype data[MAXSIZE];int top;//栈顶指针 }sqstack,*sqslink;/*置栈空*/void ClearStack(sqslink s){s->top = -1;}/*判断栈空*/int EmptyStack(sqslink s){if(s->top<0) return 1;//栈空返回1 else return 0;} /*x进栈*/int Push(sqslink s,datatype x){if(s->top>=MAXSIZE) return -1;//栈满溢出 输入-1终止进栈 s->top++;s->data[s->top] = x;return 0;} /*出栈*/int Pop(sqslink s,datatype *px){if(EmptyStack(s)) return -1;*px = s->data[s->top];s->top--;return 0;}//为简单起见,出栈操作常写作x=Pop(s) /**/int GetTop(sqslink s,datatype *px){if(EmptyStack(s)) return -1;*px = s->data[s->top];return 0;}int main(){sqslink S;S = (sqslink)malloc(sizeof(sqstack));ClearStack(S);datatype a;while(cin>>a && a!=-1){Push(S,a);}while(!EmptyStack(S)){datatype x;Pop(S,&x);printf("%d<",x);}return 0;}


0 0
原创粉丝点击