【问题标题】:Adding a Node in a Singly linked lists (supports adding in front, end and middle)在单链表中添加节点(支持前、尾、中添加)
【发布时间】:2020-02-26 07:13:24
【问题描述】:

我正在尝试编写一个“add”函数,该函数接受节点中保存的值(表示为“n”),以及节点要添加到链表中的位置(表示为“pos”)。

我看到有 3 个单独的添加函数的代码 - addAtBeginning、addAtMiddle、addAtEnd,但我想要一个添加函数来完成所有这些。

我已经为 add(int n, int pos) 编写了代码,但它没有给我预期的输出。

例如,在我的主目录中,我有:

LinkedList num;
num.add(5,1);


num.add(6,2);

num.add(7,2);
num.printList();

我期待输出:5 7 6,(因为 7 将移动到第二个位置,然后 6 将移动到第三个位置),除了我得到输出:5 6 7,其中 7被添加到第三个位置。

另外,我已经编写了 Node 类和 LinkedList 类。我一直在以较小的增量在这些类中测试我的函数,除了我的 add 函数之外,其他一切似乎都很好。

Node * newNode = new Node();

newNode->setValue(n); //Node gets value n

int i=1; //ie. first position is 1, not 0

Node * current = head;

if (current == nullptr) //ie. then new node IS the head
{
    head = newNode;
    head->next=nullptr;
    return;
}

if (current->next==nullptr) //adding to end of list
{

    current->next = newNode;
    newNode->next = nullptr;

}
else //if inserting Node at middle, (neither at beginning or end)
{


    while ( i<pos && current->next != nullptr) //traverse thru list
    {
        current=current->next;
        i = i+1;
    }

    Node * oldNext = current->next; 

    current -> next = newNode;

    newNode -> next = oldNext;



}

编译时没有错误信息。

【问题讨论】:

  • 如果用户要求在位置 6 插入一个节点,但节点数少于 5 个,应该在哪里插入节点?你应该插入节点吗?
  • if (current-&gt;next==nullptr) //adding to end of list 是如何工作的? current-&gt;next 只检查节点 2。如果列表的末尾是节点 3 怎么办?
  • 建议:画图。从一个空列表开始。绘制一个未连接的节点。绘制您需要建立的所有连接以将此节点添加到列表中。然后在将节点添加到末尾时逐步重绘列表,然后在开头插入节点时再次重新绘制。然后再次在列表中间插入一个节点。这些图纸应该描述所有的情况。然后查看您必须执行的步骤中是否有任何重复,您可以利用这些重复来减少您必须涵盖的不同案例的数量。

标签: c++ linked-list nodes singly-linked-list


【解决方案1】:

您的 while 条件存在一个小问题,如下所述:

i=1;
while ( i<pos && current->next != nullptr) //traverse thru list
{
    current=current->next;
    i = i+1;
}

现在想想这种情况,我想在 6 的位置插入节点,也就是 pos 2。

根据您的代码,我的 pos 将是 2 对吗? 根据您的代码,这是发生问题的地方:

而(我

会发生什么

  • 当 i =1 时,i
  • 现在 i 变成了 2,因为 i=i+1;
  • 现在 i 不小于 pos,所以它跳出循环,记住你的当前节点指向头部的下一个节点。

节点 * oldNext = 当前->下一个;

current -> next = newNode;

newNode -> next = oldNext;

这里发生的事情是您的旧 next 指向第三个节点,因为当前节点是第二个节点。

您需要通过更改循环条件来修复它,以便循环在 i 需要更改的上一个节点处停止。

【讨论】:

  • 或者干脆使用一个pointer-to-pointer将节点存储在列表中的当前地址,并将下一个指针设置为当前的Linus on understanding pointers
  • 是的,即使可以,但我试图指出他可能出错的地方并让他明白
  • 不,你有一个很好的答案,我只是传递其他信息。 (以及艺术作品的杀手锏——一张 8.5x11 的纸和一支铅笔是学习列表的最佳方式)这是一个简短的例子 Singly Linked List (node only, no wrapper)
【解决方案2】:

您不需要为末尾的插入编写单独的条件。你需要做的就是

  1. 首先,检查它是否是头节点(通过检查位置是否==1)。
  2. 否则,只需转到比您要插入的位置少一位的位置,例如插入在第 5 位,然后转到第 4 位,然后执行以下操作。 让我们假设我们正在通过 temp2 遍历列表,并且要插入的节点是节点 temp1。现在 temp2 位于第 4 位。所以在 temp1->next 中放入 temp2->next。此时两者都指向所需的第 5 位。现在只需通过 temp2->next 中的强 temp1 断开 temp2 和 position 之间的链接。下面是实现。
NODE* insert_n(NODE* head,int data,int position)
{
    int i;
    NODE* temp1 = (NODE*)malloc(sizeof(NODE));
    temp1->data=data;
    temp1->next = NULL;
    if(position==1)
    {
        temp1->next = head;

        head = temp1;
        return head;
    }
    NODE* temp2 = head;
    for(i=0;i<position-2;i++)
    {
        temp2= temp2->next;
    }
    temp1->next =temp2->next;
    temp2->next = temp1;
    return (head);
}


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多