【问题标题】:I don't know why my linked list pointer isnt moving我不知道为什么我的链表指针不动
【发布时间】:2018-12-03 15:10:55
【问题描述】:

我正在尝试编写一段将元素添加到列表的代码。

typedef struct things {
    int value;
    struct things *next;
} something;


int main()
{
    int input = 0;
    something *head = NULL;
    something *current = NULL;
    current = head; //current points to head address
    while(input != -1)
    {
        scanf("%d", &input);
        while(current != NULL) //loop until current node returns NULL
        {
            current = current->next; //go to next node
        }

        current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
        current->value = input;
        current->next = NULL; //next points to NULL
    }
    current=head; //current points back to head
    while(current != NULL)
    {
        printf("%d -> ", current->value);
        current = current->next;
    }
    puts("NULL");

    return 0;
}

但是,当我尝试通过列表打印时,我没有得到任何输出。所以即使我输入 1 2 3 4..etc 打印功能也不会输出任何东西

while(current != NULL)
{
    printf("%d -> ", current->value);
    current = current->next;
}
puts("NULL");

我期待像 1 -> 2 -> 3 -> ... 9 -> NULL 这样的输出。我刚刚开始学习链表,因此不胜感激。

【问题讨论】:

  • current = malloc(sizeof(something)); 你永远不会将这个新节点连接到列表的末尾,或者即使它是第一个节点,也要设置头指针。
  • 您在 malloc 之前的循环是浪费时间,因为您所做的只是将 current 重新分配给 malloc 的返回。你真正需要的是一个指向指针的指针,然后你返回 'next' 的地址并用 *current 赋值

标签: c printf singly-linked-list


【解决方案1】:

您在任何时候都不会更新head 的值。或者将列表中的最后一个节点指向新创建的节点。

检查是否首先设置了head,如果没有,则填充它。否则,找到列表的最后一个节点并将新节点添加为它的“下一个”节点,如下所示。

if(head == NULL)
{
    head = malloc(sizeof(something));
    head->value = input;
    head->next = NULL; //next points to NULL
}
else
{
    current = head;
    while(current->next != NULL) //loop until current node returns NULL
    {
        current = current->next; //go to next node
    }

    current->next = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
    current->next->value = input;
    current->next->next = NULL; //next points to NULL
}

【讨论】:

  • 由于代码重复添加到列表末尾,因此可以在if (head == NULL)分支中将current设置为head,然后简单地使用它来添加下一个节点无需搜索列表的尾部(在分配的适当位置添加current = current->next;(在检查malloc() 之后?),如有必要,修改其他分配。
【解决方案2】:

您当前的方法不适合单指针。 将内存分配给current 不会将节点插入到列表中。

只需将current 用作指向指针的指针,如下所示,您的方法就可以了。

int input = 0;
something *head = NULL;
something **current = NULL;
current = &head; //current points to head address
while(input != -1)
{
    scanf("%d", &input);
    while(*current != NULL) //loop until current node returns NULL
    {
        current = &(*current)->next; //go to next node
    }

    *current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
    (*current)->value = input;
    (*current)->next = NULL; //next points to NULL
}
current=&head; //current points back to head
while(*current != NULL)
{
    printf("%d -> ", (*current)->value);
    current = &(*current)->next;
}
puts("NULL");

【讨论】:

  • 这是一种强大的技术;我不确定我会如何教授列表处理。
  • @JonathanLeffler 我同意。我只是接受了 OP 方法。我的意思是如何使他的方法奏效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 2013-12-31
  • 1970-01-01
  • 2016-09-11
相关资源
最近更新 更多