逐个添加结点的链表输出时忽然停止,求大佬指点
自定义了创建函数,add函数和输出函数,创建链表要求结点逐个添加,调用add,用y和n判断是否继续添加,如果录入Y,则开始或继续添加,录入数据之后,回车,n结束录入主函数部分可确保没有问题
选择输出后,程序停止
请求大佬指教
参数:struct line *head是需要操作的链表
typedef struct line {
char data;
struct line *next;
} Node;
struct line *creat(struct line *head) //创建
{
struct line *tail;
while(1)
{
char ch;
printf("添加新的节点?(Y/N)\n");
fflush(stdin);
scanf ("%c",&ch);
if ((ch=='Y')||(ch=='y'))
{
add(head);
}
else if ((ch=='N')||(ch=='n'))
break;
else break;
}
}
struct line *add(struct line *head) //接受单个字符
{
struct line *p = NULL;
struct line *pr = head;
char data;
p = (struct line *) malloc(sizeof(struct line));
if (p == NULL) {
printf("申请内存失败!\n");
exit(0);
}
if (head == NULL) {
head = p;
} else {
while (pr->next != NULL) {
pr = pr->next;
}
pr->next = p;
}
pr = p;
printf("输入数据:");
scanf(" %c", &data);
pr->data = data;
pr->next = NULL;
return head;
}
out(struct line *head) //输出
{
struct line *p = head->next;
int i;
p=head->next;
if(p=NULL)
printf("空链表!\n");
while (p!=NULL) {
printf("%c ", p->data);
p= p->next;
}
printf("\n");
}