Thread: Printing a linked list

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    39

    Printing a linked list

    Everytime I print the list it only prints the last node added into the linked list.

    My print function is as follows:
    Code:
           
     while(head !=NULL)
            {
                    printf("%s , %s , %d\n", head->fname, head->lname, head->ID);
                    head = head->next;
            }
    It'll print out the last name and ID I added to the list.
    How do get it to print out the entire list?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    this loop seems ok..but has a danger!you modify the head pointer,which is the GOD.It is not the human that is going to traverse through the list(so you have to create a temp pointer that will play the role of the human)...Or if it does,at the end of his journey,he must return to the first node( a temp node must be then maintained to the head node)..
    So what i guess ,is that you modify your list in the rest of the program in a way that makes the head pointer to be not in the correct node-which is the first node of course.So maybe you post the rest of the code..Or trying to think of what i said in the 1st paragraph or better yet,both

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by std10093 View Post
    this loop seems ok..but has a danger!you modify the head pointer,which is the GOD.
    That is a complete assumption. A pointer named head does not have to be the only remaining reference to the list. If you modify the only remaining reference to the list, you could lose the ability to free the list completely. Otherwise, it's totes fine.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Well its in a function. It's not actually head.
    But this is the bulk of my main function:
    Code:
            while(fgets(line, MAX_LINE, input))
            {
                    fname = strtok(line, ",");
                    if(!fname) continue;
    
                    lname = strtok(NULL , ",");
                    if(!lname) continue;
    
                    IDstring = strtok(NULL , ",");
                    ID = atoi(IDstring);
    
                    stuPtr = createStudent(ID, fname, lname);
    
    
            }
            printList(stuPtr);
            ptr = stuPtr;
            
            stuPtr = insert(stuPtr, 19923, "Andrew", "Durington");
            printf("=========================\n");
            printList(stuPtr);
    
    
    
            return 0;
    }
    
    //Create Student Function
    Student* createStudent(int ID, char *fname, char *lname)
    {
            Student *newStu = malloc(sizeof(Student));
    
            if((newStu == NULL))
            {
                    printf("Error: Could not allocate memory\n");
                    return NULL;
            }
            newStu->ID = ID;
            strcpy(newStu->fname, fname);
            strcpy(newStu->lname, lname);
            newStu->next = NULL;
    
    
    
    
    
    
            return newStu;
    }
    And my print function is listed in the first post

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Where is the function that add nodes to the list?

    [edit] Or even, why aren't you inserting all the nodes that you read in the loop into the list? That is a big mistake.[/edit]

    If you have an error there, it would explain the faulty printing.
    Last edited by whiteflags; 09-22-2012 at 05:23 PM.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    39
    Ah. Wow. I very badly misinterpreted the directions on this. I should be able to figure this out now. Thanks whiteflags for helping me figure out my stupidity. Should be relatively easy now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 06:29 PM
  2. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 05:33 PM
  3. linked list printing problem
    By devils_own01 in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2005, 03:41 PM
  4. Printing a linked list
    By Jslam9 in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2002, 06:08 PM
  5. Problems printing a linked list.
    By Ed_Severson in forum C Programming
    Replies: 4
    Last Post: 05-03-2002, 10:31 PM