Another Linked List Question

This is a discussion on Another Linked List Question within the C Programming forums, part of the General Programming Boards category; I have a linked list. Each node has 2 pieces of data (a name and a number) plus the pointer ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    Another Linked List Question

    I have a linked list. Each node has 2 pieces of data (a name and a number) plus the pointer to the next node.

    One of the pieces of data is an arbitrary number. I need to somehow create a function that can print out the names in the ascending order of their corresponding numbers.

    for example if:

    Code:
    NODE 1: brad,5
    NODE 2: joe, 7
    NODE 3: ralph, 2
    then my function should print out:
    Code:
    ralph
    brad
    joe
    does anyone know how I might go about doing this, I'm new to link lists and this is confusing for me, I'm not asking for code just ideas.

    Thank You

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The easiest way to do this is simply to insert the node into the list wherever it falls numerically. Sort it as you build the list. Loop your way through the list checking the value of the node you're on (or the next one, either way, but be careful and always check for NULL) against what you're inserting.

    Otherwise, you can loop through the list N times, where N is the size of the entire list, finding the "next lowest" each time, and printing it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I don't understand, this one has got me really confused. You suggested to ways to do this. I'm not sure how to implement either.

    For the first one, my linked list is already made... if I created another one from it.... I don't see how I can scroll a pointer around a linked list to look for things, cause it seems like I can only momve forward and once I do I can't move back to the previous node

    I do have the number of nodes available to me, but I still dont see how I can loop through my list and look for things

    This is terribly confusing to me

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct node *ptr;
    if( nodetoadd->data < topoflist->data )
        prepend this to the top of the list
    else
        for( ptr = topoflist; ptr != NULL; ptr = ptr->next ) /* walk through the list */
        {
            if( ptr->next == NULL )
                add the node to the end of the current node
                ie: the end of the list
            else
            if( nodetoadd->data < ptr->next->data )
                insert between 'ptr' and 'ptr->next'
            /* else, keep moving down the list... */
        }
    Something like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    I tried to do this.... but it didn't work, I don't know why it doesn't work though

    Code:
    void sort_list(STUDENT* stud) {
         
         STUDENT* first;
         STUDENT* second;
         STUDENT temp_student;
         
         first = stud;
            
         while( first != NULL ) {
                
                second = first -> studentPtr ;
                
                while( second != NULL ) {
                       
                       if ( (first -> ID) < (second -> ID) ) {
                            
                            temp_student.ID = first->ID;
                            strcpy(temp_student.name,first->name);
                            first->ID = second->ID;
                            second->ID=temp_student.ID;
                            strcpy(second->name,temp_student.name);
                            
                            strcpy(temp_student.name,"");
                            temp_student.ID = 0;
                            
                            }
                               second = second -> studentPtr;
                             }
                               first = first -> studentPtr;
                            }
                            }

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Error /* In red */

    Code:
    void sort_list(STUDENT* stud) 
    {  
    STUDENT* first;
    STUDENT* second;
    STUDENT temp_student;
         
    first = stud;
            
    while( first != NULL ) 
       {
       second = first -> studentPtr ;
       while( second != NULL ) 
          {
          if ( (first -> ID) < (second -> ID) ) 
             {
             temp_student.ID = first->ID;
             strcpy(temp_student.name,first->name);
    
             first->ID = second->ID;
            strcpy(first->name, seconed->name);   
             second->ID=temp_student.ID;
             strcpy(second->name,temp_student.name);
                            
             strcpy(temp_student.name,"");
             temp_student.ID = 0;
             }
           second = second -> studentPtr;
           }
        first = first -> studentPtr;
        }
    }

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    What's wrong with the part in red? It moves my data from the second node being tested to the first, if the first is less than the second....

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I added this line

    I added this line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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