Thread: Problem in link list

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Problem in link list

    Hi,

    I have create a simple program for displaying a link list,but it is not working properly.
    The code is as follow:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct linklist
    {
     int data;
     struct linklist *next;
    };
    struct linklist *ptr;
    
    void insert(linklist *ptr)
    {
        printf("INSIDE INSERT%u",ptr );
        int num; 
        struct linklist *temp = ptr;
        struct linklist *node = (struct linklist *)malloc(sizeof(struct linklist));
        printf("\n Enter the data \n");
        scanf("%d",&num);
        node->data = num;
    
        if ( ptr == NULL ) {
            printf("INSIDE IF%u",ptr );
            ptr = node;
            node->next = NULL;
        }
        else {
            printf("INSIDE ELSE"); 
            while ( temp->next != NULL ) {
                temp = temp->next;
            }
            temp->next = node;
            node->next = NULL;
           }
    }
    
    void display(linklist *ptr) 
    {
        struct linklist *temp = ptr;
        while ( temp != NULL ) {
            printf("%d",temp->data );
            temp = temp->next; 
        }
    }
    
        
    int main()
    {
     ptr = NULL; 
        int ch;
        while ( 1 ) {
    
          switch ( ch ) {
          
              case 1:
              insert(ptr);
              break;
     
              case 2:
              display(ptr);
              break;
      
              case 3:
              exit(0);
          }
    
       printf("\nEnter your choice ");
       scanf("%d",&ch);
      }
    }
    The problem is that in insert(ptr) function it always showing the ptr value 0 i.e NULL which I don't understand since the ptr I have declared as global so it should not be initialized to 0 every time the loop runs.
    Can anybody explain me the behavior and solution to it.

    Thanks

  2. #2
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    ptr should be allocated first...

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Pointers themselves are passed by value. your global ptr is not modified by passing it to insert. To do that you'd need to either return ptr as well or prototype the function to take a pointer to a pointer, and pass it that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Linked List Problem
    By Brew in forum C Programming
    Replies: 6
    Last Post: 03-22-2003, 02:39 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM