1. 定义
一种可以实现“先进后出” 的存储结构<br /> 栈类似于箱子<br />
2. 分类
2.1 静态栈 (类似于用数组实现)
2.2 动态栈 (类似于用链表实现)
只能在链表头部插入和删除,不准在链表尾部插入和删除,基本操作入栈和出栈
3. 算法(往里放,从里取)
3.1 出栈
3.2 压栈(参看Java中线程的例子,生产消费的例子)
4. 应用
4.1 函数调用
4.2 中断
4.3 表达式求值(用两个栈,一个存放数字,一个存放符号)
4.4 内存分配
4.5 缓冲处理
4.6 迷宫
5. 栈操作code:
/* * @Descripttion: * @version: Beta-v1.0.0 * @Author: jhong.tao * @Date: 2020-12-02 19:05:11 * @LastEditTime: 2020-12-03 20:36:41 */# include <stdio.h># include <stdlib.h># include <stdbool.h>/** * @name: struct Node * @brief: The node of the stack is defined * @param {none} */typedef struct Node{ int data; // The field of data struct Node * pNext; // The field of the pointer}Node,*pNode;/** * @name: struct Stack * @brief: The sttuct of stack is defined * @return {none} */typedef struct Stack{ pNode pTop; // The pointer of top pNode pBottom; // The pointer of bottom int cnt; // The count of this stack's nodes. }Stack,*pStack;// The list of function that has been declared.bool initStack(pStack ps);pNode createNode(int val);bool pushStack(pStack ps, int val);void traverseStack(pStack ps);bool popStack(pStack ps, int* pVal );bool freeStack(pStack ps);int main(){ Stack s; // The Stack is equivalent to the struct Stack pStack ps = &s; int val; initStack(ps); pushStack(ps,1); pushStack(ps,2); pushStack(ps,3); printf("The length of this stack is equal to %d\n",s.cnt); traverseStack(ps); popStack(ps,&val); printf("The value of the val is equal to %d\n",val); traverseStack(ps); freeStack(ps); return 0;}/** * @name: bool freeStack(pStack ps) * @brief: The task of the function is freeing a stack. * @param {pStack ps:The pointer of the current stack} * @return {true of false} */bool freeStack(pStack ps){ if(ps == NULL) return false; pNode pTemp = NULL; while(ps->pBottom != ps->pTop){ pTemp = ps->pTop; ps->pTop = pTemp->pNext; free(pTemp); // The pointer of everyone node is released. pTemp = NULL; } free(ps->pTop); // The pointer of this stack is released. ps->pTop = NULL; // The pointer of this stack's head node is assigned null. ps->pBottom = NULL; // Every pinter only can be released once. free(ps); ps = NULL; return true;}/** * @name: pNode popStack(pStack ps) * @brief: The task of this function is poping a node from the current stack. * @param {pStack ps:The pionter of the current stack.} * @return {*} */bool popStack(pStack ps, int* pVal ){ if(ps->pTop == ps->pBottom) return false; pNode pTemp = ps->pTop; ps->pTop = pTemp->pNext; *pVal = pTemp->data; ps->cnt--; free(pTemp); pTemp = NULL; return true;}/** * @name: void traverseStack(pStack ps) * @brief: The task of this function is to traverse printing the current stack. * @param {pStack ps:The pointer of the current stack.} * @return {none} */void traverseStack(pStack ps){ pNode p = ps->pTop; while(p != ps->pBottom){ printf("%d ",p->data); p = p->pNext; } printf("\nThe traverse is finished\n"); return;}/** * @name: bool pushStack(pStack ps, int val) * @brief: The task of this function is pushing a node to the current stack. * @param {pStack ps:The pointer of the current stak.} * @param {int val:The val is the value of pushing to the current stack.} * @return {true of false} */bool pushStack(pStack ps, int val){ pNode node = createNode(val); if(node == NULL) return false; node->pNext = ps->pTop; ps->pTop = node; ps->cnt++; return true;}/** * @name: bool initStack(pStack ps) * @brief: The task of this function is to initialize a Stack. * @param {pStack ps:The ps is the pointer of the current Stack.} * @return {true or false} */bool initStack(pStack ps){ pNode pHeat = createNode(0); if(pHeat==NULL) return false; ps->pBottom = pHeat; ps->pTop = pHeat; ps->cnt = 0; return true;}/** * @name: pNode createNode(int val) * @brief: The task of this function is to create a node of the stack. * @param {int val:The val is the value of the field of node data.} * @return {The pointer of the node or NULL} */pNode createNode(int val){ pNode node = (pNode)malloc(sizeof(Node)); if(node == NULL) return NULL; node->data = val; node->pNext = NULL; return node;}