|
以下程序编译成功,但运行结果出错。能帮我看看是哪里出错了。好吗
[code:1]
/*栈的链接实现*/
#include <stdio.h>
typedef struct node
{
int data;
struct node *next;
} LStackTp;
int main()
{
LStackTp ls;
int input;
int i = 0;
int outnum;
InitStack(&ls);
printf("请输入一些数字:");
scanf("%d", &input);
while (input != 0)
{
Push(&ls, input);
scanf("%d", &input);
}
GetTop(&ls, &input);
printf("\n当前栈顶的数为:%d\n", input);
printf("请输入要出栈的个数:");
scanf("%d", &outnum);
printf("依此出栈的数为:");
while (i < outnum)
{
Pop(&ls, &input);
printf("%d ", input);
i ++;
}
printf("\n当前栈的排列:");
while (! EmptyStack(&ls))
{
Pop(&ls, &input);
printf("%d ", input);
}
printf("\n\n");
}
int InitStack(LStackTp *ls)
{
ls = NULL;
return (1);
}
int Push(LStackTp *ls, int x)
{
LStackTp *p;
p = (LStackTp *) malloc (sizeof(LStackTp));
p -> data = x;
p -> next = ls;
ls = p;
}
int Pop(LStackTp *ls, int *x)
{
LStackTp *p;
if (ls != NULL)
{
p = ls;
*x = p -> data;
ls = ls -> next;
free(p);
return 1;
}
else
return 0;
}
int EmptyStack(LStackTp *ls)
{
if (ls == NULL)
return (1);
else
return (0);
}
int GetTop(LStackTp *ls , int *x)
{
if (ls != NULL)
{
*x = ls -> data;
return 1;
}
else
return 0;
}
[/code:1] |
|