Changing the head pointer in a linked list

I am having trouble understanding this code. I really need to change the pointer to the head to point to the first element. So why not head the job? Changing the * head value changes when this pointer points, and what should work, right? I read the pass by reference / passed by value, but it's hard for me to understand. Can someone help clarify this? I appreciate your help. Thanks.

In C / C ++, it is easier to make mistakes if the pointer is used incorrectly. Consider this C / C ++ code to insert an element at the top of a list:

bool insertInFront( IntElement *head, int data ){ IntElement *newElem = new IntElement; if( !newElem ) return false; newElem->data = data; head = newElem; // Incorrect! return true; } 

The previous code is incorrect because it only updates the local copy of the pointer to the head. The correct version is followed by a pointer to a head pointer:

 bool insertInFront( IntElement **head, int data ){ IntElement *newElem = new IntElement; if( !newElem ) return false; newElen->data = data; *head = newElem; // Correctly updates head return true; } 
+4
source share
4 answers

Do you need help understanding the difference?

Imagine a calling function in the first case:

 IntElement *head; int data; ... insertInFront (head, data); 

Now in this case, the address pointed to by the head is pushed onto the stack and passed as an argument to insertInFront. When insertInFront does head = newElement; only the argument is changed (on the stack).

In the second case, the caller will be:

 IntElement *head; int data; ... insertInFront (&head, data); 

In this case, the chapter address is pushed onto the stack and passed as an argument to insertInFront. When you do * head = newElement, it is a de-link passed to the address to get the address of the original chapter of the list, and this is a change.

+5
source

Its quite simple when you get your head around the pointer. In the first code, IntElement *head head is a pointer to an existing title in a linked list. In this way, the caller is passed to the address of the list header element. Changing the value of the head in the insert in the front function does not change anything on the caller. The value of this address was passed to your function - not what kept that address back from the caller.

You need to pass your function "head address address" - or IntElement **head . This will allow this function to change the address held by the caller, i.e. Refresh linked list to point to new heading.

+2
source

You do not want to change the values โ€‹โ€‹of the head points, you want to change the pointer that is stored in the head itself, so do not use * head, use the pointer for the chapter itself. The head is of type IntElement * , so the parameter must be a pointer to this type: IntElement **

+1
source

If you have a value of T x somewhere, and you want some other function to modify it, you pass a pointer to x :

 T x; // set to some value modify_me(&x); // will change x /* ... */ void modify_me(T * x) { *x = new_value; } 

Now apply this mechanic to T = IntElement* . The values โ€‹โ€‹you want to change are pointers themselves!

(Perhaps using typedef will make things less confusing: typedef IntElement * NodePtr; )

Also note that your linked list is corrupted because you never set the โ€œnextโ€ pointer of a new item to a pointer to the old heading and similarly for the โ€œpreviousโ€ pointer if the list is double linked.

0
source

All Articles