【问题标题】:Linked List - insert more than one node链表 - 插入多个节点
【发布时间】:2015-09-29 13:48:09
【问题描述】:

我是 C++ 新手,很难理解链表中的插入。这是我迄今为止一直在处理的插入,我只是在添加多个节点时遇到了麻烦。

struct node *temp, *x, *y;
temp = create_node(a, b, c);
x = begin;
if (begin == NULL)
{
    begin = temp;
    temp->next = NULL;
}
else
{
    y = temp;
    temp->next = NULL;
}

【问题讨论】:

标签: c++ insert linked-list nodes


【解决方案1】:

代码的最后一部分(当列表不为空时插入)没有正确调整指针。下面的代码解决了这个问题:

struct node *temp;
temp = create_node(a, b, c);
if (begin == NULL)
{
    begin = temp;
    temp->next = NULL;
}
else
{
    temp->next = begin;
    begin = temp;
}

【讨论】:

  • 我知道我做了什么...谢谢
  • 当你在处理这种操作时,试着在一张纸上画出节点和指针并在上面执行操作。通常更容易看到你必须改变哪些指针。
猜你喜欢
  • 1970-01-01
  • 2019-04-26
  • 2013-02-06
  • 1970-01-01
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
相关资源
最近更新 更多