简体   繁体   English

C ++链表将新节点添加到列表的开头

[英]C++ linked list adding a new node to the beginning of the list

I'm struggling with how to link the new node I am creating to the rest of my linked list. 我正在努力将如何创建的新节点链接到链接列表的其余部分。

template <class T>
void LinkedList<T>::addBeg(T value)
{
      ListNode *nodePtr = head;
      head = nodePtr->next;
      head = nodePtr;
      nodePtr = new ListNode(value);
}

I know what I did wrong here: the new value is not associated the linked list at all. 我知道我在这里做错了什么:新值根本没有与链表关联。

I think I know what I need to do. 我想我知道该怎么做。 I'm pretty sure what I need to do is create the new value, insert into the beginning of the existing list, and then redefine head to be the newly created value. 我很确定我需要做的是创建新值,插入现有列表的开头,然后将head重新定义为新创建的值。

The problem I'm having, is I don't know how to do this. 我遇到的问题是我不知道该怎么做。

So, what I think I need to do (at least logically), is set 因此,我认为我需要做的(至少在逻辑上)已完成

*nodePtr = new Listnode(value);

Then set 然后设置

nodePtr = head; 

then set 然后设置

head = nodePtr-> next; 

and then set the 然后设置

new ListNode(value) = head;

Am I on the right track? 我在正确的轨道上吗? I can't shake the nagging feeling that I'm not correctly linking the new ListNode to the existing list and I can't figure out if I am making the wrong steps or if I'm missing a step. 我无法摆脱没有将新的ListNode正确链接到现有列表的烦恼的感觉,也无法弄清楚是执行错误的步骤还是缺少步骤。

To create a new node to the head of a list, follow these steps 要在列表的开头创建一个新节点,请按照下列步骤操作

  1. Create a temporary node holding your value 创建一个保存您的价值的临时节点
  2. Set the temporary node's next to point to the head 设置临时节点的下一个指向头
  3. Set the head to temporary 将头设为临时

You first have to create a new Node, then link it to the current head. 您首先必须创建一个新的Node,然后将其链接到当前的头部。 Then you switch the reference from your previous head to the newly created node. 然后,将参考从先前的头切换到新创建的节点。

ListNode *newHead = new ListNode;
newHead->next = head;
head = newHead;

If you say nodePtr = head; 如果说nodePtr = head; then you're just overwriting the pointer you just made. 那么您只是在覆盖刚创建的指针。 You want to change nodePtr->next = head; head = nodePtr; 您想更改nodePtr->next = head; head = nodePtr; nodePtr->next = head; head = nodePtr;

Think carefully about what you have and what you need to do: 仔细考虑一下您拥有什么和需要做什么:

  • You have two nodes that you care about: the current head and your new nodePtr . 您有两个要关心的节点:当前head和新的nodePtr
  • You want nodePtr->next to point to your current head, and update head to point to nodePtr so that this is the first node in the list. 您希望nodePtr->next指向您当前的头,并更新头以使其指向nodePtr以便它是列表中的第一个节点。

The order in which you do things is important: if you assign the new value of head before making nodePtr->next point to the previous value, you will lose your whole list! 操作的顺序很重要:如果在使nodePtr->next指向先前的值之前分配head的新值,则会丢失整个列表! Therefore: 因此:

  1. Create your new node nodePtr . 创建新节点nodePtr
  2. Make nodePtr->next point to head. 使nodePtr->next指向头。
  3. Make head point to nodePtr 将头指向nodePtr

You are on the wrong way. 你走错了路。 The solution is create the new node, link the next node to head, and after refer the head to new node: 解决方案是创建新节点,将下一个节点链接到head,然后将head引用到新节点:

template <class T>
void LinkedList<T>::addBeg(T value)
{
    ListNode *nodePtr = new ListNode(value);
    nodePtr->next = head;
    head = nodePtr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM