【问题标题】:Program to create a linked list not working创建链接列表的程序不起作用
【发布时间】:2021-10-11 01:12:47
【问题描述】:

只有头部元素被打印,没有元素被进一步打印。

我的猜测是createLinkedList(int n) 函数部分内的 else{.....} 内的代码似乎没有发挥作用。

代码如下:

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

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

void createLinkedList(int n)
{
    int i;
    struct Node *temp, *p;
    
    for(i=1; i<=n; i++)
    {
        // First thing we do is create an ISOLATED NODE WHICH IS NOT LINKED TO ANYTHING 
        
        temp = (struct Node *)malloc(sizeof(struct Node));
        printf("Now enter your element %d of linked list: ",i);
        scanf("%d",&(temp->data));
        temp->next = NULL;
        
        // ISOLATED NODE CREATED. SINCE THIS IS UNLINKED, WE MUST STORE IT'S POINTER VARIABLE TO NULL
        
        
        if(head == NULL) // Meaning our Linked List is empty
            head = temp; 
        
        else // Meaning our Linked List is not empty. Has at least one element.
        {
            p = head;
            while(p != NULL)
                p = p->next;  // Accessing Last pointer of Linked List
            
            p = temp; // THAT IOSOLATED NODE Is Hereby Connected to our final Linked LIST Element
        }
        printf("\n");
    }
}

int main() 
{
    head = NULL;
    printf("Enter the length of your linked list: ");
    int n;
    scanf("%d", &n);
    printf("\n");
    createLinkedList(n);
    return 0;
}

【问题讨论】:

    标签: c linked-list


    【解决方案1】:

    更改while(p != NULL) p = p-&gt;next;

    while(p->next != NULL)
                p = p->next; 
    

    这将为您提供可以插入节点的最后一个指针位置。现在,p 将在迭代结束时为空。

    之后您需要执行p-&gt;next = temp 以便将新创建的节点添加到链表中。

    【讨论】:

    • 但代码`p->next`显然也指向空指针。我的代码只是进行了额外的迭代并达到了相同的结果。
    • 您的代码将一直运行到 p 为空,然后当您执行 p = temp 时,p 将存储 temp 的地址,仅此而已。在插入时需要有最后一个节点的地址,并且最后一个节点的 next 不会指向任何其他节点。因此,您需要遍历列表,直到找到下一个为空的节点。这将为您提供最后一个节点的地址,之后您所要做的就是使最后一个节点的 next 指针指向新添加的节点。
    • 你的结果是pNULL。在分配p = temp 的下一行会发生什么?这相当于NULL = temp,这不是你想要的。
    【解决方案2】:

    我们可以跟踪链表中最后一个节点的尾指针,而不是在 else 块中循环。下面是修改后的代码:

    void createLinkedList(int n)
    {
        int i;
        struct Node *temp, *tail;
        
        for(i=1; i<=n; i++)
        {
            // First thing we do is create an ISOLATED NODE WHICH IS NOT LINKED TO ANYTHING 
            
            temp = (struct Node *)malloc(sizeof(struct Node));
            printf("Now enter your element %d of linked list: ",i);
            scanf("%d",&(temp->data));
            temp->next = NULL;
            
            // ISOLATED NODE CREATED. SINCE THIS IS UNLINKED, WE MUST STORE IT'S POINTER VARIABLE TO NULL
            
            
            if(head == NULL) // Meaning our Linked List is empty
            {
                head = temp;
                tail = temp;
            }
            
            else // Meaning our Linked List is not empty. Has at least one element.
            {
                tail->next = temp; // THAT IOSOLATED NODE Is Hereby Connected to our final Linked LIST Element
                tail = temp;
            }
            printf("\n");
        }
    }
    
    int main() 
    {
        head = NULL;
        printf("Enter the length of your linked list: ");
        int n;
        scanf("%d", &n);
        printf("\n");
        createLinkedList(n);
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      您的代码可以运行,但在使用 while(p != NULL) 时会丢失下一个节点的地址,因此您应该将其更改为 while(p-&gt;next != NULL){p=p-&gt;next;}{p-&gt;next=temp;}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多