链表无法打印所有元素

Linked list unable to print all elements

我正在尝试打印提示用户输入的链接列表。 下面的代码不打印整个列表,一次只打印最后一个元素。 我似乎没有找到错误。你能看看吗?

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *head;

void Insert(int x) {
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp->data = x;
    temp->next = NULL;
    head = temp;
};

void Print() {
    struct Node *temp = head;
    printf("Linked list is: ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
};

int main() {
    head = NULL;
    int i, x;
    for (i = 1; i <= 10; i++) {
        if (i == 1) {
            printf("Enter 1st number: \n");
        } else if (i == 2) {
            printf("Enter 2nd number: \n");
        } else {
            printf("Enter %dth number: \n", i);
        }
        scanf("%d", &x);
        Insert(x);
        Print();
    }
}

temp->next = NULL; 是罪魁祸首。应该是 temp->next = head;.

另一个(更极端的)问题是您的代码无法检查 mallocscanf 中的错误。


编辑回复评论:

如果你想追加(而不是前置),你需要保留一个尾指针用于前向遍历,然后使用一个虚拟的第一个节点(避免分支)或 special-case 一个插入到一个空列表。

一段代码中的两者示例(通过 exit(1) 进行简单的错误处理):

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

#define DUMMYFIRST 1 //change to 0 to compile the other variant

#if DUMMYFIRST
    struct Node dummyfirst;
    struct Node *head=&dummyfirst;
#else
    struct Node *tail,*head=0;
#endif

void Insert(int x) {
    struct Node *newnode = malloc(sizeof(struct Node));
    //don't cast the result of malloc in C
    //

    if(!newnode) { perror("malloc"); exit(1); }
    newnode->data = x;
    newnode->next = 0;

    #if !DUMMYFIRST
        if(!tail) tail = head = newnode;
        else head->next = newnode;
    #else
        head->next = newnode;
    #endif

    head = newnode;
};

void Print() {
    #if DUMMYFIRST
        struct Node *newnode = dummyfirst.next;
    #else
        struct Node *newnode = tail;
    #endif
    printf("Linked list is: ");
    while (newnode != NULL) {
        printf("%d ", newnode->data);
        newnode = newnode->next;
    }
    printf("\n");
};

int main() {
    int i, x;
    for (i = 1; i <= 10; i++) {
        if (i == 1) {
            printf("Enter 1st number: \n");
        } else if (i == 2) {
            printf("Enter 2nd number: \n");
        } else {
            printf("Enter %dth number: \n", i);
        }
        if(1!=scanf("%d", &x)) exit(1);
        Insert(x);
        Print();
    }
}

一种对库更友好的错误处理方法是将错误传播给调用者,即,不是立即退出并显示错误消息,而是将 return 值从 void 更改为某些内容指示错误,例如以便调用者可以检查并决定要做什么(打印它,以本地化版本打印它,尝试不同的算法...)

例如:

struct Node *Insert(int x) {
    struct Node *newnode = malloc(sizeof(struct Node));
    //don't cast the result of malloc in c
    //

    if(!newnode) return NULL;
    //...
};
//...
//calling code:
    if(!Insert(x)) perror("Insert"),exit(1);

当您插入新节点时,您不会 link 列表的其余部分,您应该写

而不是 temp->next = NULL;
    temp->next = head;

为确保定义的行为,您应该检查内存分配失败和无效输入。

同时删除函数体后的虚拟 ;

这是修改后的版本:

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *head;

int Insert(int x) {
    struct Node *temp = malloc(sizeof(*temp));
    if (temp) {
        temp->data = x;
        temp->next = head;
        head = temp;
        return 1;
    } else {
        return 0;
    }
}

void Print(void) {
    struct Node *temp = head;
    printf("Linked list is: ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main() {
    static char suffix[4][3] = { "th", "st", "nd", "rd" };
    int i, x;
    for (i = 1; i <= 10; i++) {
        int suff = (i >= 1 && i <= 3) ? i : 0;
        printf("Enter %d%s number:\n", i, suffix[suff]);
        if (scanf("%d", &x) != 1) {
            fprintf(stderr, "invalid or missing input\n");
            break;
        }
        if (!Insert(x)) {
            fprintf(stderr, "cannot allocate memory for Node\n");
            return 1;
        }
        Print();
    }
    return 0;
}