Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- struct Student
- {
- std::string name;
- std::string group;
- unsigned short book;
- unsigned short marks[4];
- };
- struct ListNode
- {
- Student value;
- ListNode *prev = nullptr;
- ListNode *next = nullptr;
- };
- struct List
- {
- ListNode *front = nullptr;
- ListNode *back = nullptr;
- };
- int pushListStudent(List *const list, Student &&student)
- {
- if (list->front == nullptr) { // Элементы в списке отсутствуют
- list->front = new ListNode({
- std::forward<Student>(student)
- }); // Создаем
- list->back = list->front;
- } else {
- ListNode *last = list->back;
- list->back = new ListNode({
- std::forward<Student>(student)
- });
- list->back->prev = last; // Линковка
- last->next = list->back;
- }
- return 0;
- }
- // Освобождение памяти
- int destroyList(List *const list)
- {
- for (ListNode *current = list->front; current != nullptr; current = current->next) {
- delete current->prev; // Освобождение предыдущего
- }
- delete list->back; // Освобождение последнего
- list->front = list->back = nullptr; // Очистка
- return 0;
- }
- int swap(ListNode *const first, ListNode *const second, List *const list)
- {
- if (first == second)
- return 0;
- // Копируем указатели, чтобы анализировать их
- // даже после перезаписи
- ListNode *firstPrev = first->prev;
- ListNode *firstNext = first->next;
- ListNode *secondPrev = second->prev;
- ListNode *secondNext = second->next;
- // Перелинковываем указатели у вершин
- // (Меняем друг с другом)
- // Если вершины рядом, то нужно линковать соседей
- first->prev = (firstNext == second) ? second : second->prev;
- first->next = (firstPrev == second) ? second : second->next;
- second->prev = (secondNext == first) ? first : firstPrev;
- second->next = (secondPrev == first) ? first : firstNext;
- // Теперь правильно ликнуем те вершины, которые ссылались на first и second
- if (first->prev)
- first->prev->next = first;
- if (first->next)
- first->next->prev = first;
- if (second->next)
- second->next->prev = second;
- if (second->prev)
- second->prev->next = second;
- // Обновляем поля списка, если это нужно
- if (second->next == nullptr)
- list->back = second;
- else if (second->prev == nullptr)
- list->front = second;
- if (first->next == nullptr)
- list->back = first;
- else if (first->prev == nullptr)
- list->front = first;
- return 0;
- }
- // Сортировка
- void sort(List *const list)
- {
- // Своеобразный цикл для обхода списка
- for (ListNode *current = list->front; current != nullptr; current = current->next) {
- ListNode *minimum = current;
- for (ListNode *inside = current; inside != nullptr; inside = inside->next) {
- if (inside->value.name < minimum->value.name) {
- minimum = inside;
- }
- }
- swap(minimum, current, list);
- current = minimum; // Минимум переместится на место current
- // И будет левее
- }
- }
- int main()
- {
- List mainList;
- size_t amount; // Размер списка
- std::cin >> amount;
- for (size_t i = 0; i < amount; i++) {
- Student student;
- std::cin.ignore(); // Костыль
- std::cout << "[S " << std::setw(3) << i << "] Name : ";
- std::getline(std::cin, student.name); // Ввод
- std::cout << "[S " << std::setw(3) << i << "] Group : ";
- std::getline(std::cin, student.group);
- std::cout << "[S " << std::setw(3) << i << "] Book : ";
- std::cin >> student.book;
- for (size_t j = 0; j < 4; j++) {
- std::cout << "[S " << std::setw(3) << i
- << "] Mark(" << j << "): ";
- std::cin >> student.marks[j];
- }
- pushListStudent(&mainList, std::move(student));
- }
- sort(&mainList); // Сортируем
- // Своеобразный цикл для обхода списка
- for (ListNode *current = mainList.front; current != nullptr; current = current->next) {
- static size_t counter = 0; // Счетчик
- std::cout << "[S " << std::setw(3) << counter
- << "] Name : " << current->value.name
- << std::endl;
- std::cout << "[S " << std::setw(3) << counter
- << "] Group : " << current->value.group
- << std::endl;
- std::cout << "[S " << std::setw(3) << counter
- << "] Book : " << current->value.book
- << std::endl;
- for (size_t j = 0; j < 4; j++)
- std::cout << "[S " << std::setw(3) << counter
- << "] Mark(" << j << "): "
- << current->value.marks[j] << std::endl;
- counter++;
- }
- destroyList(&mainList);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement