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.