数据结构--顺序栈的基本操作

来源:互联网 发布:淘宝钛锗手链 编辑:程序博客网 时间:2024/05/17 07:19
若存储栈的长度为StackSize,则栈顶位置top必须小于StackSize。当栈存在一个元素时,top等于0,因此通常把空栈的判定条件定为top等于-1。

顺序栈的存储结构为:

typedef struct{  int data[MAXSIZE];  int top;}SqStack;
顺序栈的基本操作有:

Status InitStack(SqStack &S){  S.top=-1;  return OK;}Status ClearStack(SqStack &S){  S.top=-1;  return OK;}Status StackEmpty(SqStack S){  return S.top==-1?true:false;}int StackLength(SqStack S){  return S.top+1;}Status Push(SqStack &S,int e){  if(S.top==MAXSIZE-1) return ERROR;  S.top++;  S.data[S.top]=e;  return OK;}Status Pop(SqStack &S,int &e){  if(StackEmpty(S)) return ERROR;  e=S.data[S.top];  S.top--;  return OK;}Status GetTop(SqStack S,int &e){  if(StackEmpty(S)) return ERROR;  e=S.data[S.top];  return OK;}Status StackTraverse(SqStack S){  int i=0;  while (i<=S.top)  {    printf("%d ",S.data[i]);    i++;  }  printf("\n");  return OK;}


原创粉丝点击