用C语言编链栈 但是老是显示为空是怎么回事?
CEL1 2017-10-18 08:22:03 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define FALSE 0
#define TRUE 1
typedef struct node
{
int data,count;
struct node *next;
}Stack;
void Initstack();
void Createstack();
int Destroystack();
int Push();
int Pop();
int IsEmpty();
int IsFull();
int Display();
int Poponly();
int Readfirst();
void Menu();
void Initstack(Stack *top)
{
top->next=NULL;
}
void Createstack(Stack *top,int maxsize)
{
int i;
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
for(i=0;i<maxsize;i++)
{
p->next=top->next;
}
printf("创建栈成功,栈的空间大小是:%d\n",maxsize);
}
int Destroystack(Stack *top)
{
char ch;
if(IsEmpty())
{
printf("栈为空,请先创建一个栈!\n");
return FALSE;
}
printf("你真的要销毁栈吗?请输入(Y/y)确定,其他键为放弃:\n");
ch=getchar();
if(ch==89||ch==121)
{
while(Poponly()!=0);
printf("栈已经销毁!\n");
}
else
printf("已放弃摧毁该栈!\n");
return TRUE;
}
int Push(Stack *top,int data)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
if(IsEmpty())
{
printf("数据%d进栈失败!\n",data);
return FALSE;
}
p->data=data;
p->next=top->next;
top->next=p;
printf("数据%d已成功进栈!\n",p->data);
IsFull();
return TRUE;
}
int Pop(Stack *top,int *x)
{
Stack *p;
if(IsEmpty(top)==0)
{
printf("栈为空!\n");
return FALSE;
}
p=top->next;
top->next=p->next;
*x=p->data;
free(p);
printf("%d已出栈!\n",*x);
return TRUE;
}
int IsEmpty(Stack *top,int len)
{
if(top->next==NULL)
{
printf("栈为空!\n");
return FALSE;
}
return TRUE;
}
int Poponly(Stack *top)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
if(!IsEmpty())
{
p=top->next;
top->next=p->next;
free(p);
}
return TRUE;
}
int IsFull(Stack *top,int count,int len)
{
if(len==count)
printf("此栈已满,请勿再增加数据!\n");
if(len<count)
printf("栈空间大小为:%d\n",count-len);
return TRUE;
}
int Display(Stack *top)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
p=top->next;
if(IsEmpty(top)==0)
{
printf("栈为空!\n");
return FALSE;
}
printf("栈中的内容是:栈顶->");
while(p)
{
printf("%3d",p->data);
p=p->next;
}
printf("栈底\n");
return TRUE;
}
int Readfirst(Stack *top,int *x)
{
Stack *p;
p=(Stack *)malloc(sizeof(Stack));
*x=top->next->data;
printf("栈顶数据是:%d",&x);
return TRUE;
}
void Menu()
{
system("cls");
printf("****************************\n");
printf("** 顺序栈基本功能演示 **\n");
printf("****************************\n");
printf(" 1.创建栈 \n");
printf(" 2.销毁栈 \n");
printf(" 3.数据进栈 (仅限整数)\n");
printf(" 4.数据出栈 \n");
printf(" 5.显示栈中全部数据 \n");
printf(" 6.读取栈顶数据 \n");
printf(" 7.判断是否是空栈 \n");
printf(" 8.判断是否是满栈 \n");
printf(" 9.退出 \n");
return;
}
int main()
{
int x,selection,maxsize,len;
Stack *top ;
top=(Stack *)malloc(sizeof(Stack));
while(1)
{
Menu();
printf("请输入您的选择:\n");
scanf("%d",&selection);
fflush(stdin);
switch(selection)
{
case 1:printf("请输入栈的空间大小:");
scanf("%d",&maxsize);
Initstack(top);
Createstack(top,maxsize);break;
case 2:Destroystack(top);break;
case 3:printf("向栈压入数据:");
scanf("%d",&x);
Push(top,x);break;
case 4:Pop(top,&x);break;
case 5:Display(top);break;
case 6:Readfirst(top,&x);break;
case 7:IsEmpty(top);break;
case 8:IsFull(top,maxsize,&len);break;
case 9:return 0;
default:printf("输入错误!\n");
}
system("pause");
}
return 0;
}