您的位置:首页 > 其它

采用链式存储实现,进栈、出栈操作

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: