注册 登录
编程论坛 C语言论坛

逐个添加结点的链表输出时忽然停止,求大佬指点

sherlocked13 发布于 2020-04-05 19:32, 2071 次点击
自定义了创建函数,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");

}
4 回复
#2
forever742020-04-05 20:09
缺个等号
#3
sherlocked132020-04-05 20:14
回复 2楼 forever74
是输出函数里面的那个吗?
    if(head==NULL)
        printf("空链表!\n");
改了程序还是停止了
#4
sherlocked132020-04-05 20:16
   g改成了 if(p==NULL)
        printf("空链表!\n");楼上写错了
#5
fulltimelink2020-04-10 09:27
creat 没有返回, add的返回没有使用

程序代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct line {
    char data;
    struct line *next;
} Node;

void clearStdin() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF) {};
}

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);
    clearStdin();
    pr->data = data;
    pr->next = NULL;
    return head;
}




struct line * creat(struct line *head)          //创建
{
   
    while (1)
    {

        int ch;
        printf("添加新的节点?(Y/N)\n");
        ch = getchar();
        clearStdin();
        
        if ((ch == 'Y') || (ch == 'y'))
        {
            head = add(head);
            continue;
        }
        break;
    }
    return head;
}




void out(struct line *head)          //输出
{
    struct line *p = head;
    if (p == NULL)
        printf("空链表!\n");
    while (p != NULL) {
        printf("%c ", p->data);
        p = p->next;
    }
    printf("\n");

}

void main(void) {
    struct line * head = NULL;
    head = creat(head);
    out(head);
    free(head);
}


[此贴子已经被作者于2020-4-10 09:28编辑过]

1