Thread: linked list

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    linked list

    Code:
    #include <stdio.h>
    
    struct node{
    	int age;
    	struct node *next;
    };
    
    void add_head(struct node **head,struct node *new_node);
    void printList(struct node **head);
    void add_tail(struct node **head,struct node *new_node);
    
    int main(void){
    
        struct node *head=0;
        struct node *newNode;
        struct node *newNodeA;
        
        /*create a new Node.Add it to the head*/
        newNode=(struct node*)malloc(sizeof(struct node));
        newNode->age=2;
        add_head(&head,newNode);
        printList(&head);
        
        /*create a new Node.Add it to the tail*/              
        newNodeA=(struct node*)malloc(sizeof(struct node));
        newNodeA->age=3;
        add_tail(&head,newNodeA);
        printList(&head);    
    }    
    
    void add_head(struct node **head,struct node *new_node){
         new_node->next=*head;
        *head=new_node;
    }
    
    void add_tail(struct node **head,struct node *new_node){
        struct node *current=*head;
      
        while(current!=NULL){
            current=current->next;              
        }
        current->next=new_node;         
        
          
    }
         
         
    void printList(struct node **head){
         struct node *current=*head;
         
         while(current!=NULL){
         printf("\n%d",current->age);                         
         current=current->next;
         }
    }
    the above code is a linked list. it doesn't do the add tail.can any one let me know why? BUT it compiles error-free.

  2. #2
    old man
    Join Date
    Dec 2005
    Posts
    90
    The first problem I notice: you cast malloc(), which suppresses the error you should receive for not including stdlib.h ...

    Even after fixing that, it still segfaults, so I'm looking at the rest of your code as well ...

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Few issues, here:

    Code:
    void add_tail(struct node **head,struct node *new_node){
        struct node *current=*head;
      
        while(current!=NULL){
            current=current->next;              
        }
        current->next=new_node;    // Consider what you're asking here.
            
    }
    You're looping until your current pointer is the NULL pointer. Then you're trying to access the next member of this NULL pointer. Does that make sense? When you traverse through a linked list, you're looking for the pointer who's next member is NULL, not the current object. Secondly, you have another issue where you just kind of hope your next pointer equals NULL without assigning NULL to it. That's bad. Infact, if you don't assign it, then you'll most likely end up with an infinte loop because it will never find the null pointer.

    I've taken the liberty of rewriting the code a bit for you. I want you to look at it and see what I did.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
    	int age;
    	struct node *next;
    };
    
    void add_head(struct node **head,struct node *new_node);
    void printList(struct node *head);                       // There is no reason
    void add_tail(struct node *head,struct node *new_node);  // to pass a pointer to head in these
                                                             // two functions. I changed it.
    int main(void){
    
        struct node *head=0;
        struct node *newNode;
        struct node *newNodeA;
        
        /*create a new Node.Add it to the head*/
        newNode=(struct node*)malloc(sizeof(struct node));
        newNode->age=2;
        add_head(&head,newNode);
        printList(head);
        
        /*create a new Node.Add it to the tail*/              
        newNodeA=(struct node*)malloc(sizeof(struct node));
        newNodeA->age=3;
        add_tail(head,newNodeA);
        printList(head);    
        getchar();
    }    
    
    void add_head(struct node **head,struct node *new_node){
         new_node->next=*head;
        *head=new_node;
    }
    
    void add_tail(struct node *head,struct node *new_node){
        struct node *current=head;
        
        while(current->next!=NULL){  // Find the last tangible object in the list
            current=current->next;              
        }
        current->next=new_node;      // Set it's next member to the new node.
        current = current->next;     // Go to that new tail member
        current->next = NULL;        // Set it's next member to NULL
          
    }
         
         
    void printList(struct node *head){
         struct node *current=head;
         
         while(current->next!=NULL){ // Find the last tangible object in the list
         printf("%d",current->age);                         
         current=current->next;
         }
         printf("%d\n",current->age); // Print the last member's age.
    }
    Last edited by SlyMaelstrom; 03-01-2006 at 11:55 AM.
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by rahulsk1947
    Code:
    void add_tail(struct node **head,struct node *new_node){
        struct node *current=*head;
      
        while(current!=NULL){
            current=current->next;              
        }
        current->next=new_node;         
        
          
    }
    the above code is a linked list. it doesn't do the add tail.can any one let me know why? BUT it compiles error-free.
    When your loop condition becomes false (NULL), you try to dereference current, which is NULL. You could solve that by reading ahead one node e.g, current->next != NULL.


    - xeddiex

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
    	int age;
    	struct node *next;
    };
    
    void add_head(struct node **head,struct node *new_node);
    void printList(struct node *head);                       // There is no reason
    int pop(struct node **headRef);
                                                             // two functions. I changed it.
    int main(void){
    
        struct node *head=0;
        struct node *newNode;
        struct node *newNodeA;
        
        /*create a new Node.Add it to the head*/
        newNode=(struct node*)malloc(sizeof(struct node));
        newNode->age=2;
        add_head(&head,newNode);
        printList(head);
        
        /*create a new Node.Add it to the tail*/              
        newNodeA=(struct node*)malloc(sizeof(struct node));
        newNodeA->age=3;
        add_head(&head,newNodeA);
        printList(head);
        
        int value=pop(&head);
        printf("\n Popped value is %d \n*****head removed***\n",value);
        printList(head);
        
            
        
        
    }    
    
    int pop(struct node **headRef){
        struct node *current;
        current=*headRef;
        
        int data=current->age;
        
         current=current->next;  //point to next node
        *headRef=current;
        
        free(current);
        return(data);
    }
    
    void add_head(struct node **head,struct node *new_node){
         new_node->next=*head;
        *head=new_node;
    }
         
    void printList(struct node *head){
         struct node *current=head;
         
         printf("\nThe list is: ");
         while(current->next!=NULL){ // Find the last tangible object in the list
         printf("\n%d",current->age);                         
         current=current->next;
         }
         printf("\n%d\n",current->age); // Print the last member's age.
    }

    Above is my linked list.My query is in the function pop i wrote to remove the head. The function does remove the head and pop's the right value.

    But my problem is the new head value .It becomes ZERO .
    why is it so? Can some one clarify this !!

    Thanks.
    Last edited by rahulsk1947; 03-07-2006 at 10:05 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void printList(struct node *head){
         struct node *current=head;
         
         printf("\nThe list is: ");
         while(current->next!=NULL){ // Find the last tangible object in the list
         printf("\n%d",current->age);                         
         current=current->next;
         }
         printf("\n%d\n",current->age); // Print the last member's age.
    }
    This code assumes head won't be NULL . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM