标题:[求助]: there is something wrong with my Stack....what should I do ?
只看楼主
毛毛小虫
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-11-1
 问题点数:0 回复次数:0 
[求助]: there is something wrong with my Stack....what should I do ?


#include <stdio.h>
#include <malloc.h>
#define null 0

typedef struct stacknode
{
int data;
struct stacknode *next;
}stacklink;
typedef struct
{
int stacksize;
stacklink *top;
}linkstack;


void initlink(linkstack *s)
{
s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}

void pushlink(linkstack *s,int x)
{
stacklink *p;
p=(stacklink *)malloc(sizeof(stacklink));
p->data=x;
p->next=s->top->next;
s->top->next=p;

}

int poplink(linkstack *s)
{
int x;
stacklink *p;
if(s->top->next==null)
printf("the stack is empty\n");
else
{
x=s->top->next->data;
p=s->top->next;
s->top=s->top->next;
free(p);

}
return x;
}


void display(linkstack *s)
{
stacklink *p;
p=s->top->next;
printf("打印出该栈为:\n");
if (s->top=null) printf("the stacklink is empty!\n");
else {while(p)
{printf("-----%d",p->data);
p=p->next;}
}
}


void main()
{
int n,k,i,select,h,x1;
linkstack *p = (linkstack *)malloc(sizeof(linkstack));
initlink(p);

printf("请输入你所要创建的站长度:\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("请输入值:\n");
scanf("%d",&k);
pushlink(p,k);
}
printf("select 1:打印出该栈为\n");
printf("select 2:进栈操作\n");
printf("select 3:出栈操作\n");

printf("请选择:\n");
scanf("%d",&select);

switch(select)
{
case 1:{display(p);break;}
case 2:{printf("请输入值 :\n");
scanf("%d",&h);
pushlink(p,h);
display(p);
break;}
case 3:{x1=poplink(p);printf(" %d\n",x1);
display(p);
break;}

}
}

/*Thank you for your help!!*/

搜索更多相关主题的帖子: Stack wrong something 
2007-11-01 19:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-182642-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.238991 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved