4

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 tmp pointer point to what head is 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?

4 Answers 4

15

This is how your list was (suppose)

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

Head is pointing to location A.

You make a new node

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

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

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

And then you makehead to point to B instead of A.

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

head & tmp point to B now.

Sign up to request clarification or add additional context in comments.

1 Comment

Here is a complete implementation stackoverflow.com/questions/19148576/…
1

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,

Correct.

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

No. To point to head, it would need to contain the address of head. But this line makes it contain the value of head. So no.

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

Yes.

8 Comments

@so then since the value of a pointer is the address of the thing it points to, for tmp to get the value of head would mean that it is getting the address of what head points to..
That's right. tmp -> next = head; gives tmp -> next and head the same value. Thus tmp -> next now points to whatever head points to.
but then wouldn't head = tmp mean that head now points to whatever tmp is pointing to? And isn't tmp now pointing to what was formerly the first node on the linked list?
@FrostyStraw Yes. That's exactly what you want to put a new node at the head of a singly-linked list. That node points to the former head. That node is the new head.
Ohh, I see. Because tmp is pointing at the new node that was created, so head now has the value of what tmp is pointing to, and is hence pointing to the new node. I thought it was saying that head was getting the value of what the new node was pointing at, and thus was pointing at the second node. I think I get it now though, thank you!
|
0
#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();
}

1 Comment

While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.