Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- struct Bus
- {
- string way_id;
- int time;
- string way;
- string bus_id;
- string cost;
- string date;
- };
- struct Node
- {
- Bus data;
- Node* next;
- Node* prev;
- };
- void print(Node* list)
- {
- if (list != NULL)
- {
- Node* t = list;
- while (t != NULL)
- {
- cout << t->data.bus_id << " " << t->data.way << " " << t->data.date << " " << t->data.cost << endl;
- t = t->next;
- }
- }
- }
- void add(Node** list, Bus bus)
- {
- Node* n = new Node();
- n->data = bus;
- if (*list == NULL)
- {
- n->next = NULL;
- n->prev = NULL;
- *list = n;
- }
- else
- {
- Node* t = *list;
- while (t->next != NULL && t->data.bus_id.compare(bus.bus_id) != 0)
- {
- t = t->next;
- }
- if (t->data.bus_id.compare(bus.bus_id) == 0)
- {
- if (t->data.date < bus.date)
- {
- if (t == *list)
- {
- n->next = *list;
- n->prev = NULL;
- (*list)->prev = n;
- *list = n;
- }
- else
- {
- n->prev = t->prev;
- n->next = t;
- if (t->prev != NULL)
- t->prev->next = n;
- t->prev = n;
- }
- }
- else
- {
- while (t->next != NULL && t->data.bus_id.compare(bus.bus_id) == 0 && t->data.date >= bus.bus_id)
- {
- t = t->next;
- }
- if (t->data.bus_id.compare(bus.bus_id) == 0 && t->data.date < bus.date || t->data.bus_id.compare(bus.bus_id) != 0)
- {
- n->prev = t->prev;
- n->next = t;
- t->prev->next = n;
- t->prev = n;
- }
- else
- {
- n->next = t->next;
- n->prev = t;
- t->next = n;
- }
- }
- }
- else
- {
- n->next = NULL;
- n->prev = t;
- t->next = n;
- }
- }
- }
- void pushBackById(Node* list, string b_id) {
- Node* n = new Node;
- if (list == NULL) {
- n->next = NULL;
- n->prev = NULL;
- list = n;
- }
- else {
- Node* l = list;
- while (l->next != NULL && l->data.bus_id.compare(b_id)) {
- l = l->next;
- }
- l->next = new Node;
- }
- };
- Node* search(Node* list, string id)
- {
- Node* t = list;
- while (t != NULL && t->data.bus_id.compare(id) != 0)
- {
- t = t->next;
- }
- return t;
- }
- int cnt(Node* list, string id)
- {
- int cont = 0;
- Node* t = list;
- while (t != NULL && t->data.bus_id.compare(id) == 0)
- {
- cont++;
- t = t->next;
- }
- return cont;
- }
- void remove(Node** list, Node* bus_list)
- {
- if (*list == bus_list->next)
- {
- while (*list != NULL && (*list)->data.bus_id.compare(bus_list->data.bus_id) == 0)
- {
- Node* rm = *list;
- *list = (*list)->next;
- if ((*list) != NULL)
- (*list)->prev = NULL;
- delete rm;
- }
- }
- else
- {
- Node* t = *list;
- while (t->next != bus_list->next)
- {
- t = t->next;
- }
- Node* rm;
- while (t->next != NULL && t->next->data.bus_id.compare(bus_list->data.bus_id) == 0)
- {
- rm = t->next;
- if (t->next->next != NULL)
- t->next->next->prev = t;
- t->next = t->next->next;
- delete rm;
- }
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- Node* list = NULL; //пустой список
- int command = -1;
- //работа со списком через меню
- while (command != 0)
- {
- cout << "\n1 - Добавить новый автобус\n";
- cout << "2 - Вывод списка в прямом порядке\n";
- cout << "3 - Удалить автобус заданного маршрута\n";
- cout << "4 - Найти маршрутку\n";
- cout << "0 - Выход\n";
- cout << "\nВведите номер команды: ";
- cin >> command;
- cin.ignore();
- switch (command)
- {
- case 1:
- {
- Bus bus;
- cout << "\nВведите номер автобуса: ";
- getline(cin, bus.bus_id);
- cout << "Введите путь: ";
- getline(cin, bus.way);
- cout << "Введите время отправления: ";
- cin >> bus.date;
- add(&list, bus);
- break;
- }
- case 2:
- {
- cout << "\nСписок в прямом порядке\n";
- print(list);
- break;
- }
- case 3:
- {
- string bus_id;
- cout << "\nВведите модель для удаления: ";
- getline(cin, bus_id);
- Node* s = search(list, bus_id); //выполняем поиск
- if (s != NULL) //если поиск успешный
- {
- //список для удаления
- Node* rm_list = new Node();
- rm_list->data.bus_id = bus_id;
- rm_list->next = s;
- remove(&list, rm_list);
- cout << "\nУдаление выполнено\n";
- }
- else
- {
- cout << "\nАвтомобили заданной марки не найдены!\n";
- }
- break;
- }
- case 4:
- {
- cout << "\nВведите номер маршрутки:\n";
- string bus_id;
- getline(cin, bus_id);
- int s = cnt(list, bus_id);
- cout << "\nМаршруток: " << s << endl;
- }
- case 0:
- {
- break;
- }
- default:
- {
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement