如何将第一个节点移动到链表c ++中列表的末尾

时间:2021-02-20 07:16:20

I want to take the first node of my linked list to the end of it, but so far i couldn't.

我想把我的链表的第一个节点带到它的末尾,但到目前为止我不能。

the idea that i had is to create a new node that contains the first node's info and then delete the first node, and then add the new node to the end of the list.

我的想法是创建一个包含第一个节点信息的新节点,然后删除第一个节点,然后将新节点添加到列表的末尾。

this is my code, but it doesn't work because i can't access the start pointer in my list since it's private:

这是我的代码,但它不起作用,因为我无法访问列表中的启动指针,因为它是私有的:

Newnode-> = list.start->next->info;
list.RemoveFront();     //a function to remove the first node.
list.AddBack(Newnode);  //a function to add the element to the end of the list.

1 个解决方案

#1


Are you serious?

你是认真的吗?

There are two cases:

有两种情况:

  1. Your linked list has a pointer to the last link (Very useful)
  2. 您的链表有一个指向最后一个链接的指针(非常有用)

  3. Or you don't.
  4. 或者你没有。

If you don't have a pointer to the last node, traverse the list and create one.

如果没有指向最后一个节点的指针,请遍历列表并创建一个。

Here are the operations:

以下是操作:

  1. Temp points to first node.
  2. Temp指向第一个节点。

  3. Change Head to point to 2nd Node.
  4. 将头改为指向第二节点。

  5. Set the Temp node to point to nil or null.
  6. 将Temp节点设置为指向nil或null。

  7. Make the last node point to the temp node.
  8. 使最后一个节点指向临时节点。

  9. Make the Tail pointer point to the temp node.
  10. 使Tail指针指向temp节点。

Please draw this out. Linked lists are always easier to understand when you draw them.

请把它画出来。绘制链接列表时总是更容易理解。

#1


Are you serious?

你是认真的吗?

There are two cases:

有两种情况:

  1. Your linked list has a pointer to the last link (Very useful)
  2. 您的链表有一个指向最后一个链接的指针(非常有用)

  3. Or you don't.
  4. 或者你没有。

If you don't have a pointer to the last node, traverse the list and create one.

如果没有指向最后一个节点的指针,请遍历列表并创建一个。

Here are the operations:

以下是操作:

  1. Temp points to first node.
  2. Temp指向第一个节点。

  3. Change Head to point to 2nd Node.
  4. 将头改为指向第二节点。

  5. Set the Temp node to point to nil or null.
  6. 将Temp节点设置为指向nil或null。

  7. Make the last node point to the temp node.
  8. 使最后一个节点指向临时节点。

  9. Make the Tail pointer point to the temp node.
  10. 使Tail指针指向temp节点。

Please draw this out. Linked lists are always easier to understand when you draw them.

请把它画出来。绘制链接列表时总是更容易理解。