栈是一种可以实现“先进后出”的存储结构,其次栈是一种线性结构。
栈的分类: 栈的算法:
静态栈 出栈
动态栈 压栈
以下,我们实现一个动态栈,动态栈实质上就是一个操作受限的链表,完整代码如下所示:
1 /* 2 filename: stack.c 3 */ 4 #include <stdio.h> 5 #include <malloc.h> 6 7 typedef enum{false, true} bool; 8 9 struct Node 10 { 11 int data; //数据域 12 struct Node * pNext; //指针域 13 }; 14 15 struct Stack 16 { 17 struct Node * pTop; //栈顶指针 18 struct Node * pBottom; //栈底指针 19 }; 20 21 void init_stack(struct Stack * pS); //初始化,造一个空栈 22 void push(struct Stack * pS, int val); //入栈 23 bool empty_stack(struct Stack * pS); //判断栈是否为空 24 void traverse_stack(struct Stack * pS); //遍历输出 25 bool pop(struct Stack * pS, int *pVal); //出栈 26 void clear_stack(struct Stack * pS); //清空栈 27 28 int main(void) 29 { 30 struct Stack pS; 31 int val; 32 33 init_stack(&pS); //造一空栈 34 push(&pS, 1); //入栈 35 push(&pS, 2); //... 36 push(&pS, 3); //... 37 push(&pS, 4); //... 38 push(&pS, 5); //入栈 39 printf("the stack:"); 40 traverse_stack(&pS); 41 42 if (pop(&pS, &val)) //出栈 43 printf("pop successful. val = %d\n", val); 44 else 45 printf("pop failed.\n"); 46 printf("the stack:"); 47 traverse_stack(&pS); 48 49 clear_stack(&pS); //清空栈 50 printf("the stack:"); 51 traverse_stack(&pS); 52 53 return 0; 54 } 55 56 /* 初始化,造一个空栈 */ 57 void init_stack(struct Stack * pS) 58 { 59 pS->pTop = (struct Node *)malloc(sizeof(struct Node)); 60 if (NULL == pS->pTop) 61 { 62 printf("failed to allocate memory.\n"); 63 exit(-1); 64 } 65 else 66 { 67 pS->pBottom = pS->pTop; 68 pS->pBottom->pNext = NULL; 69 } 70 return; 71 } 72 73 /* 入栈 */ 74 void push(struct Stack * pS, int val) 75 { 76 struct Node * pNew = (struct Node *)malloc(sizeof(struct Node)); 77 if (NULL == pNew) 78 { 79 printf("failed to allocate memory.\n"); 80 exit(-1); 81 } 82 else 83 { 84 pNew->data = val; 85 pNew->pNext = pS->pTop; 86 pS->pTop = pNew; 87 } 88 return; 89 } 90 91 /* 判断栈是否为空 */ 92 bool empty_stack(struct Stack * pS) 93 { 94 if (pS->pTop == pS->pBottom) 95 return true; 96 else 97 return false; 98 } 99 100 /* 遍历输出 */ 101 void traverse_stack(struct Stack * pS) 102 { 103 struct Node * p = pS->pTop; 104 105 while (p != pS->pBottom) 106 { 107 printf("%d ", p->data); 108 p = p->pNext; 109 } 110 printf("\n"); 111 return; 112 } 113 114 /* 出栈 */ 115 bool pop(struct Stack * pS, int *pVal) 116 { 117 if (empty_stack(pS)) //若栈为空 118 return false; 119 else 120 { 121 struct Node * q = pS->pTop; 122 123 *pVal = q->data; 124 pS->pTop = q->pNext; 125 free(q); 126 q = NULL; 127 128 return true; 129 } 130 } 131 132 /* 清空栈 */ 133 void clear_stack(struct Stack * pS) 134 { 135 if (empty_stack(pS)) 136 return; 137 else 138 { 139 struct Node * p = pS->pTop; //指向栈顶元素 140 struct Node * q = NULL; //指向栈顶元素下一个元素 141 142 while (p != pS->pBottom) 143 { 144 q = p->pNext; 145 free(p); 146 p = q; 147 } 148 pS->pTop = pS->pBottom; 149 return; 150 } 151 } 152 /* 153 the output results in Code::Blocks 10.05 154 ---------------------------------------------------- 155 the stack:5 4 3 2 1 156 pop successful. val = 5 157 the stack:4 3 2 1 158 the stack: 159 160 Process returned 0 (0x0) execution time : 0.009 s 161 ---------------------------------------------------- 162 */