链栈的基本操作
2016-04-24 22:07
435 查看
#include <stdio.h> #include <stdlib.h> typedef struct node{ char data; node * next; }LStack; LStack * init(){ LStack * ls; ls = (LStack *)malloc(sizeof(LStack)); ls->next = NULL; return ls; } void push(LStack * ls,char x){ LStack * p; p = (LStack *)malloc(sizeof(LStack)); p->data = x; p->next = ls->next; ls->next = p; } void pop(LStack * ls){ LStack * p; char x; if(ls->next == NULL){ printf("the stack is empty!\n"); }else{ p = ls->next; ls->next = p->next; x = p->data; free(p); } } void print(LStack * ls){ LStack * p; p = ls->next; while(p!=NULL){ printf("%4c",p->data); p = p->next; } printf("\n"); } int main(){ LStack * ls; ls = init(); char x; printf("please input the values of the stack!\n"); scanf("%c",&x); while(x!='\n'){ push(ls,x); scanf("%c",&x); } printf("\n"); print(ls); printf("\n"); printf("after pop (once)\n"); pop(ls); print(ls); printf("after pop (twice)\n"); pop(ls); print(ls); return 0; }
相关文章推荐
- python文件读写
- 20145322何志威 《Java程序设计》第8周学习总结
- netstat命令使用
- Java的构造方法
- 装饰者模式
- web服务器的工作原理及相关协议
- IP地址与网段的划分
- java.util包---LinkedList
- vim下ctags+taglist环境的搭建
- Jquery中filter()与Find()区别
- Hibernate4实战 之 第一部分 Hibernate入门
- JavaWeb学习笔记——jsp内置对象
- 包含min函数的栈
- 实践--ViewPager实现App引导页
- Appium九宫格滑动解锁研究
- HDU 2222 Keywords Search(AC自动机)
- java并发之concurrent包的结构
- static_cast, dynamic_cast, const_cast探讨
- 20145211 《Java程序设计》实验报告三:敏捷开发与XP实践
- html,body设置高度100%和默认的滚动条(转)