1. 定义

  1. 一种可以实现“先进后出” 的存储结构<br /> 栈类似于箱子<br />

2. 分类

2.1 静态栈 (类似于用数组实现)

2.2 动态栈 (类似于用链表实现)

  1. 只能在链表头部插入和删除,不准在链表尾部插入和删除,基本操作入栈和出栈

3. 算法(往里放,从里取)

3.1 出栈

3.2 压栈(参看Java中线程的例子,生产消费的例子)

4. 应用

4.1 函数调用

4.2 中断

4.3 表达式求值(用两个栈,一个存放数字,一个存放符号)

4.4 内存分配

4.5 缓冲处理

4.6 迷宫

5. 栈操作code:

  1. /*
  2. * @Descripttion:
  3. * @version: Beta-v1.0.0
  4. * @Author: jhong.tao
  5. * @Date: 2020-12-02 19:05:11
  6. * @LastEditTime: 2020-12-03 20:36:41
  7. */
  8. # include <stdio.h>
  9. # include <stdlib.h>
  10. # include <stdbool.h>
  11. /**
  12. * @name: struct Node
  13. * @brief: The node of the stack is defined
  14. * @param {none}
  15. */
  16. typedef struct Node{
  17. int data; // The field of data
  18. struct Node * pNext; // The field of the pointer
  19. }Node,*pNode;
  20. /**
  21. * @name: struct Stack
  22. * @brief: The sttuct of stack is defined
  23. * @return {none}
  24. */
  25. typedef struct Stack{
  26. pNode pTop; // The pointer of top
  27. pNode pBottom; // The pointer of bottom
  28. int cnt; // The count of this stack's nodes.
  29. }Stack,*pStack;
  30. // The list of function that has been declared.
  31. bool initStack(pStack ps);
  32. pNode createNode(int val);
  33. bool pushStack(pStack ps, int val);
  34. void traverseStack(pStack ps);
  35. bool popStack(pStack ps, int* pVal );
  36. bool freeStack(pStack ps);
  37. int main(){
  38. Stack s; // The Stack is equivalent to the struct Stack
  39. pStack ps = &s;
  40. int val;
  41. initStack(ps);
  42. pushStack(ps,1);
  43. pushStack(ps,2);
  44. pushStack(ps,3);
  45. printf("The length of this stack is equal to %d\n",s.cnt);
  46. traverseStack(ps);
  47. popStack(ps,&val);
  48. printf("The value of the val is equal to %d\n",val);
  49. traverseStack(ps);
  50. freeStack(ps);
  51. return 0;
  52. }
  53. /**
  54. * @name: bool freeStack(pStack ps)
  55. * @brief: The task of the function is freeing a stack.
  56. * @param {pStack ps:The pointer of the current stack}
  57. * @return {true of false}
  58. */
  59. bool freeStack(pStack ps){
  60. if(ps == NULL)
  61. return false;
  62. pNode pTemp = NULL;
  63. while(ps->pBottom != ps->pTop){
  64. pTemp = ps->pTop;
  65. ps->pTop = pTemp->pNext;
  66. free(pTemp); // The pointer of everyone node is released.
  67. pTemp = NULL;
  68. }
  69. free(ps->pTop); // The pointer of this stack is released.
  70. ps->pTop = NULL; // The pointer of this stack's head node is assigned null.
  71. ps->pBottom = NULL; // Every pinter only can be released once.
  72. free(ps);
  73. ps = NULL;
  74. return true;
  75. }
  76. /**
  77. * @name: pNode popStack(pStack ps)
  78. * @brief: The task of this function is poping a node from the current stack.
  79. * @param {pStack ps:The pionter of the current stack.}
  80. * @return {*}
  81. */
  82. bool popStack(pStack ps, int* pVal ){
  83. if(ps->pTop == ps->pBottom)
  84. return false;
  85. pNode pTemp = ps->pTop;
  86. ps->pTop = pTemp->pNext;
  87. *pVal = pTemp->data;
  88. ps->cnt--;
  89. free(pTemp);
  90. pTemp = NULL;
  91. return true;
  92. }
  93. /**
  94. * @name: void traverseStack(pStack ps)
  95. * @brief: The task of this function is to traverse printing the current stack.
  96. * @param {pStack ps:The pointer of the current stack.}
  97. * @return {none}
  98. */
  99. void traverseStack(pStack ps){
  100. pNode p = ps->pTop;
  101. while(p != ps->pBottom){
  102. printf("%d ",p->data);
  103. p = p->pNext;
  104. }
  105. printf("\nThe traverse is finished\n");
  106. return;
  107. }
  108. /**
  109. * @name: bool pushStack(pStack ps, int val)
  110. * @brief: The task of this function is pushing a node to the current stack.
  111. * @param {pStack ps:The pointer of the current stak.}
  112. * @param {int val:The val is the value of pushing to the current stack.}
  113. * @return {true of false}
  114. */
  115. bool pushStack(pStack ps, int val){
  116. pNode node = createNode(val);
  117. if(node == NULL)
  118. return false;
  119. node->pNext = ps->pTop;
  120. ps->pTop = node;
  121. ps->cnt++;
  122. return true;
  123. }
  124. /**
  125. * @name: bool initStack(pStack ps)
  126. * @brief: The task of this function is to initialize a Stack.
  127. * @param {pStack ps:The ps is the pointer of the current Stack.}
  128. * @return {true or false}
  129. */
  130. bool initStack(pStack ps){
  131. pNode pHeat = createNode(0);
  132. if(pHeat==NULL)
  133. return false;
  134. ps->pBottom = pHeat;
  135. ps->pTop = pHeat;
  136. ps->cnt = 0;
  137. return true;
  138. }
  139. /**
  140. * @name: pNode createNode(int val)
  141. * @brief: The task of this function is to create a node of the stack.
  142. * @param {int val:The val is the value of the field of node data.}
  143. * @return {The pointer of the node or NULL}
  144. */
  145. pNode createNode(int val){
  146. pNode node = (pNode)malloc(sizeof(Node));
  147. if(node == NULL)
  148. return NULL;
  149. node->data = val;
  150. node->pNext = NULL;
  151. return node;
  152. }