【问题标题】:Unable to print linked list无法打印链表
【发布时间】:2014-05-13 17:20:25
【问题描述】:

我无法打印整个链表.. 我的错误是什么?我是链表的新手..

这是基础结构

struct data {
    int id;
    int age;
    struct data *next;
};

主要功能

int main() {
    int mode;
    struct data *initial;
    struct data *insert;
    struct data *temp;

    insert = initial;
    printf("1. Insert\n5. Print List\n");

    while (1) {
        printf("Mode: ");
        scanf("%d", &mode);
        if (mode == 1) {
            insert = malloc(sizeof(struct data));

            printf("Id: ");
            scanf("%d", &(insert->id));

            printf("Age: ");
            scanf("%d", &(insert->age));

            insert=insert->next;
        } else if (mode == 5) {
            temp = initial;
            while (temp != insert) {
                printf("Id: %d\tAge: %d\n", temp->id, temp->age);
                temp = temp->next;
            }
        }
    }
}

感谢您的帮助。

【问题讨论】:

    标签: c struct linked-list


    【解决方案1】:

    您永远不会将 initial 初始化为任何内容,甚至不会将 NULL0 初始化,因此您无法明智地从中获取列表。

    您还需要在两个scanf() 调用之后和分配insert = insert->next; 之前设置insert->next = NULL;。您需要以某种方式将insert 挂钩到原始列表中;可能在声明时使用struct data *initial = 0;,在insert = insert->next; 之前使用if (initial == 0) initial = insert;

    你还需要做更多的错误检查:scanf()malloc() 都可能失败,当它们失败时你应该做一些明智的事情。

    【讨论】:

    • 好的;所以你需要做一些调试。您还需要将insert->next 设置为新分配的结构,然后才能丢失插入中的值。坚持下去;你最终会到达那里。
    【解决方案2】:

    除了@JonathanLeffler 提出的观点之外,我在这段代码中看到了问题:

    if(mode==1)
    {
        insert=malloc(sizeof(struct data));
        printf("Id: ");scanf("%d",&(insert->id));
        printf("Age: ");scanf("%d",&(insert->age));
        insert=insert->next;
    }
    

    第一次执行此块时,请执行以下操作:

    1. struct data 创建内存并让insert 指向它。
    2. 填写struct data中的数据。
    3. insert 变成insert->next。但是insert->next 在声明之前没有被初始化为任何东西。在声明的末尾insert 指向一些完全不可预测的东西。
    4. 您已经忘记了分配的内存,因为没有任何指向它。

    第二次执行此块时,请执行以下操作:

    1. struct data 创建内存并让insert 指向它。
    2. 填写struct data中的数据。
    3. insert 变为insert->next。但是insert->next 在声明之前没有被初始化为任何东西。在声明的末尾insert 指向完全不可预测的东西。
    4. 您已经忘记了分配的内存,因为没有任何指向它。

    这里发生了什么?

    1. 您重复了相同的过程。
    2. 您忘记了两次调用 malloc 分配的内存。
    3. insert 仍然指向不可预测的东西。

    每次执行该循环时都会重复此过程。

    这是你可以解决的问题。

    if(mode==1)
    {
        // Allocate memory and let temp point
        // to the allocated memory.
        temp=malloc(sizeof(struct data));
    
        // Fill up the data of the newly allocated memory.
        printf("Id: ");scanf("%d",&(temp->id));
        printf("Age: ");scanf("%d",&(temp->age));
    
        // Make temp clean by making its next point to NULL.
        temp->next = NULL;
    
        // Now figure out where to link this newly allocated
        // memory to the linked list.
    
        // Assuming you initialize insert correctly...
        if ( insert == NULL )
        {
           initial = insert = temp;
        }
        else
        {
           insert->next = temp;
           insert = temp;
        }
    }
    

    希望这是有道理的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多