when passing head of linked list to function.Why do we need to pass it by reference of reference such as in push(node* &head, int key)

Related searches

Printed out the address of head and &head: head:0x603050 &head :0x7fffffffe4b8: what does this signify?

void push(node* &head,int key)// Inserts items at front of link list
{
    node* linkNode=new node(); //declares new node
    linkNode->data=key;
    if(head==NULL)             //if the link list is empty then create a new one.
    {
        linkNode->next=NULL;
        head=linkNode;   //1
    }
    else
    {
        linkNode->next=head;
        head=linkNode;
    }    
}

Main function where all other functions are called from link list is 8,4,2 main function

int main(int argc, char** argv) 
{
    node* head=NULL;         //initializing head to NULL
    push(head,2);           //creating link list
    push(head,4);           //this requires &head
    push(head,8);           //link list is 8,4,2
    selectSort(head);        //this does not require &head
    reverse(head);          //this requires &head
    return 0;
}

Why do we need to pass it by reference of reference such as in push(node* &head, int key)

Otherwise it won't work to set the given linkNode as current head:

    if(head==NULL)             //if the link list is empty then create a new one.
    {
        linkNode->next=NULL;
        head=linkNode; // <- This statement changes the head variable passed from main()
    }

What you have is a reference to a pointer (head) that will be 'returned' from the push() function, and set the head pointer passed from the caller correctly:

node* head=NULL;
push(head,2); // sets head to the node created for key '2'

Don't forget to delete all the node instances you have created with new node();. In a different context as you're showing, this might lead to memory leaks.

why do i need to return the head in a linked list?c++, In your case you are pushing nodes onto the front of the list thus your code is EDIT: The head should be pass by reference. val should be passed by value. If you don't return the new beginning then the calling function will´┐Ż We can make a function polymorphic by passing objects as reference (or pointer) to it. For example, in the following program, print() receives a reference to the base class object. print() calls the base class function show() if base class object is passed, and derived class function show() if derived class object is passed.

That's not a "reference of reference"; it's a reference to a pointer.

It means that, once the pointer head has been set to point to the new element, within the function, this change also affects the pointer you originally passed in to the function.


selectSort(head);        //this does not require &head

It probably should do, actually, if the function performs a sort over all elements of the list.


reverse(head);          //this requires &head

After this call, head now points to the new head of the list. This would not be possible if you'd passed head by value.

An alternative implementation might return the new head pointer, instead of using this "out parameter" convention.

How to write C functions that modify head pointer of a Linked List , Consider simple representation (without any dummy node) of Linked List. Functions that operate on such Linked lists can be divided in two categories: We can always pass head pointer as an argument and traverse/update the For example, if the list is 1->2->3->4, then it should be modified to 2->3->4”. Iterate trough the linked list. In loop, do following. // Before changing next of current, // store next node. next = curr->next // Now change next of current // This is where actual reversing happens curr->next = prev // Move prev and curr one step forward prev = curr curr = next; Below is the implementation of the above approach:

The reason you must pass head to the push function is because your push function is expecting to modify the value of the head pointer. If you don't pass it by reference, any changes to it will only be available within the function call. For example, if it was not passed by reference, and you passed head (initialized to NULL), to the push function, a new item would be created, but your value for head would only be updated inside the function. Once you left the function, it will still be NULL (because you passed the pointer by copy).

Note that this can go away if you create a linked list class instead of treating your nodes as a linked list themselves (that is, encapsulate the nodes under a list interface - which is what the standard library does).

[PDF] Linked List Basics, pointers and memory before you can understand linked lists. A "pointer" stores a reference to another variable 1) Pass The List By Passing The Head Pointer We need Push() to be able to change some of the caller's memory — namely the from NULL to point to the new node, such as the tail variable in the following. Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2 Output: 30 The node at index 2 is 30

Linked List Implementation in C, In previous post, we have introduced linked list data structure and discussed about Each node of a linked list contains a single data element and a pointer to the next node in the list. We can allocate dynamic memory in C using malloc() or calloc() function. malloc() Instead the list is passed in as a "reference" pointer. However, the trick in identifying double pointer. Passing single pointer is as good as pass-by-value, and we will pass the same pointer again and again. We need to pass the address of head pointer for reflecting the changes in parent recursive calls. Thanks to Sharad Chandra for suggesting this approach.

[PDF] Linked List Problems, Such pointers to pointers are often called "reference pointers". Many list functions need to change the caller's head pointer. In C++ this in the C language, pass a pointer to the head pointer. Change the passed in head pointer to be NULL int i;. // Deal with the head node here, and set the tail pointer. Push(&head, 1);. What we've learned from these pictures is that when we find the node we want to remove, we need some kind of reference to the node preceding it OR some way to backtrack to that node. In addition, since we need to fix the pointer to the beginning of the list, we'll need some way to get the new beginning pointer out of the function.

If your linked list is just you literally managing a bunch of nodes (getting your hands dirty and avoiding Object Oriented Design at all costs), then yeah you would pass the head. Make sure you pas the head pointer by reference though, because the sort function may need to change which element is the first element.

Comments
  • > Printed out the address of head and &head: head:0x603050 &head :0x7fffffffe4b8: what does this signify? 0x603050 is a pointer to the first node which is in the heap, 0x7fffffffe4b8 is a pointer to head which is on the stack. In order to change the address in head, you need to change the address stored in 0x7fffffffe4b8.
  • you have to pass by reference if the function has to modify the argument passed. In C, we will have to pass by pointer to pointer. Eg: coliru.stacked-crooked.com/a/3a353d5761903768