LIFO 棧結構spa
1 typedef int ElemenType; 2 struct seqStack{ 3 ElemeType data[MaxSize]; 4 int top; 5 }; 6 typedef struct seqStack* Stack;
LIFO 棧基本操做code
1 //LIFO 順序棧 初始化
2 void InitStack(Stack s){ 3 s -> top = -1; 4 } 5
6 //LIFO 順序棧判斷棧空
7 boolean StackEmpty(Stack s){ 8 if(s -> top == -1) return true; 9 else return false; 10 } 11
12 //LIFO 順序棧判斷棧滿
13 boolean StackFull(Stack s){ 14 if(s -> top == MaxSize-1) return true; 15 else return false; 16 } 17
18 //LIFO 順序棧進棧
19 void Push(Stack s, ElemType x){ 20 if(s->top == MaxSize-1){ 21 printf("棧滿溢出錯誤!\n"); 22 exit(1); 23 } 24 s -> top++; 25 s -> data[s>top] = x; 26 } 27
28 //LIFO 順序棧出棧
29 ElemType Pop(Stack s){ 30 if(StackEmpty(s){ 31 printf("棧下溢錯誤!\n"); 32 exit(1); 33 } 34 x = s->data[s->top]; 35 s -> top--; 36 return x; 37 } 38
39 //LIFO 順序棧讀取棧頂元素
40 ElemType GetTop(Stack s){ 41 if(StackEmpty(s){ 42 printf("下溢錯誤!\n"); 43 exit(1); 44 } 45 return s -> data[s -> top]; 46 }