链栈中的入栈操作有点类似于头插法
2013-08-30 08:53
113 查看
#include<stdio.h> #include<malloc.h> #define LEN sizeof(linkstack) typedef struct node { int data; struct node *next; }linkstack; linkstack *push(linkstack *top,int x); linkstack *pop(linkstack *top,int *datap); void print(linkstack *top); int menu_select(); void main() { linkstack *top=(struct node*)malloc(LEN); top=NULL; int kk,x; int *datap=(int *)malloc(sizeof(int *)); kk=menu_select(); do { switch(kk) { case 1: printf("请输入数据:");scanf("%d",&x); getchar();top=push(top, x);break; case 2: top=pop(top,datap);break; case 3: print(top);break; case 0: return; } kk=menu_select(); }while(kk!=0); } linkstack *push(linkstack *top,int x) //入栈操作 { linkstack *p; p=(struct node *)malloc(LEN); p->data=x; p->next=top; top=p; return(top); } linkstack *pop(linkstack *top,int *datap) { linkstack *p; if(top==NULL) { printf("栈空!"); return(NULL); } else { *datap=top->data; p=top; top=top->next; free(p); return(top); } } int menu_select() { char c; int n; printf("\n\t\t\t\t请选择操作\n"); printf("\t\t\t\t1:进栈运算\n"); printf("\t\t\t\t2:出栈运算\n"); printf("\t\t\t\t3:打印\n"); printf("\t\t\t\t0:退出\n"); do { c=getchar(); n=c-48; }while((n>3)&&(n<0)); getchar(); return(n); } void print(linkstack *top) { linkstack *t; t=top; while(t!=NULL) { printf("%d\t",t->data); t=t->next; } }
相关文章推荐
- 链栈的定义、初始化、出栈、入栈等操作
- 链栈的常用操作(初始化,入栈,出…
- 6-5 链栈的基本操作(10 分)PTA无法评测但是感觉写的符合要求
- 链栈的进栈出栈操作
- 顺序栈、链栈基本操作
- 【数据结构】链栈的基本操作
- JAVA顺序栈操作——初始化、入栈、出栈、判断空栈、判断栈满
- xcode 类似于eclipse和android studio 里面的ctrl+d复制行的操作的设置
- c语言(逆序)建立链栈与栈的基本操作
- 链栈操作实现
- 链栈的相关操作
- 顺序栈和链栈的各种操作
- 采用顺序存储实现栈的初始化、入栈、出栈操作。
- 顺序栈、链栈基本操作
- 数据结构学习(九)——链栈的操作
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 顺序栈、链栈基本操作
- 链栈的基本操作-数据结构
- 数据结构 栈的建立及入栈出栈操作 C语言版
- 再回首,数据结构——链栈上的常见操作