数据结构03:栈

[复制链接]
759|0
 楼主| seifguo 发表于 2016-3-28 23:10 | 显示全部楼层 |阅读模式
  1. /*
  2. * 根据郝斌老师视频整理
  3. * 编译环境:
  4. * guo@linux:~$ gcc --version
  5. * gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4
  6. */
  7. #include <stdio.h>
  8. #include <malloc.h>
  9. #include <stdlib.h>

  10. #define FALSE 0
  11. #define TRUE 1

  12. typedef struct Node
  13. {
  14.         int data;
  15.         struct Node *pNext;
  16. }NODE, *PNODE;

  17. typedef struct Stack
  18. {
  19.         PNODE pTop;
  20.         PNODE pBottom;
  21. }STACK, *PSTACK;

  22. /**********************声明**********************************/
  23. /*初始化栈*/
  24. void InitStack(PSTACK );
  25. /*压栈*/
  26. void PushStack(PSTACK , int );
  27. /*遍历*/
  28. void TraverseStack(PSTACK );
  29. /*判断栈是否为空*/
  30. int  IsEmpty(PSTACK );
  31. /*出栈*/
  32. int  PopStack(PSTACK ,int *);
  33. /*清空栈*/
  34. void ClearStack(PSTACK );


  35. /*
  36. * 函数名称:InitStack。
  37. * 输入参数:pStack栈变量的地址。
  38. * 输出参数:无。
  39. * 函数说明:新建一个栈。栈的特点是"先入后出"。
  40. */
  41. void InitStack(PSTACK pStack)
  42. {
  43.         /*申请一块内存,将内存地址赋值给栈变量所指向的pTop成员*/
  44.         pStack->pTop = (PNODE)malloc(sizeof(NODE));
  45.         if (NULL == pStack->pTop)
  46.         {
  47.                 printf("动态分配内存失败!\n");
  48.                 exit(-1);
  49.         }
  50.         pStack->pBottom = pStack->pTop;
  51.         pStack->pBottom->pNext = NULL;

  52.         return;
  53. }

  54. /*
  55. * 函数名称:PushStack。
  56. * 输入参数:pStack栈变量的地址;val压栈的值。
  57. * 输出参数:无。
  58. * 函数说明:将一个元素压入栈中。
  59. */
  60. void PushStack(PSTACK pStack, int val)
  61. {
  62.         PNODE pNew = (PNODE)malloc(sizeof(NODE));
  63.         if (NULL == pNew)
  64.         {
  65.                 printf("动态分配内存失败!\n");
  66.                 exit(-1);
  67.         }
  68.        
  69.         pNew->data = val;
  70.         pNew->pNext = pStack->pTop;
  71.         pStack->pTop = pNew;

  72.         return;
  73. }

  74. /*
  75. * 函数名称:TraverseStack。
  76. * 输入参数:pStack栈变量的地址。
  77. * 输出参数:无。
  78. * 函数说明:将一个栈遍历输出。
  79. */
  80. void TraverseStack(PSTACK pStack)
  81. {
  82.         PNODE p = pStack->pTop;

  83.         if (pStack->pTop == pStack->pBottom)
  84.         {
  85.                 printf("栈为空!\n");
  86.         }
  87.         else
  88.         {
  89.                 while (p != pStack->pBottom)
  90.                 {
  91.                         printf("%d ",p->data);
  92.                         p = p->pNext;
  93.                 }
  94.                 printf("\n");
  95.         }

  96.         return;
  97. }

  98. /*
  99. * 函数名称:IsEmpty。
  100. * 输入参数:pStack栈变量的地址。
  101. * 输出参数:如果栈为空返回TRUE;否则返回FALSE。
  102. * 函数说明:判断栈是否为空。
  103. */
  104. int IsEmpty(PSTACK pStack)
  105. {
  106.         if (pStack->pTop == pStack->pBottom)
  107.                 return TRUE;
  108.         else
  109.                 return FALSE;
  110. }


  111. /*
  112. * 函数名称:PopStack。
  113. * 输入参数:pStack栈变量的地址;pVal保存出栈的值。
  114. * 输出参数:出栈成功返回TRUE;否则返回FALSE。
  115. * 函数说明:出栈。
  116. */
  117. int PopStack(PSTACK pStack,int *pVal)
  118. {
  119.     if ( IsEmpty(pStack) )
  120.     {
  121.         return FALSE;
  122.     }
  123.     else
  124.     {
  125.         PNODE r = pStack->pTop;
  126.         *pVal = r->data;
  127.         pStack->pTop = r->pNext;
  128.         free(r);
  129.         r = NULL;
  130.         return TRUE;
  131.     }
  132. }

  133. /*
  134. * 函数名称:ClearStack。
  135. * 输入参数:pStack栈变量的地址。
  136. * 输出参数:无。
  137. * 函数说明:清空栈。
  138. */
  139. void ClearStack(PSTACK pStack)
  140. {
  141.     if ( IsEmpty(pStack) )
  142.     {
  143.         return ;   
  144.     }
  145.     else
  146.     {
  147.         PNODE p = pStack->pTop;
  148.         PNODE q = NULL;
  149.         while (p != pStack->pBottom)
  150.         {
  151.             q = p->pNext;
  152.             free(p);
  153.             p = q;
  154.         }
  155.     }
  156.     pStack->pTop = pStack->pBottom;
  157. }

  158. int main(void)
  159. {
  160.         STACK Stack;
  161.     int val;

  162.         InitStack(&Stack);
  163.         PushStack(&Stack, 1);
  164.         PushStack(&Stack, 2);
  165.         PushStack(&Stack, 3);
  166.         PushStack(&Stack, 4);
  167.     PushStack(&Stack, 5);
  168.     PushStack(&Stack, 6);
  169.     PushStack(&Stack, 7);
  170.     PushStack(&Stack, 8);
  171.         TraverseStack(&Stack);

  172.     if ( PopStack(&Stack, &val) )
  173.     {
  174.         printf("出栈的元素是%d\n", val);
  175.     }
  176.     else
  177.     {
  178.         printf("出栈失败!\n");
  179.     }
  180.     printf("出栈后遍历:\n");
  181.     TraverseStack(&Stack);

  182.     if ( PopStack(&Stack, &val) )
  183.     {
  184.         printf("出栈的元素是%d\n", val);
  185.     }
  186.     else
  187.     {
  188.         printf("出栈失败!\n");
  189.     }
  190.     printf("出栈后遍历:\n");
  191.     TraverseStack(&Stack);

  192.     ClearStack(&Stack);
  193.     printf("清空后遍历:\n");
  194.     TraverseStack(&Stack);

  195.         return 0;
  196. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

83

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部