LIFO棧 ADT接口 鏈表實現

LIFO 鏈棧結構node

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

LIFO 鏈棧基本操做spa

 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 } 
相關文章
相關標籤/搜索