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

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Creating a simple linked list


    Hi All, I am trying to create a linked list which contains data of user defined structure called customer. Can someone please give me an example?

    Thanks!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    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
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    who said I didn't?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > who said I didn't?
    YOU said it when you said "give me an example".
    Linked lists have been endlessly re-implemented, so finding an example should be as easy as falling off a log for anyone with more than 2-seconds of attention span to find.

    I rather suspect that your real question is "I've tried to write one, but it doesn't work, so I need a better example to use".

    If you really do have your own code, then post your code and we'll tell you what you did wrong. Learning how to write code and fix your own code is far better than simply giving up and then asking for someone else to give you the code.

    Don't forget to put [code][/code] tags around your code.
    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
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    This is what I tried up till now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //linked list
    
    //NODE
    
    typedef struct {
      int x;
      struct node *next;
    }node;
    
    struct node* BuildFirstThree(){
    	struct node* head = NULL;
    	struct node* second = NULL;
    	struct node* third = NULL;
    	
    	head = malloc(sizeof(struct node));
    	second = malloc(sizeof(struct node));
    	third = malloc(sizeof(struct node));
    	
    	head-> x =1;
    	head-> next = second;
    	
    	second-> x = 2;
    	second-> next = third;
    	
    	third -> x = 3; 
    	third -> next = NULL;
    	
    	return head;
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    What I am trying to work out is a method to add to that list, so that I can make a dynamic structure.

    Originally Posted by mrcuta
    This is what I tried up till now:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //linked list
    
    //NODE
    
    typedef struct {
      int x;
      struct node *next;
    }node;
    
    struct node* BuildFirstThree(){
    	struct node* head = NULL;
    	struct node* second = NULL;
    	struct node* third = NULL;
    	
    	head = malloc(sizeof(struct node));
    	second = malloc(sizeof(struct node));
    	third = malloc(sizeof(struct node));
    	
    	head-> x =1;
    	head-> next = second;
    	
    	second-> x = 2;
    	second-> next = third;
    	
    	third -> x = 3; 
    	third -> next = NULL;
    	
    	return head;
    }
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Say
    Code:
    struct node*append( struct node *head, int value ) {
      // malloc a node
      // seek the last node of the list
      // update the links
      // return the (possibly modified) head
    }
    Where in main you do
    Code:
    struct node *head = NULL;
    head = append( head, 1 );
    head = append( head, 2 );
    head = append( head, 3 );
    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