Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct student {
- char fullName[50];
- int yearOfBirth;
- int scolarship;
- student *nextStudent;
- };
- student *firstStudent;
- void createList();
- void enterData(student *student);
- void showList();
- void showStudent(student *student);
- void sortList();
- int main()
- {
- createList();
- std::cout << "\nStudents (before sorting):\n";
- showList();
- sortList();
- std::cout << "\nStudents (after sorting):\n";
- showList();
- return 0;
- }
- void createList()
- {
- char choice;
- firstStudent = NULL;
- student *current;
- do {
- std::cout << "\nContinue? Enter y for yes or n for no: ";
- std::cin >> choice;
- if (choice == 'n') return;
- else {
- if (firstStudent == NULL) {
- firstStudent = new student;
- current = firstStudent;
- }
- else {
- current->nextStudent = new student;
- current = current->nextStudent;
- }
- enterData(current);
- current->nextStudent = NULL;
- }
- } while (true);
- }
- void enterData(student *student)
- {
- std::cout << "\nEnter a fullName for the student: ";
- std::cin >> student->fullName;
- std::cout << "Enter a year of birth: ";
- std::cin >> student->yearOfBirth;
- std::cout << "Enter a scolarship: ";
- std::cin >> student->scolarship;
- }
- void showList()
- {
- student *current = firstStudent;
- while (current != NULL) {
- showStudent(current);
- current = current->nextStudent;
- }
- }
- void showStudent(student *student)
- {
- std::cout << student->fullName << ' ' << student->yearOfBirth << ' ' << student->scolarship << '\n';
- }
- void sortList()
- {
- student *p1, *p2, *q1, *q2, *temp;
- p1 = firstStudent;
- p2 = NULL;
- while (p1->nextStudent != NULL)
- {
- q1 = p1->nextStudent;
- q2 = p1;
- while (q1 != NULL)
- {
- if (p1->fullName[0] < q1->fullName[0])
- {
- if (p1 == q2)
- {
- p1->nextStudent = q1->nextStudent;
- q1->nextStudent = p1;
- p1 = q1;
- q1 = q2;
- q2 = q1;
- }
- else
- {
- temp = p1->nextStudent;
- p1->nextStudent = q1->nextStudent;
- q1->nextStudent = temp;
- p1 = q1;
- q1 = temp;
- q2->nextStudent = q1;
- }
- if (p2 == NULL) firstStudent = p1;
- else p2->nextStudent = p1;
- }
- q2 = q1;
- q1 = q1->nextStudent;
- }
- p2 = p1;
- p1 = p1->nextStudent;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement