Printing linked lists

This is a discussion on Printing linked lists within the C Programming forums, part of the General Programming Boards category; Trying to print a linked list, but the first item is the only thing that prints. I've played around with ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    Printing linked lists

    Trying to print a linked list, but the first item is the only thing that prints. I've played around with it, and I cannot figure out whether it is my print function, or my construction of the list that is causing this.

    This is my construction.
    Code:
    Player* open( FILE *fp ){
            int i, lines;
            char hold[ MAX_LINE ] = { 0 };
            char* token = NULL;
            const char space[] = " ";
            const char nLine[] = "\n";
    
    
            Player* head = NULL;
            char name[ MAX_NAME ] = { 0 };
            int goals;
            int assists;
    
    
            lines = countLines( fp );
    
    
    /* Tokenize the strings in the file and pass them to link. */
            for( i = 0; i < lines; i++ ){
                    fgets( hold, MAX_LINE, fp );
    
    
                    token = strtok( hold, space );
                    strcpy( name, token );
    
    
                    token = strtok( NULL, space );
                    goals = atoi( token );
    
    
                    token = strtok( NULL, nLine );
                    assists = atoi( token );
    
    
    /* In the first iteration, head is NULL, thus the newly created node will be the head
     * of the list. We are returning to head because we are not passing head by reference. */
                    head = link( head, name, goals, assists );
    
    
                    if( token == NULL ){
                            break;
                    }//endif
            }//endfr
    
    
            return head;
    }//endfn
    
    
    Player* link( Player* x, char name[], int goals, int assists ){
            Player* current = x; // Set our record pointer to the passed node.
    
    
    /* We are calling createRecord to malloc and assign pertinant data to the node. */
            Player* newPlayer = createPlayer( name, goals, assists );
    
    
    /* Now we check to see if the list is empty. */
            if( x == NULL ){
                    return newPlayer;
    
    
            }else{
                    while( current != NULL ){
                            current = current->next;
                    }//endwh
    
    
                    current = newPlayer;
            }//endel        
    
    
            return x;
    }//endfn
    Where createPlayer simply mallocs and assigns pertinent data to created node, then returns the pointer to the node.


    This is my print function.
    Code:
    void printPlayers( Player* head ){
    
            Player* x = head;
    
    
            if( x == NULL ){
                    printf("\n\nThe list is empty!\n");
            }else{
                    printf("\nName       Goals      Assists");
    
    
                    while( x != NULL ){
                            printf("\n%s %5d %10d", x->name, x->goals, x->assists );
    
    
                            x = x->next;
                    }//endwh
            }//endel

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I don't see a line where you link the "next" pointer to another node in your list. Something like "current->next = ..."

    Bye, Andreas

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    In other words, the print function is fine, and it is the contruction that is faulty.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    Quote Originally Posted by AndiPersti View Post
    I don't see a line where you link the "next" pointer to another node in your list. Something like "current->next = ..."

    Bye, Andreas
    I see, for anyone who might search for this in the future..

    I changed this:
    Code:
    while( current != NULL ){ 
                           current = current->next; 
                   }//endwh 
                   current = newPlayer;
    to this:

    Code:
    while( current->next != NULL ){ 
                           current = current->next; 
                   }//endwh 
                   current->next = newPlayer;
    And this works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 12:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Replies: 8
    Last Post: 03-15-2010, 01:23 PM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Printing linked lists
    By jcramer in forum C Programming
    Replies: 2
    Last Post: 03-08-2004, 12:57 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21