LIFO栈 ADT接口 链表实现

LIFO 链栈结构

1 typedef int ElemType;
2 struct node{
3     ElemType data;
4     struct node* next;
5 };
6 typedef struct node* Stack;

LIFO 链栈基本操作

 1 //LIFO 链栈初始化
 2 void InitStack(Stack top){
 3      top = NULL;
 4 }
 5 
 6 //LIFO 链栈判断栈空
 7 boolean StackKEmpty(Stack top){
 8      if(top == NULL) return true;
 9      else return false;
10 11 
12 //LIFO 链栈进栈
13 void Push(Stack top, ElemType x){
14      LinkedStack p;
15      p = malloc(sizeof *p);
16      p -> data =x;
17      p -> next = top;
18      top = p;
19 }
20 
21 //LIFO 链栈出栈
22 ElemType Pop(Stack top){
23      LinkedStack p;
24      ElemType x;
25      if(top == NULL){
26         printf("栈下溢错误!\n");
27         exit(1);
28      }
29      p = top;
30      x = p -> data;
31      top = top -> next;
32      free(p);
33      return x;
34 }
35 
36 //LIFO 链栈读取栈顶
37 ElemType GetTop(Stack top){
38      if(top == NULL){
39         printf("栈下溢错误! \n");
40         exit(1);
41      }
42      return top -> data;
43 } 

 

posted on 2018-09-30 11:15  MACHINE_001  阅读(133)  评论(0编辑  收藏  举报

导航