主题:请各位帮忙看看,这个建链表的程序为何正运行着,会中断跳出?
#include<stdio.h>
#include <stdlib.h>
struct link
{int data;
struct link *next;
};
struct link *Append(struct link *head)
{
struct link *p=NULL;
struct link *pr=head;
int data;
p=(struct link *)malloc(sizeof(struct link));
if(p==NULL)
{printf("NO enough memory to alloc");
exit(0);
}
if(head==NULL)
{head=p;}
else
{
while (pr->next !=NULL)
{pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("input node data:");
scanf("%d",&data);
pr->data=data;
pr->next=NULL;
return head;
}
main()
{int i;
char c;
struct link *head=NULL;
printf("do you want to append a new node?");
scanf("%c",&c);
i=0;
while(c=='Y')
{
head=Append(head);
printf("do you want to append a new node");
scanf("%c",&c);
i++;
}
printf("%d new nodes have been appended!",i);
}
#include <stdlib.h>
struct link
{int data;
struct link *next;
};
struct link *Append(struct link *head)
{
struct link *p=NULL;
struct link *pr=head;
int data;
p=(struct link *)malloc(sizeof(struct link));
if(p==NULL)
{printf("NO enough memory to alloc");
exit(0);
}
if(head==NULL)
{head=p;}
else
{
while (pr->next !=NULL)
{pr=pr->next;
}
pr->next=p;
}
pr=p;
printf("input node data:");
scanf("%d",&data);
pr->data=data;
pr->next=NULL;
return head;
}
main()
{int i;
char c;
struct link *head=NULL;
printf("do you want to append a new node?");
scanf("%c",&c);
i=0;
while(c=='Y')
{
head=Append(head);
printf("do you want to append a new node");
scanf("%c",&c);
i++;
}
printf("%d new nodes have been appended!",i);
}