1

I kept confusing by the bug of my code. I created a linked list and use the push() to add elements and printList() to output the elements, the code below works correctly.

#include <stdio.h>
#include <stdlib.h>
struct linkedList {
    int         _Value;
    struct linkedList   * _Next;
};
typedef  struct linkedList linkedList_t;

/* Function to push a node */
void push( linkedList_t** listHead, int new_data )
{
    /* allocate node */
    linkedList_t* new_node =
        (linkedList_t *) malloc( sizeof(linkedList_t) );

    /* put in the data  */
    new_node->_Value = new_data;

    /* link the old list off the new node */
    new_node->_Next = *listHead;

    /* move the head to point to the new node */
    *listHead = new_node;
}


/* Function to print linked list */
void printList( linkedList_t *head )
{
    linkedList_t *tmp = head;
    while ( tmp != NULL )
    {
        printf( "%d  ", tmp->_Value );
        tmp = tmp->_Next;
    }
}
int main( int argc, char* argv[] )
{  
    linkedList_t *head = NULL;
    push( &head, 20 );
    push( &head, 4 );
    push( &head, 15 );
    push( &head, 85 );
    printList( head );
    return 0;
    }

The Problem is that when i change the arguments as single pointer like:

 void push( linkedList_t* listHead, int new_data )
{
    /* allocate node */
    linkedList_t* new_node =
        (linkedList_t *) malloc( sizeof(linkedList_t) );

    /* put in the data  */
    new_node->_Value = new_data;

    /* link the old list off the new node */
    new_node->_Next = listHead;

    /* move the head to point to the new node */
    listHead = new_node;
}

when I called the printList() function ,nothing happened , I think it was because head kept equaling to NULL but i could not find out what is wrong with my code,assuming head will be changed when i call push() in the main function and my main function is the following:

int main( int argc, char* argv[])
{  
    linkedList_t *head = NULL;
    push( head, 20 );
    push( head, 4 );
    push( head, 15 );
    push( head, 85 );
    printList( head );
    return 0;
    }

I needed some suggestions .Anybody help? thanks!

timlentse
  • 195
  • 2
  • 11
  • `listHead = new_node;` has absolute no effect on the caller-provided pointer passed in to this function; thus why you second snippet doesn't work. Somehow, some way, you need to reflect back to the caller any potential change in the list head pointer *value*. This is typically done either by passing a pointer-to-pointer or by returning the new list-head (the latter I somewhat despise, as it puts the onus on the caller to remember to save the return result). You *may* find [**this**](http://stackoverflow.com/a/15244661/1322972) an interesting read, in particular the second paragraph. – WhozCraig Nov 12 '14 at 06:28

1 Answers1

1

You are actually passing the copy of head pointer when you use single pointer. In case of double pointer you are passing the address of head pointer so that changes to it makes sense.

You can make code working with single pointer version with minute change. In that case you need to return head pointer from your push function.

linkedList_t* push( linkedList_t* listHead, int new_data );

In that case change reflected would be:-

linkedList_t *head = NULL;
head  = push( head, 20 );
head = push( head, 4 );

Hope I am clear enough...

ravi
  • 10,994
  • 1
  • 18
  • 36