1. 程式人生 > >《棧篇》基本操作

《棧篇》基本操作

順序棧的基本操作

ADT

//PS1
//ADT Stack
//line
#define MaxStackSize 100
typedef int ElemType;

typedef struct
{
 ElemType stack[MaxStackSize];
 int top;
}Stack;

void InitStack(Stack *S)
{
 if((S=(Stack*)malloc(sizeof(Stack)))==NULL)
  exit(OVERFLOW);
 S->top=-1;
}

int StackEmpty(Stack S)
{
 if(S.top==-1)
  return 1;
 else return 0;
}

void StackPush(Stack*S,ElemType elem)
{
 if(S->top==MaxStackSize-1)
 {
  pritnf("%d","Stack is full");
  exit(0);
 }
 else
 S-top++;
 S->top=elem;
}

void StackPop(Stack *S ,ElemType * elem)
{
 if(StackEmpty(*S))
 {
  printf("Stack is empty");
  exit(0);
 }
 else 
 *elem=S->top--;
}

void Gettop(Stack S,ElemType * elem)
{
 if(StackEmpty(*S))
 {
  printf("Stack is empty");
  exit(0);
 }
 else
 *elem=S.top;
}