1 / 4
文档名称:

算法与数据结构链栈.doc

格式:doc   大小:34KB   页数:4页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

算法与数据结构链栈.doc

上传人:mh900965 2018/3/21 文件大小:34 KB

下载得到文件列表

算法与数据结构链栈.doc

相关文档

文档介绍

文档介绍:#include<>
#include<>
#include<>
typedef int elemtype;
typedef struct linkstack
{
elemtype dada;
struct linkstack *next;
}linkstack;
linkstack *top;
//初始化
linkstack *init()
{
linkstack *top=(linkstack*)malloc(sizeof(linkstack));
top->next=NULL;
return top;
}
//判栈空
int empty(linkstack *top)
{
if(top->next==NULL)
return 1;
else
return 0;
}
//入栈
int push(linkstack *top,elemtype x)
{
linkstack *node;
node=(linkstack*)malloc(sizeof(linkstack));
if(node==NULL)
{
printf("error!\n");
return 0;
}
else
{
node->dada=x;
node->next=top->next;
top->next=node;
return 1;
}
}
//出栈
int pop(linkstack *top,elemtype *x)
{
linkstack *node;
if(top->next==NULL)
{
printf("error!\n");
return 0;
}
else
{
node=top->next;
*x=node->dada;
top->next=node->next;
free(node);
return 1;
}
}
//取栈顶元素
int gettop(linkstack *top,elemtype *x)
{
if(top->next==NULL)
{
printf("栈空!\n");
return 0;
}
else
{
*x=top->next->dada;
return 1;
}
}
//
//主函数
void main()
{
linkstack *top;
int iget,iflag,in;
elemtype x;
top=init();
for(;;)
{
system("cls");
printf("请选择操作:\n\t\t\t\t\t1------入栈\n\t\t\t\t\t2------判栈空\n\t\t\t\t\t3------出栈\n\t\t\t\t\t4------取栈顶元素\n\t\t\t\t\t5------退出\n");
scanf("%d",&iget);
if(iget==1)
{
printf("请输入入栈数据:\n");
scanf("%d",&x);
iflag=pus