【问题标题】:Insertion at end in circular linked list not working in C在循环链表的末尾插入在 C 中不起作用
【发布时间】:2015-08-23 14:36:10
【问题描述】:

请指出代码中的错误。

函数insertatend()第一次插入,但不再插入。

我正在尝试在循环链表的末尾插入一个节点,但是在第一次插入一个元素后,如果我们再次尝试输入数据,它就会卡在 while 循环中。

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

typedef struct node node;
node *head = NULL;

node *insertatend(node *head, int value)
{
    node *temp, *p;
    p = head;
    temp = (node *)malloc(sizeof(node));
    temp->data = value;
    temp->next = head;
    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        while (p->next != head)
            p = p->next;
        p->next = temp;
    }
    return head;
}

void display(node *head)
{
    node *p = head;
    if (head == NULL)
    {
        printf("\nlinked list is empty\n");
        return;
    }
    while (p->next != head)
    {
        printf("%d  ", p->data);
        p = p->next;
    }
    printf("\n");
}

int main()
{
    int ch = 1, value;
    while (ch)
    {
        printf("1.Insert  2.Display");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                printf("enter an element:");
                scanf("%d", &value);
                head = insertatend(head, value);
                break;
            case 2:
                display(head);
                break;
        }
    }
    return 0;
}

【问题讨论】:

  • 循环 链表的“结束”必须是我听过的最模糊的概念之一。我说的含糊而不仅仅是愚蠢,因为你显然知道你的意思——但不管它是什么,这绝对不是它的名字。
  • temp=(node*)malloc(sizeof(node)) ... 你要 free() tmp 吗?你真的需要转换 malloc() 吗?
  • @Michi 肯定不在insert 函数中。
  • @WhozCraig 从我在这里看到的,我们有一个最小的程序。所以我只是问。
  • 如果是这么小的程序,请帮我指出错误

标签: c pointers linked-list insert circular-list


【解决方案1】:

我认为错误在这里:

temp->next=head;
if(head==NULL){
    head=temp;
}

当您输入第一个元素时,head 为空。所以 temp->next 设置为 NULL,head 设置为 temp。 当您输入第二个元素时,它会执行以下操作:

else{
while(p->next!=head)
        p=p->next;
p->next=temp;}

其中 p->next 为空,因此您永远不会遇到 p->next == head 的情况,您将始终处于循环中!

编辑: 所以解决方法是将其更改为:

if(head==NULL){
    head=temp;
}
temp->next=head;

编辑:显示函数中的第二个错误:循环不打印最后一个元素。我刚刚测试了它,它工作正常。

所以完整的代码看起来像:

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

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

typedef struct node node;
node *head = NULL;

node *insertatend(node *head, int value)
{
    node *temp, *p;
    p = head;
    temp = (node *)malloc(sizeof(node));
    temp->data = value;

    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        while (p->next != head)
            p = p->next;
        p->next = temp;
    }
    temp->next = head;
    return head;
}

void display(node *head)
{
    node *p = head;
    if (head == NULL)
    {
        printf("\nlinked list is empty\n");
        return;
    }
    do
    {
        printf("%d  ", p->data);
        p = p->next;
    } while (p != head);
    printf("\n");
}

int main()
{
    int ch = 1, value;
    while (ch)
    {
        printf("1.Insert  2.Display");
        scanf("%d", &ch);
        switch (ch)
        {
            case 1:
                printf("enter an element:");
                scanf("%d", &value);
                head = insertatend(head, value);
                break;
            case 2:
                display(head);
                break;
        }
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    替代版本,使用尾指针而不是头指针,以加快追加速度。

    #include <stdlib.h>
    #include <stdio.h>
    
    struct node {
        struct node *next;
        int data;
    };
    typedef struct node node;
    
    node *insertatend(node *tail, int value)
    {
        node *p;
        p = malloc(sizeof(node));
        p->data = value;
        if(tail == NULL){
            p->next = p;
        } else {
            p->next = tail->next;
            tail->next = p;
        }
        return p;
    }    
    
    void display(node *tail)
    {
        node *p = tail;
        if (p == NULL)
        {
            printf("\nlinked list is empty\n");
            return;
        }
        do{
            p = p->next;
            printf("%d  ", p->data);
        }while(p != tail);
        printf("\n");
    }
    
    int main()
    {
        node *tail = NULL;
        int i;
        for(i = 0; i < 8; i++)
            tail = insertatend(tail, i);
        display(tail);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-16
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多