Thread: Linked list -

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2

    Linked list -


    I just made a c program to insert, delete and print nodes of a linked list. I have made three functions - one for inserting, second for deleting and third for printing.

    But I am having a problem - when I declare the head(the starting node) as global it works fine. But when I declare it inside main and pass it into the functions it doesn't wok. Is it necessary to keep head as global?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > Is it necessary to keep head as global?
    No it isn't.

    You need to post code if you want us to see exactly where you went wrong.

    Note, you need to do one of these things.
    Code:
    node *head = NULL;  // empty list
    
    head = insert( head, value );
    // or
    insert( &head, value );
    Inserting into an empty list modifies the head, so you need some way of updating the variable in main, either by assigning a return result, or by passing in a pointer to the head.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Well I'll post the main function and the insert function as otherwise it will get messy
    Code:
    typedef struct node
    {
    	int data;
    	struct node *next;
    }node;
    
    void insert(node* head, int num)
    {
    	node *ptr1, *ptr2, *ptr3;
    	ptr1 = malloc(sizeof(node));
    	ptr1->data = num;
    	ptr1->next = NULL;
    	if(!head)
    		head = ptr1;
    	else
    	{
    		ptr2 = head;
    		if(ptr2->data >= num)
    		{
    			ptr1->next = ptr2;
    			head = ptr1;
    		}
    		else
    		{
    			while(ptr2 && (ptr2->data)<num)
    			{
    				ptr3 = ptr2;
    				ptr2 = ptr2->next;
    			}
    			ptr3->next = ptr1;
    			ptr1->next = ptr2;
    		}
    	}
    }
    
    int main()
    {
    	node *head;
    	char cmd[2];
    	int num;
    	while(1)
    	{
    		scanf("%s", cmd);
    		if(cmd[0]=='i')
    		{
    			scanf("%d", &num);
    			insert(head, num);
    		}
    		else if(cmd[0]=='d')
    		{
    			scanf("%d", &num);
    			delete(head, num);
    		}
    		else if(cmd[0]=='p')
    			print(head);
    		else if(cmd[0]=='q')
    			break;
    	}
    	return 0;
    }
    I am taking commands(cmd)
    If its 'i' I insert a number, if its 'd' i delete a number, if its 'p' I print the list while if its q i break.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Like I said, your insert needs to be able to return a modified head pointer back to main.

    This, you are not doing at the moment.

    See my two example methods for achieving this, and pick one.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    75
    Rep Power
    2
    Well thanks, it works now ... but I don't understand -
    Why didn't it work before?
    When you pass an arrray(pointer to an array), changes made to the content of the array is reflected in the main function too right? Here too I passed a pointer so changes should reflect in the main function
    So why didn't it work in my code?

    P.S. I didn't really expect anyone to see my code and help. Thanks a lot. This site is great for programmers!
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > changes made to the content of the array is reflected in the main function too right?
    True, but you're not changing the content, you're changing the pointer.

    Consider
    Code:
    void foo ( int a ) {
      a = 1;
    }
    int main ( ) {
      int b = 0;
      foo(b);  // do you expect b to change here?
    }
    What about now?
    Code:
    void foo ( int *a ) {
      *a = 1;
    }
    int main ( ) {
      int b = 0;
      foo(&b);  // do you expect b to change here?
    }
    Now replace "int" with "node*" and try your code.

    Remember, you're trying to modify where a pointer points to, not change what it points at (which you're also doing, but that isn't the only thing going on).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo