I'm trying to sort my linked list by students' last name (sortLastName function), and it's not working. Can anyone please tell me what I'm doing wrong? Thanks in advance.
PS: Here are the contents of the students.dat file, incase you need it.
Smith Jefferey 891789 j.smith@spartans.nsu.edu 3.75
Lee Jackson 3678902 j.lee@spartans.nsu.edu 3.66
Gonzales Mariana, 168790 m.gonzales18@spartans.nsu.edu 4.0
Jones Mike 8973125 m.jones143@spartans.nsu.edu 3.1
Williams Anita 2985465 a.williams@spartans.nsu.edu 3.64
Ronsinson Taylor 3278976 t.robinson@spartans.nsu.edu 3.55
Clark Allen 1094567 a.clark@spartans.nsu.edu 3.48
Turner Tavon 318796 t.turner@spartans.nsu.edu 3.2
Jenkins Nelson 289563 n.jenkins@spartans.nsu.edu 3.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
using namespace std;
#include <iostream>
#include <fstream>
#include<string>
struct student;
typedef student * studentptr, * head, * tail;
struct student
{
string firstName;
string lastName;
unsigned int studentID;
string email;
float gpa;
studentptr next;
};
class studentsProc // Definition of class named studentsProc
{
public:
studentsProc();
void loadList(); // Function that loads data from a data file into a student list
void printList();
void retrieveStudent();
void insertStudent();
void deleteStudent();
void printStudent();
void sortLastName();
void sortGPA();
void insertOrder(studentptr s);
void printSortedList(studentptr sortedStudents);
private:
student * students; // Pointer to the list of students
void print() const; //Print the contents of the list
};
studentsProc::studentsProc()
{
students = NULL;
}
void studentsProc::loadList()
{
ifstream infile;
infile.open("students.dat");
students = NULL; //head pointer points no where
student temp;
studentptr temp_ptr;
while (!infile.eof())
{
temp_ptr = new student;
infile >> temp_ptr->lastName >> temp_ptr->firstName >> temp_ptr->studentID >> temp_ptr->email >> temp_ptr->gpa;//place data in the node
temp_ptr->next = students;
students = temp_ptr;
}
infile.close();
}
void studentsProc::retrieveStudent()
{
string lastName;
cout << endl;
print();
cout << endl;
cout << "Please Enter the Last Name of the Student's Information you Wish to Retrieve: " << endl;
cin >> lastName;
cout << endl << "Here is the Information You Requested: " << endl;
for (studentptr s = students; s != NULL; s = s->next)
if(lastName==s->lastName)
{
cout << "Student Name: " << s->lastName << ", " << s->firstName << endl << endl;
}
}
void studentsProc::insertStudent()
{
studentptr temp = new student;
cout << endl;
cout << "Please Enter the Following Information About The New Student: " << endl;
cout << "Last Name: "; cin >> temp->lastName;
cout << "First Name: "; cin >> temp->firstName;
cout << "Student ID: "; cin >> temp->studentID;
cout << "E-Mail: "; cin >> temp->email;
cout << "GPA: "; cin >> temp->gpa;
cout << endl;
temp->next = students;
students = temp;
cout << "The Student List Including the New Student's Information: " << endl;
printList();
}
void studentsProc::deleteStudent()
{
cout << endl;
studentptr prev, pos;
string lastName;
bool done = false;
pos = NULL;
prev = NULL;
print();
cout << endl;
cout << "Please Enter the Student's Last Name you Wish to Delete: " << endl;
cin >> lastName;
cout << endl;
for (studentptr s = students; (s != NULL) && (done == false); s = s->next)
{
if (s->lastName == lastName)
{
done = true;
pos = s;
}
else
{
prev = s;
}
}
if(prev != NULL)
{
prev->next = pos->next;
}
else
{
students = students->next;
}
cout << "The Student List Excluding the New Student's Information: " << endl;
printList();
}
void studentsProc::printList()
{
cout << endl;
for (studentptr s = students; s != NULL; s = s->next)
cout << "First Name: " << s->firstName << " * "
<< "Last Name: " << s->lastName << " * "
<< "Student ID: " << s->studentID << " * "
<< "E-Mail: " << s->email << " * "
<< "GPA: " << s->gpa << endl;
}
void studentsProc::sortLastName()
{
studentptr sortedStudents = NULL;
for (studentptr s = students; s!= NULL; s=s->next)
{
insertOrder(s);
}
}
void studentsProc::insertOrder(studentptr node)
{
studentptr prev, pos;
studentptr temp = new student;
studentptr sortedStudents = NULL;
bool done = false;
pos = NULL;
prev = NULL;
temp->lastName = node->lastName;
temp->firstName = node->firstName;
temp->studentID = node->studentID;
temp->email = node->email;
temp->gpa = node->gpa;
temp->next = NULL;
for (studentptr s = sortedStudents; (s!=NULL) && (done == false); s=s->next)
{
if (s->lastName > temp->lastName)
{
done = true;
pos = s;
}
else
{
prev = s;
}
}
if (pos == NULL)
{
sortedStudents=temp;
temp->next = NULL;
}
else if (prev == NULL)
{
temp->next = sortedStudents;
sortedStudents = temp;
}
else
{
prev->next = temp;
temp->next = pos;
}
printSortedList(sortedStudents);
}
void studentsProc::printSortedList(studentptr sortedStudents)
{
cout << endl;
for (studentptr s = sortedStudents; s != NULL; s = s->next)
cout << "First Name: " << s->firstName << " * "
<< "Last Name: " << s->lastName << " * "
<< "Student ID: " << s->studentID << " * "
<< "E-Mail: " << s->email << " * "
<< "GPA: " << s->gpa << endl;
}
void studentsProc::sortGPA()
{
}
void studentsProc::printStudent()
{
string lastName;
cout << endl;
print();
cout << endl;
cout << "Please Enter the Last Name of the Student's Information you Wish to Retrieve: " << endl;
cin >> lastName;
cout << endl << "Here is the Information You Requested: " << endl;
for (studentptr s = students; s != NULL; s = s->next)
if(lastName==s->lastName)
{
cout << "Last Name: " << s->lastName << " * "
<< "First Name: " << s->firstName << " * "
<< "Student ID: " << s->studentID << " * "
<< "E-mail: " << s->email << " * "
<< "GPA: " << s->gpa << endl;
}
cout << endl;
}
void studentsProc::print() const
{
cout << "Student List of Names: " << endl;
for (studentptr s = students; s != NULL; s = s->next)
cout << "Last Name: " << s->lastName << " * "
<< "First Name: " << s->firstName << endl;
}
int main()
{
int choice;
studentsProc student;
student.loadList();
do
{
// Menu options to carry out specific operations for student list
cout << "Please Choose an Option from this Menu" << endl << endl;
cout << "1. Retrieve and Print a Student from the Student List " << endl;
cout << "2. Insert a Student into the Student List " << endl;
cout << "3. Delete a Student from the Student List " << endl;
cout << "4. Print the Contents of a Student Record " << endl;
cout << "5. Print the Contents for a List of Students " << endl;
cout << "6. View the Students List Sorted by Last Name " << endl;
cout << "7. View the Students List Sorted by GPA " << endl;
cout << "8. Exit the Program" << endl;
cin >> choice; // Asks user to input their choice
// Switch menu that carries out the specific operations for student list based on the user's input
switch (choice)
{
case 1: student.retrieveStudent(); // Function call to retrieve and print a student's information from the student list
break;
case 2: student.insertStudent(); // Function call to insert a student's information into the student list
break;
case 3: student.deleteStudent(); // Function call to delete a student's information from the student list
break;
case 4: student.printStudent(); // Function call to print the contents of a student's record
break;
case 5: student.printList(); // Function call to print the contents for a list of students
break;
case 6: student.sortLastName(); // Function call to sort the student list by students' last names
break;
case 7: student.sortGPA(); // Function call to sort the student list by students' GPAs
break;
case 8: exit(0);
default: cout << "Please enter a value 1-8" << endl; // Default case in which user does not enter a number between 1 and 8, asks user to reenter value
}//end switch
} while (choice != 9);
return 0;
}
|