用链表模拟栈(100)
来源:互联网 发布:灵格斯翻译家 for mac 编辑:程序博客网 时间:2024/06/12 23:59
源代码:
//目的:利用链表模拟栈 #include <stdio.h>#include <stdlib.h>typedef struct node {int data;struct node *next;} Stacknode;Stacknode *Init (Stacknode *top) {top = NULL;return top;}int not_empty (Stacknode *top) { return top != NULL;}Stacknode *Push (Stacknode *top, int x) {Stacknode *p;p = (Stacknode*)malloc(sizeof(Stacknode));if (p == NULL) return p;else {p->data = x;p->next = top;top = p;return top;}}Stacknode *Pop (Stacknode *top, int *x) {Stacknode *p;if (top == NULL) return top;else { *x = top->data; p = top; top = top->next;free(p);return top; }}int main() {Stacknode p, *top, *l;top = &p;int a,b,i;top = Init (top);while (1) {printf("\n1.Input a stack data\n");printf("\n2.Output a stack data\n");printf("\n3.Exit\n");printf("Please select one->");scanf("%d",&a);if (a == 3) break;switch(a) {case 1: printf("\nPlease input the data->"); scanf("%d",&b); top = Push(top, b); l = top; printf("\nthe elements are:\n"); while(l != NULL) { printf("%d ", l->data); l = l->next;} break;case 2:if (top != NULL) {top = Pop(top, &b);l = top; while(l != NULL) { printf("%d ", l->data); l = l->next; } }else printf("\nStack is empty\n");break;}}return 0;}
运行结果:
0 0
- 用链表模拟栈(100)
- POJ_1363_Rails(栈模拟)
- BNUOJ44586(栈模拟)
- 栈(数组模拟)
- Flipper (栈模拟)
- 用链表集合模拟一个栈
- UVA 101(栈模拟)
- So Easy!!! (栈模拟)
- 模拟栈问题(思路)
- 栈模拟
- 模拟栈
- 模拟栈
- 特长生模拟 BIOtech(模拟)
- 【100题】两个栈模拟一个队列
- JS模拟栈(面向对象)
- uva 127 ``Accordian'' Patience(栈+模拟)
- poj 1028 Web Navigation(栈模拟)
- hdu 3887 树状数组(模拟栈)
- 更新记录
- java运行环境
- rails 项目学习1
- 【LeetCode】413. Arithmetic Slices(Medium)
- CGAffineTransform 放射变换
- 用链表模拟栈(100)
- shell脚本练习
- 204. Count Primes
- ios绘图系列五:绘制渐变CGContextDrawLinearGradient, CGContextDrawRadialGradient, CAGradientLayer
- 职业连连看模型,助你找到理想职业
- Leetcode 293. Flip Game & 294. Flip Game II
- Linux的文件搜索
- ubuntu上初次使用bochs
- 玩转HTML5移动页面_非常实用的css\js\图片优化等工具