Thread: i have problem creating a linked list

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    72

    i have problem creating a linked list

    the problem says to ask input from user about name and age and store it in linked lists also ask him a number each time he finishes putting data in the linked list if he gives 1 that means he wants to store more if he gives 0 that means he doesnt want to store anything else

    then we want to show the results on screen

    here is what i ve done so far

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
           char name[20];
           int age;
           struct node *next;
           };
    
    struct node *prosthiki(int il, char on[]){
           
           struct node *a;
           a = malloc(sizeof(struct node));
           a->age = il;
           strcpy(a->name,on);
           a->next = NULL;
           
           return a;
           
           }
           
    struct node *prosthiki2(int il, char on[], struct node *lol){
           
           lol = malloc(sizeof(struct node));
           lol->age = il;
           strcpy(lol->name,on);
           lol->next = NULL;
           return lol;
           
           
           }
    
    void show(struct node *head){
         struct node *l;
         l=head;
         while(l!=NULL){
                        printf("%d ",l->age);
                        printf("%s\n",l->name);
                        l = l->next;
                        }
         }
         
    int main(void){
        int check = 1;
        int count=0;
        struct node *head = NULL;
        struct node *current = NULL;
        char on[20];
        int gad;
        
        while (check!=0){
              
              printf("give name: \n");
              scanf("%s",on);
              fflush(stdin);
              printf("give age: \n");
              scanf("%d",&gad);
              fflush(stdin);
              if (count==0){
                            head = prosthiki(gad,on);
                            }
              else {
                   current = prosthiki2(gad,on,current);
                   head->next = current;       
                   current = current->next;
                   
                   }
              printf("success!!!!\n\n");
              printf("do you want to continue: \n");
              scanf("%d",&check);
              fflush(stdin);
              count++;
              }
        
        show(head);
        getchar();
        return 0;
    }
    it works for 2 nodes and 1 node, it doesnt work for more than 2 nodes

    what am i doing wrong?

    thanks

    ps: it's not an assignment i was reading an ebook to understand linked lists and came up with this problem

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This:
    Code:
    fflush(stdin);
    will result in undefined behaviour. You can't skip over undefined behavior in debugging -- once you find it, you have to get rid of it, because it is impossible to say what chain of consequences it might have.

    Read this:
    STDIN pitfalls
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by MK27 View Post
    This:
    Code:
    fflush(stdin);
    will result in undefined behaviour. You can't skip over undefined behavior in debugging -- once you find it, you have to get rid of it, because it is impossible to say what chain of consequences it might have.

    Read this:
    STDIN pitfalls
    i used getchar instead but it doesnt work again

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node{
           char name[20];
           int age;
           struct node *next;
           };
    
    struct node *prosthiki(int il, char on[]){
           
           struct node *a;
           a = malloc(sizeof(struct node));
           a->age = il;
           strcpy(a->name,on);
           a->next = NULL;
           
           return a;
           
           }
           
    struct node *prosthiki2(int il, char on[], struct node *lol){
           
           lol = malloc(sizeof(struct node));
           lol->age = il;
           strcpy(lol->name,on);
           lol->next = NULL;
           return lol;
           
           
           }
    
    void show(struct node *head){
         struct node *l;
         l=head;
         while(l!=NULL){
                        printf("%d ",l->age);
                        printf("%s\n",l->name);
                        l = l->next;
                        }
         }
         
    int main(void){
        int check = 1;
        int count=0;
        struct node *head = NULL;
        struct node *current = NULL;
        char on[20];
        int gad;
        
        while (check!=0){
              
              printf("give name: \n");
              scanf("%s",on);
              getchar();
              printf("give age: \n");
              scanf("%d",&gad);
             getchar();
              if (count==0){
                            head = prosthiki(gad,on);
                            }
              else {
                   current = prosthiki2(gad,on,current);
                   head->next = current;       
                   current = current->next;
                   
                   }
              printf("success!!!!\n\n");
              printf("do you want to continue: \n");
              scanf("%d",&check);
              getchar();
              count++;
              }
        
        show(head);
        getchar();
        return 0;
    }
    Last edited by nik2; 02-17-2010 at 04:01 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If by "does not work" you mean the loop ends, then I guess you need to try this:
    Code:
    while (1) {
    instead of "while (check != 0)". while(1) is an infinite loop -- you will have to stop the program with ctrl-c.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    what i exactly mean is that

    if we give input

    Code:
    give name: 
    a
    give age: 
    10
    
    do you want to continue: 
    1
    
    give name:
    b
    give age: 
    20
    
    do you want to continue: 
    0
    the output will be

    Code:
    10 a
    20 b
    but if we give input

    Code:
    give name: 
    a
    give age: 
    10
    
    do you want to continue: 
    1
    
    give name: 
    b
    give age: 
    20
    
    do you want to continue: 
    1
    
    give name: 
    c
    give age: 
    30
    
    do you want to continue: 
    0
    the output will be

    Code:
    0
    10 a
    30 c
    but i want it to be
    Code:
    10 a
    20 b
    30 c
    Last edited by nik2; 02-17-2010 at 04:15 PM.

  6. #6
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Quote Originally Posted by nik2 View Post
    Code:
              else {
                   current = prosthiki2(gad,on,current);
                   head->next = current;       <<-- WRONG
                   current = current->next;
                   
                   }
     
    }
    For the first to loops it works, but after, that code assigns to the first successor of the head, the current node. That means you have a list with only two elements and each round you lost the reference to the second element.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    72
    Quote Originally Posted by flexo87 View Post
    For the first to loops it works, but after, that code assigns to the first successor of the head, the current node. That means you have a list with only two elements and each round you lost the reference to the second element.
    hm yea you're right

    this way won't work I guess, I will try to think of another algorithm

  8. #8
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    The algorithm it's fine, use another reference which points to the Kth-node. Where K is the the value of the variable count, and use it to add the current node to the end of the list. Otherwise you can modify your *prosthiki2() in a way that it scans all the list till the last element and then add the new node. I would choose the first one,... the second is slower (in terms of computation)!

  9. #9
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    head->next = current;
    current = current->next;

    You need to make a pointer to pointer to structures; that pointer to pointer will have the address of the first node.

    After first input, you make that ptr to ptr point to the first node. The following nodes that come need to be directed to the first node (the one ptr to ptr points to) and the pointer to pointer has to be directed at the new node.

    And to output them just iterate through the tree of nodes and print out the proper values.
    Last edited by Tool; 02-17-2010 at 04:43 PM.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    15
    Dear friend,

    You did some mistake in the assign the pointer address. I have attach the correct code here. You need to change the coding in the main function only. No need to change any other coding.
    Then You need to change the calling of show function also. Because here I have changed some coding.


    Code:
     while (check!=0){
    
              printf("give name: \n");
              scanf("%s",on);
              fflush(stdin);
              printf("give age: \n");
              scanf("%d",&gad);
              fflush(stdin);
              if (count==0){
                            head = prosthiki(gad,on);
                            root=head;  // To copy the address of the main node to some other node 
                            }
              else {
                   current = prosthiki2(gad,on,current);
                   head->next = current; // To store the current pointer value to the head of next.
                   head=current; // To store the current address to the head
                   current=current->next; // To store the current of next to current 
                   }
              printf("success!!!!\n\n");
              printf("do you want to continue: \n"); 
              scanf("%d",&check);
              fflush(stdin);
      show(root);
        getchar();
        return 0;
    Explanation:
    ==========
    In the initial stage the pointer "current" value is null. In the function "prosthiki2" you allocate the memory and return that pointer address. It is correct.

    But inside the main function you wrongly assign the address to the head pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circularly linked list
    By BlackOps in forum C Programming
    Replies: 0
    Last Post: 07-18-2009, 08:12 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Problem with creating linked list
    By bigzeppelin2k in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 03:35 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM