Thread: function for linked list

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    function for linked list

    I have written a very simple example for Linked list....
    but i guess I am doing something wrong with function call to print it...

    can someone help here!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct node {
      int  data;              
      struct node *next;         
    }
    
    struct node* build(){
      struct node* head = NULL;
      struct node* third = NULL;
      struct node* second = NULL;
      
      head = (struct node *)malloc(sizeof(struct node));
      second = (struct node *)malloc(sizeof(struct node));
      third = (struct node *)malloc(sizeof(struct node));
      head->data = 1;
      head->next = second;
      
      second->data = 2;
      second->next = third;
      
      third->data = 3;
      third->next = NULL;
      return head;
    } 
    
    void printlist(head){
      struct node *current;
      struct node *lhead;
      current = lhead;
      if(current!= NULL)
        {
          do
    	{
    	  printf("%d\t",current->data);
    	  current = current->next;
    	} while (current!= lhead);
          printf("\n");  
        } 
      else
        printf("The list is empty\n");
      
    }
    
    int main(){
    struct node *list = NULL;
     build(list);
     printlist(list);
    
    }
    thanks!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > current = lhead;


    Um, I think you mean current = head;

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    ya...i had trieid that too...
    but then i get bus error....

    I guess i m writing some wrong function

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    74
    You're not saving the result of the build function, the head of the list.

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanks !
    i figured out later

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM