LIFO栈 ADT接口 数组实现

LIFO 栈结构

1 typedef  int ElemenType;
2 struct seqStack{
3        ElemeType data[MaxSize];
4        int top;
5        };
6 typedef struct seqStack* Stack;

LIFO 栈基本操作

 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 }

 

posted on 2018-09-30 10:51  MACHINE_001  阅读(344)  评论(0编辑  收藏  举报

导航