重学数据结构003——栈的基本操作及实现(链式存储)

来源:互联网 发布:养成类游戏源码 编辑:程序博客网 时间:2024/05/17 23:53

1.栈的概念

    展示只允许在其一端进行插入语删除操作的表。从定义上来说,栈其实也是线性表,因此栈也具备大多数线性表所具备的基本操作。但是,从定义上可知,栈在进行插入、删除操作时,只能在一端进行操作,这一端成为栈顶(top)。

    栈最核心的操作主要是:进栈(Push)、出栈(Pop)、返回栈顶元素(Top)。 此外,栈还判断栈是否为空、创见栈、清空栈等操作。

    既然是线性表,那么栈从实现方式来说主要有两种:顺序存储实现(数组)、链式存储实现(链表)。下面是链式存储实现方式下,站的数据结构定义:

  1. typedef struct Node *PtrToNode; 
  2. typedef PtrToNode Stack; 
  3. typedef struct Node 
  4.     int Element; 
  5.     PtrToNode Next; 
  6. }; 

    站的基本操作如下:

  1. //判断栈是否为空 
  2. int IsEmpty(Stack S); 
  3. //创见栈 
  4. Stack CreateStack(); 
  5. //销毁栈 
  6. void DisposeStack(Stack S); 
  7. //清空栈 
  8. void MakeEmpty(Stack S); 
  9. //进栈 
  10. void Push(int X,Stack S); 
  11. //返回栈顶元素 
  12. int Top(Stack S); 
  13. //出栈 
  14. void Pop(Stack S); 

    下面是一个完整的关于栈的基本操作的例子:

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. typedef struct Node *PtrToNode; 
  5. typedef PtrToNode Stack; 
  6. typedef struct Node 
  7.     int Element; 
  8.     PtrToNode Next; 
  9. }; 
  10.  
  11. int IsEmpty(Stack S); 
  12. Stack CreateStack(); 
  13. void DisposeStack(Stack S); 
  14. void MakeEmpty(Stack S); 
  15. void Push(int X,Stack S); 
  16. int Top(Stack S); 
  17. void Pop(Stack S); 
  18.  
  19. //判断栈是否为空 
  20. int IsEmpty(Stack S) 
  21.     return S->Next == NULL; 
  22. //创建链栈 
  23. Stack CreateStack() 
  24.     Stack S = malloc(sizeof(struct Node)); 
  25.     if(S == NULL) 
  26.     { 
  27.         printf("No enough memory!"); 
  28.         return NULL; 
  29.     } 
  30.     S->Next = NULL; 
  31.     MakeEmpty(S); 
  32.     return S; 
  33.  
  34. void MakeEmpty(Stack S) 
  35.     if(S == NULL) 
  36.     { 
  37.         printf("Use CreateStack First!"); 
  38.     } 
  39.     else 
  40.     { 
  41.         while(!IsEmpty(S)) 
  42.         { 
  43.             Pop(S); 
  44.         } 
  45.     } 
  46.  
  47. void Push(int X,Stack S) 
  48.     PtrToNode Tmp; 
  49.     Tmp = malloc(sizeof(struct Node)); 
  50.     if(Tmp != NULL) 
  51.     { 
  52.         Tmp->Element = X; 
  53.         Tmp->Next = S->Next; 
  54.         S->Next = Tmp; 
  55.     } 
  56.     else 
  57.     { 
  58.         printf("Out of space!"); 
  59.     } 
  60.  
  61. void Pop(Stack S) 
  62.      
  63.     if(IsEmpty(S)) 
  64.     { 
  65.         printf("The Stack is Empty!"); 
  66.     } 
  67.     else 
  68.     { 
  69.         PtrToNode Tmp = S->Next; 
  70.         S->Next = Tmp->Next; 
  71.         free(Tmp); 
  72.     } 
  73.  
  74. int Top(Stack S) 
  75.     if(IsEmpty(S)) 
  76.     { 
  77.         printf("The stack is empty!"); 
  78.         return 0; 
  79.     } 
  80.     else 
  81.     { 
  82.         return S->Next->Element; 
  83.     } 
  84.  
  85. int main(void
  86.     Stack S = CreateStack(); 
  87.     int i; 
  88.     for(i = 0; i < 5; i++) 
  89.     { 
  90.         Push(i,S); 
  91.     } 
  92.  
  93.     while(!IsEmpty(S)) 
  94.     { 
  95.         printf("%d\n",Top(S)); 
  96.         Pop(S); 
  97.     } 
  98.     return 0; 

 

原创粉丝点击