C++ 将节点添加到链表的前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19194224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:35:17  来源:igfitidea点击:

Adding node to the front of a linked list

c++

提问by FrostyStraw

I'm really confused as to what exactly is going on here..

我真的很困惑这里到底发生了什么..

I have the function

我有这个功能

void addToFront(int data)  
{
  Node* tmp = new Node();
  tmp -> data = data;
  tmp -> next = head;
  head = tmp;
}

So when we do the line tmp-> next = head, we are making the tmppointer point to what headis pointing to (the current first element of the list)? Because that's what it feels like, but wouldn't that just make it point to head? And then when we do head = tmp, we are making head point to the new node that we created, correct?

因此,当我们执行 line 时tmp-> next = head,我们使tmp指针指向head指向的内容(列表的当前第一个元素)?因为这就是它的感觉,但这不就是让它指向head吗?然后当我们这样做时head = tmp,我们正在指向我们创建的新节点,对吗?

回答by Sadique

This is how your list was (suppose)

这就是你的清单(假设)

 +----+-----+   +-----+-----+   +-----+------+
 |  A |     +-->|     |     +-->|     |      |
 +----+-----+   +-----+-----+   +-----+------+
 /

Headis pointing to location A.

Head指向位置A

You make a new node

你创建一个新节点

 +----+----+
 | B  |    |
 +----+----+
 /

tmpnow points to B. You insert data in tmp->data, and then make tmp -> next = head;. So now we have:

tmp现在指向B. 您在 中插入数据tmp->data,然后在tmp -> next = head;. 所以现在我们有:

 +----+----+     +----+-----+   +-----+-----+   +-----+------+
 | B  |next|---->|  A |     +-->|     |     +-->|     |      |
 +----+----+     +----+-----+   +-----+-----+   +-----+------+
                  /

And then you makeheadto point to Binstead of A.

然后你head指向B而不是A.

 +----+----+     +----+-----+   +-----+-----+   +-----+------+
 | B  |next|---->|  A |     +-->|     |     +-->|     |      |
 +----+----+     +----+-----+   +-----+-----+   +-----+------+
 /  \

head& tmppoint to Bnow.

head&tmp指向B现在。

回答by David Schwartz

So when we do the line tmp-> next =head, we are making the tmp pointer point to what head is pointing to (the current first element of the list)? Because that's what it feels like,

因此,当我们执行 tmp-> next =head 这行时,我们是在使 tmp 指针指向 head 所指向的对象(列表的当前第一个元素)?因为就是那种感觉

Correct.

正确的。

but wouldn't that just make it point to head?

但这不会让它指向头部吗?

No. To point to head, it would need to contain the addressof head. But this line makes it contain the valueof head. So no.

不。要指向头部,它需要包含头部的地址。但是这一行使它包含了head的。所以不行。

And then when we do head = tmp, we are making head point to the new node that we created, correct?

然后当我们执行 head = tmp 时,我们将 head 指向我们创建的新节点,对吗?

Yes.

是的。

回答by muhammad sameer

#include<iostream>
using namespace std;
struct Node
{
    int data;
    Node *link;
};
class LinkList
{
    Node *headptr;
public:
    LinkList()
    {
        headptr=NULL;
    }

    void InsertatFirst(int val)
    {
        Node *newNode;
        newNode = new Node;
        newNode ->link =NULL;
        newNode ->data = val;
        newNode ->link=headptr;
        headptr = newNode;
    }

    void Display()
    {
        Node *disNode;
        disNode = headptr;
        while(disNode !=NULL)
        {
            cout<<"Display Node Value is "<<disNode ->data<<endl<<endl;
            disNode =  disNode->link;
        }
    }

};
void main()
{
    LinkList lobj;
    lobj.InsertatFirst(45);
    lobj.InsertatFirst(2);
    lobj.InsertatFirst(1);
    lobj.InsertatFirst(0);
    lobj.InsertatFirst(-1);
    lobj.Display();
}

回答by Wesley

void addToFront(int data) {
  Node* tmp = new Node(t); //assume your Node constructor can handle this
  if(numElements != 0) {   //should check weather empty or not
    x->next = head;        //link them first
    head = x;             //and now make the head point to the new head
  } else {      //if empty you should also set the tail pointer
    head = x;
    tail = x;
  }
  numElements++;
}

If there is a head, the new node "next" will be point to the current head which is pointing to another node. and then head=x, the pointer called head is now set to point to the new node rather than the previous one

如果有一个头,新节点“下一个”将指向当前指向另一个节点的头。然后 head=x,称为 head 的指针现在被设置为指向新节点而不是前一个节点