0
// Program to insert node at front in linked list. 

//This is a simple program in linked list but I do not understand the difference between // values of &newNode,newNode and newNode->next

void PushAtFrontLinkList(int value) { if(head==NULL) { head=tail; } node* newNode=new node(); newNode->data=value; newNode->next=head; head=newNode; // Trying to differentiate between data contained in newNode and &newNode and newNode->next cout<<"just new node"<<newNode<<endl; // what will be contained in newNode? cout<<"address of node"<<&newNode<<endl; // what will be contained in &newNode? cout<<"new node next"<<newNode->next<<endl; // It will be the address of the next node? } 
5
  • they are pointer to node *, pointer to node and pointer to node respectively. Commented Sep 17, 2014 at 20:55
  • Does that mean newNode should contain the same address as contained in newNode->next? In short will newNode contain some address or it will just contain the structure of node. Commented Sep 17, 2014 at 20:57
  • in expectation they should be different. but anything can happen while playing with pointers or you intend to make it so in the initial state. Commented Sep 17, 2014 at 21:02
  • @ HuStmpHrrr also, newNode contains the address of itself? newNode->next contain the address of next node? Commented Sep 17, 2014 at 21:08
  • newNode contains the node you just "newed". next contains the next node if you manipulate the pointer in a right way. Commented Sep 17, 2014 at 23:35

2 Answers 2

1

The newNode will contain the the address for the new node object you just created. As for newNode->next, it contains the address for the next node in the list.

Note that at the end, newNode will be the head of the list, and newNode->next will be pointing to the old head.

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

2 Comments

thanks. But what would &newNode contain? If I want to pass the address of newNode to some other function (pass by reference)then will pass like function(newNode) or function(&newNode)?
You would use function (newNode). That's because newNode is already an address. Note that you declared it as node* newNode. The type node* (with the asterisk at the end) is used to declare a variable that simply contains the address to a variable of type node, i.e. it's just a pointer.
0

&newNode: The address in memory for the node newNode: The node to manipulate newNode->next: The node* pointer that is next.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.