Advertisement
Guest User

Лаба 14 2005

a guest
Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct student {
  4. char fullName[50];
  5. int yearOfBirth;
  6. int scolarship;
  7. student *nextStudent;
  8. };
  9.  
  10. student *firstStudent;
  11.  
  12. void createList();
  13. void enterData(student *student);
  14. void showList();
  15. void showStudent(student *student);
  16. void sortList();
  17.  
  18. int main()
  19. {
  20. createList();
  21. std::cout << "\nStudents (before sorting):\n";
  22. showList();
  23. sortList();
  24. std::cout << "\nStudents (after sorting):\n";
  25. showList();
  26.  
  27. return 0;
  28. }
  29.  
  30. void createList()
  31. {
  32. char choice;
  33. firstStudent = NULL;
  34. student *current;
  35. do {
  36. std::cout << "\nContinue? Enter y for yes or n for no: ";
  37. std::cin >> choice;
  38. if (choice == 'n') return;
  39. else {
  40. if (firstStudent == NULL) {
  41. firstStudent = new student;
  42. current = firstStudent;
  43. }
  44. else {
  45. current->nextStudent = new student;
  46. current = current->nextStudent;
  47. }
  48. enterData(current);
  49. current->nextStudent = NULL;
  50. }
  51.  
  52. } while (true);
  53. }
  54.  
  55. void enterData(student *student)
  56. {
  57. std::cout << "\nEnter a fullName for the student: ";
  58. std::cin >> student->fullName;
  59. std::cout << "Enter a year of birth: ";
  60. std::cin >> student->yearOfBirth;
  61. std::cout << "Enter a scolarship: ";
  62. std::cin >> student->scolarship;
  63. }
  64.  
  65. void showList()
  66. {
  67. student *current = firstStudent;
  68. while (current != NULL) {
  69. showStudent(current);
  70. current = current->nextStudent;
  71. }
  72. }
  73.  
  74. void showStudent(student *student)
  75. {
  76. std::cout << student->fullName << ' ' << student->yearOfBirth << ' ' << student->scolarship << '\n';
  77. }
  78.  
  79. void sortList()
  80. {
  81. student *p1, *p2, *q1, *q2, *temp;
  82. p1 = firstStudent;
  83. p2 = NULL;
  84. while (p1->nextStudent != NULL)
  85. {
  86. q1 = p1->nextStudent;
  87. q2 = p1;
  88. while (q1 != NULL)
  89. {
  90. if (p1->fullName[0] < q1->fullName[0])
  91. {
  92. if (p1 == q2)
  93. {
  94. p1->nextStudent = q1->nextStudent;
  95. q1->nextStudent = p1;
  96. p1 = q1;
  97. q1 = q2;
  98. q2 = q1;
  99. }
  100. else
  101. {
  102. temp = p1->nextStudent;
  103. p1->nextStudent = q1->nextStudent;
  104. q1->nextStudent = temp;
  105. p1 = q1;
  106. q1 = temp;
  107. q2->nextStudent = q1;
  108. }
  109. if (p2 == NULL) firstStudent = p1;
  110. else p2->nextStudent = p1;
  111. }
  112. q2 = q1;
  113. q1 = q1->nextStudent;
  114. }
  115. p2 = p1;
  116. p1 = p1->nextStudent;
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement