采用链式存储实现,进栈、出栈操作
2012-04-19 19:20
267 查看
#include<stdio.h>
#include<stdlib.h>
typedef struct stacklist{
int data;
struct stacklist *next;
}stacklist,*linkstack;
linkstack top,head;
linkstack create()
{
linkstack p;
p=(linkstack)malloc(sizeof(stacklist));
p->next=NULL;
return p;
}
void bulidstack()
{
linkstack p,q;
int data;
p=top;
printf("请输入进栈的元素:");
scanf("%d",&data);
q=create();
q->data=data;
q->next=p->next;
top->next=q;
}
void outstack()//出栈
{
linkstack p;
p=top->next;
if(p!=NULL)
{
printf("%d 出栈\n",p->data);
top->next=p->next;
free(p);
}
else
printf("栈为空\n");
}
void tip()
{
printf("**********\n");
printf("*1 进栈 *\n");
printf("*2 出栈 *\n");
printf("*请选择:*\n");
printf("**********\n");
}
int main()
{
top=create();
int k;
tip();
while(scanf("%d",&k),k)
{
switch(k)
{
case 1:
bulidstack();
printf("操作完成\n");
tip();
break;
case 2:
outstack();
printf("操作完成\n");
tip();
break;
}
}
return 0;
}
#include<stdlib.h>
typedef struct stacklist{
int data;
struct stacklist *next;
}stacklist,*linkstack;
linkstack top,head;
linkstack create()
{
linkstack p;
p=(linkstack)malloc(sizeof(stacklist));
p->next=NULL;
return p;
}
void bulidstack()
{
linkstack p,q;
int data;
p=top;
printf("请输入进栈的元素:");
scanf("%d",&data);
q=create();
q->data=data;
q->next=p->next;
top->next=q;
}
void outstack()//出栈
{
linkstack p;
p=top->next;
if(p!=NULL)
{
printf("%d 出栈\n",p->data);
top->next=p->next;
free(p);
}
else
printf("栈为空\n");
}
void tip()
{
printf("**********\n");
printf("*1 进栈 *\n");
printf("*2 出栈 *\n");
printf("*请选择:*\n");
printf("**********\n");
}
int main()
{
top=create();
int k;
tip();
while(scanf("%d",&k),k)
{
switch(k)
{
case 1:
bulidstack();
printf("操作完成\n");
tip();
break;
case 2:
outstack();
printf("操作完成\n");
tip();
break;
}
}
return 0;
}
相关文章推荐
- c++ 采用链式结构实现关于入栈,出栈,打印栈的操作
- 采用链式存储实现队列的初始化、入队、出队操作。
- 采用顺序存储实现栈的初始化、入栈、出栈操作。
- JAVA实现队列 队列的链式存储结构及操作
- 数据结构-队列的链式存储实现操作
- 线性表链式存储(单循环链表)及其15种操作的实现
- 线性表链式存储(单链表)及其15种操作的实现
- 栈的链式存储的定义、初始化、判断空、入栈、出栈、读取栈顶元素、求栈长度、清空栈操作
- 队列操作的实现-链式存储
- 栈的链式存储的实现与操作
- 重学数据结构003――栈的基本操作及实现(链式存储)
- 线性表链式存储(静态链表)及其12种操作的实现
- 队列的链式存储的实现及操作
- 栈的链式存储 c实现的几种基本操作
- 采用链式结构实现任意多项式的存储,求两个多项式的和
- 线性表链式存储(双向循环链表)及其14种操作的实现
- 【线性表】顺序存储、链式存储的实现及操作
- 集合的链式存储及操作实现
- 二叉树(二叉链式存储)8种基本操作的实现
- 线性表链式存储实现各种操作