数结初学者的链栈,我实在找不出错在哪里,求大神帮忙
我想知道Creat函数里面到底发生什么了,为什么调试的结果这么乱,帮忙看看,谢谢啦#include <stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}Node,*Linkstack;
void Creat_Stack(Linkstack S)
{
int n,i;
Node *p = S;
printf("please input the max n:\n");
scanf("%d\n",&n);
for(i =0;i < n;i++)
{
p->next = (Node*)malloc(sizeof(Node));
scanf("%d ",&p->data);
p = p->next;
}
p = NULL;
p = S->next;
while(p)
{
printf("%3d",p->data);
p = p->next;
}
}
void Push_Stack(Linkstack S,int e)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data=e;
p->next=S->next;
S->next=p;
}
int Pop_stack(Linkstack S,int *e)
{
Node *p;
if(S->next==NULL)
{
puts("It's a empty stack");
return 0;
}
p=S->next;
*e=p->data;
S->next=p->next;
free(p);
return 1;
}
int Pf(Linkstack S)
{
Node *p;
p=S->next;
printf("\n");
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
}
void main()
{
Linkstack N;
int e,a;
N = (Node *)malloc(sizeof(Node));
N->next = NULL;
Creat_Stack(N);
/* printf("please input what you want to push e:\n");
scanf("%d\n",&e);
Push_Stack( N,e);
printf("after pushing,the Linkstack is:\n");
Pf(N);
Pop_stack( N, &a);
printf("after poping,the Linkstack is:\n");
Pf(N); */
getch();
}
push 跟 pop两个函数我调试过,是对的