Hello all, I'm having a bit of trouble logically figuring out what to do in this case for my cs150 class. I have to create a linked list from a while loop, but I can't figure out how to link the list successively at the end of each line so that it creates a nice chain.

My original thought was to create a node outside the loop, read the first line, then start the loop at line 2 and after each struct is filled, link the first to that one, create a second temp node to link to the first, then after the next line, link the first to the last/most current, the temp to the previous, and the most current back to the first. (I think)

After that, I need to sort the names in order by ID, but I'm unsure how to do this since I believe my linked list will only follow it's chain as it was created, and I would have to set up some sort of condition that creates links based on some sort of lowest -> highest struct selection sort (for info.id).

Any guidance is greatly appreciated; I'm still trying to think this one through and running into some trouble.

Thanks for any help!

inFile.open("peeps.txt");
    
    if(!inFile)
    {
        cout << "Too bad. No File.";
    }
    
    nodeType *first, *next;
    
    first = new nodeType; //00x
    inFile >> first->info.firstname;
    inFile >> first->info.id;
    inFile >> first->info.height;
    inFile >> first->info.weight;
    first->link = NULL;
    
    while (inFile)
    {
        next = new nodeType; //01x
        inFile >> next->info.firstname;
        //trouble ahead trouble ahead

        
        first->link = next; //meh
    }

I think what you want to do is to load a text file into a single linked-list data structure, and then offer the user to sort the list by certain attributes; in the case you speak of, by user i.d.

So let's break this mother down step by step.

Let's say your .txt file is similar to this format:

Amanda Hugandkiss 04323 19 3.2
Mike Hunt 346543 17 2.6
Oliver Closeoff 19432 18 2.9
Craven Moorehead 65437 14 3.9

So you would natrually want to design a data structure suitable to the text file:

struct student
{
     string first_name;
     string last_name;
     int student_id;
     int age;
     double gpa;
     student* next;
};

Now ye' task is to load your text file into a data structure; in this case, a linked list:

student* temp = new student,
student* head_ptr = temp;

while(infile)
{
     infile >> temp->first_name;
     infile >> temp->last_name;
     infile >> temp->student_id;
     infile >> temp->age; 
     infile >> temp->gpa;
     temp->next = new student;
     temp = temp->next;
}

temp->next = NULL;

All is fine and dandy. Basic file i/o loaded into a simple link-list data structure. simple pointer assignments make sure you have a dedicated head pointer, and the last pointer points to NULL. life == good.

Now you want to let the user sort the list as they desire. In your case, you would like to sort the list in ascending order based on the student_id attribute. Let's use a basic bubble sort:

void sort_by_id(student* head_ptr)
{
     student* node = head_ptr;
     student* temp = NULL;
 
     while(node->next != NULL)
     {
          if(node->student_id > node->next->student_id)
          {
               temp = node;
               node = node->next;
               temp->next = node;
               node = temp;
          } 

          node = node->next;
     }
}

it's just that easy.

Edited 5 Years Ago by Clinton Portis: n/a

A better bubble sort:

void sort_by_id(student* head_ptr)
{
     student* node = head_ptr;
     student* temp = NULL;
 
     while(node->next != NULL)
     {
          if(node->student_id > node->next->student_id)
          {
               temp = node->next;
               node->next = temp->next;
               temp->next = node;
          } 

          node = node->next;
     }
}

Edited 5 Years Ago by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.