Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- using namespace std;
- struct myList
- {
- int value;
- myList *next = nullptr;
- myList *prev = nullptr;
- };
- void PrintList(myList *key, myList *keyEnd)
- {
- int ind = 1;
- if (key == nullptr)
- {
- cout << "[Пустой лист]\n\n";
- return;
- }
- while (key->next != nullptr)
- {
- cout << key->value << " [" << ind << "] ";
- ind++;
- key = key->next;
- }
- cout << key->value << " [" << ind << "]\n\n";
- // while (keyEnd->prev != nullptr)
- // {
- // cout << keyEnd->value << " [" << ind << "] ";
- // ind--;
- // keyEnd = keyEnd->prev;
- // }
- // cout << keyEnd->value << " [" << ind << "]\n\n";
- //ind++;
- //key = key->next;
- }
- int SelectBut(int *indMenu)
- {
- int but = getch();
- if (but == 13)
- return *indMenu;
- if (but == 27)
- return 0;
- if (but - 48 >= 0 && but - 48 <= 5)
- return but - 48;
- if (but == 224)
- {
- but = getch();
- if (but == 72)
- (*indMenu)--;
- else if (but == 80)
- (*indMenu)++;
- *indMenu = ((*indMenu) + 6) % 6;
- return -2;
- }
- return -3;
- //cout << but; //...................................................................
- }
- void PrintMenu(int select, myList *key, myList *keyEnd)
- {
- system("cls");
- PrintList(key, keyEnd);
- char arrow[] = "<--";
- char arrMenu[6][256] =
- {
- "0) Выход........................",
- "1) Создание.....................",
- "2) Удаление.....................",
- "3) Создание несколько...........",
- "4) Удаление несколько...........",
- "5) Сортировка..................."
- };
- for (int i = 0; i < 6; i++)
- {
- cout << arrMenu[i];
- if (select == i)
- cout << arrow;
- cout << endl;
- }
- }
- int SizeList(myList *key)
- {
- if (key == nullptr)
- return 0;
- int ind = 1;
- while (key->next != 0)
- {
- key = key->next;
- ind++;
- }
- return (ind);
- }
- void AddCell(myList **key, myList *cell,myList **keyEnd)
- {
- if (*key == nullptr)
- {
- cout << "Это ваш первый эллемент\nВведите значение: ";
- *key = new myList;
- cin >> (*key)->value;
- (*keyEnd) = (*key);
- return;
- }
- cout << "После какого эллемента добавть новый: ";
- int ind;
- cin >> ind;
- int sizeList = SizeList(cell);
- if ((ind > sizeList) || (ind < 0))
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- cout << "Введите значение: ";
- int value;
- cin >> value;
- myList *demo = new myList;
- demo->value = value;
- if (ind == 0)
- {
- demo->next = (*key);
- (*key)->prev = demo;
- (*key) = demo;
- return;
- }
- for (int i = 1; i < ind; i++)
- cell = cell->next;
- demo->next = cell->next;
- cell->next = demo;
- if (demo->next != nullptr)
- (demo->next)->prev = demo;
- demo->prev = cell;
- if (ind == sizeList)
- {
- (*keyEnd) = demo;
- }
- }
- void AddCellSeveral(myList **key, myList *cell)
- {
- cout << "Сколько эллементов создать: ";
- int num, name = 1, ind = 1;
- cin >> num;
- if (num <= 0)
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- if (*key == nullptr)
- {
- cout << "Это ваши первые эллементы\nВведите значения:\n" << name << " эллемент: ";
- *key = new myList;
- cin >> (*key)->value;
- name++;
- num--;
- cell = *key;
- //cout << cell;
- }
- else
- {
- cout << "После какого эллемента создать: ";
- cin >> ind;
- if ((ind > SizeList(cell)) || (ind < 0))
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- }
- if(ind == 0)
- {
- cout << "Введите значения:\n" << name << " эллемент: ";
- myList *demo = new myList;
- cin >> demo->value;
- demo->next = (*key);
- (*key) = demo;
- num--;
- name++;
- ind = 1;
- cell = (*key);
- }
- for (int i = 1; i < ind; i++)
- cell = cell->next;
- for (int i = 0; i < num; i++)
- {
- cout << "Введите значения:\n" << name << " эллемент: ";
- myList *demo = new myList;
- cin >> demo->value;
- demo->next = cell->next;
- cell->next = demo;
- cell = cell->next;
- name++;
- }
- }
- void DeleteCell(myList **key, myList *cell, myList **keyEnd)
- {
- if (*key == nullptr)
- {
- cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- cout << "Какой эллемент удалить: ";
- int ind;
- cin >> ind;
- int sizeList = SizeList(cell);
- if ((ind > sizeList) || (ind <= 0))
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- if (ind == 1)
- {
- myList *demo = (*key);
- (*key) = (*key)->next;
- if ((*key) != nullptr)
- (*key)->prev = nullptr;
- delete[] demo;
- return;
- }
- for (int i = 1; i < ind-1; i++)
- cell = cell->next;
- myList *demo = cell->next;
- cell->next = cell->next->next;
- if (cell->next != nullptr)
- cell->next->prev = cell;
- if (ind == sizeList)
- {
- (*keyEnd) = cell;
- }
- delete[] demo;
- }
- void DeleteCellSeveral(myList **key, myList *cell)
- {
- if (*key == nullptr)
- {
- cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- cout << "С какого эллемента удалить: ";
- int ind;
- cin >> ind;
- if ((ind > SizeList(cell)) || (ind <= 0))
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- cout << "Сколько эллементов удалить: ";
- int num;
- cin >> num;
- if (ind + num - 1 > SizeList(cell))
- {
- cout << "Недопустимое значение\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- if (ind == 1)
- {
- for (int i = 0; i < num; i++)
- {
- myList *demo = (*key);
- (*key) = (*key)->next;
- delete[] demo;
- }
- return;
- }
- for (int i = 1; i < ind-1; i++)
- cell = cell->next;
- for (int i = 0; i < num; i++)
- {
- myList *demo = cell->next;
- cell->next = demo->next;
- delete[] demo;
- }
- }
- void SortList(myList **key, myList *cell)
- {
- if (*key == nullptr)
- {
- cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- int sizeL = SizeList(*key);
- for (int i = 0; i < sizeL - 1; i++)
- {
- for (int j = 0; j < sizeL - 1; j++)
- {
- myList *cellN = (cell->next);
- if (cell->value > cellN->value)
- {
- int value = cell->value;
- cell->value = cellN->value;
- cellN->value = value;
- }
- cell = cell->next;
- }
- cell = (*key);
- }
- }
- void SortList1(myList **key, myList *cell)
- {
- if (*key == nullptr)
- {
- cout << "Список пустой!\nНажмите любую кнопку, чтобы вернуться в меню...";
- int but = getch();
- return;
- }
- int sizeL = SizeList(*key);
- myList *pred = nullptr;
- for (int i = 0; i < sizeL - 1; i++)
- {
- for (int j = 0; j < sizeL - 1; j++)
- {
- //cout << i << " - " << j << endl;
- myList *cellN = (cell->next);
- if (cell->value > cellN->value)
- {
- if (j == 0)
- {
- cell->next = cellN->next;
- cellN->next = cell;
- (*key) = cellN;
- pred = (*key);
- }
- else
- {
- cell->next = cellN->next;
- cellN->next = cell;
- pred->next = cellN;
- pred = cellN;
- }
- }
- else
- {
- pred = cell;
- cell = cell->next;
- }
- }
- pred = nullptr;
- cell = (*key);
- }
- }
- main()
- {
- setlocale(LC_ALL, "Russian");
- myList *key = nullptr;
- myList *keyEnd = nullptr;
- int indMenu = 0;
- int but = -2;
- while (but != 0)
- {
- PrintMenu(indMenu, key, keyEnd);
- but = SelectBut(&indMenu);
- if (but == 1)
- AddCell(&key, key, &keyEnd);
- else if (but == 2)
- DeleteCell(&key, key, &keyEnd);
- else if (but == 3)
- AddCellSeveral(&key, key);
- else if (but == 4)
- DeleteCellSeveral(&key, key);
- else if (but == 5)
- SortList1(&key, key);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement