Advertisement
Bibodui

Квеве

Dec 9th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int Value;
  9.     Node* Next;
  10. };
  11.  
  12. Node* head;//заглавное звено (голова)
  13. Node* tail;//конечное звено (хвост)
  14.  
  15. void Initial(Node*&, Node*&);
  16. void Output(Node*, Node*);
  17. void AddElement(Node*&, Node*&, int);
  18. int DeleteElement(Node*&, Node*&, int);
  19. void DeleteAll(Node*&, Node*&);
  20.  
  21. int main()
  22. {
  23.     int q;
  24.  
  25.     do
  26.     {
  27.         SetConsoleOutputCP(1251);
  28.         SetConsoleCP(1251);
  29.  
  30.         cout << "Выберите действие:" << endl;
  31.         cout << "1. Ввести элементы очереди" << endl;
  32.         cout << "2. Вывести очередь" << endl;
  33.         cout << "3. Добавить новый элемент в очередь" << endl;
  34.         cout << "4. Удалить элемент из очереди" << endl;
  35.         cout << "5. Удалить очередь" << endl;
  36.         cout << "0. Выход" << endl;
  37.  
  38.         cin >> q;
  39.  
  40.         if (q == 1)
  41.             Initial(head, tail);
  42.         else if (q == 2)
  43.             Output(head, tail);
  44.         else if (q == 3)
  45.         {
  46.             int elem;
  47.             cout << "Введите значение нового элемента" << endl;
  48.             cin >> elem;
  49.             AddElement(head, tail, elem);
  50.         }
  51.         else if (q == 4)
  52.         {
  53.             int elem = 0;
  54.             elem = DeleteElement(head, tail, elem);
  55.         }
  56.         else if (q == 5)
  57.             DeleteAll(head, tail);
  58.  
  59.     } while (q != 0);
  60. }
  61.  
  62. void Initial(Node *&head, Node *&tail)
  63. {
  64.     Node* tmp;
  65.     int elem;
  66.     cout << "Введите элементы:" << endl;
  67.     cin >> elem;
  68.     if (elem != 0)
  69.     {
  70.         tmp = new Node;
  71.         tmp->Value = elem;
  72.         tmp->Next = nullptr;
  73.         head = tail = tmp;
  74.         cin >> elem;
  75.         while (elem != 0)
  76.         {
  77.             tmp = new Node;
  78.             tmp->Value = elem;
  79.             tmp->Next = nullptr;
  80.             tail->Next = tmp;
  81.             tail = tmp;
  82.             cin >> elem;
  83.         }
  84.     }
  85.     else head = tail = tmp = nullptr;
  86. }
  87.  
  88. void Output(Node *head, Node *tail)
  89. {
  90.     if (head == nullptr)
  91.         cout << "Очередь пуста" << endl;
  92.     else
  93.     {
  94.         Node* tmp = head;
  95.         cout << "Вывод очереди:" << endl;
  96.         while (tmp != nullptr)
  97.         {
  98.             cout << tmp->Value << '\t';
  99.             tmp = tmp->Next;
  100.         }
  101.         cout << endl;
  102.     }
  103. }
  104.  
  105. void AddElement(Node*& head, Node*& tail, int elem)
  106. {
  107.     Node* tmp = new Node;
  108.     tmp->Value = elem;
  109.     tmp->Next = nullptr;
  110.     if (head != nullptr)
  111.     {
  112.         tail->Next = tmp;
  113.         tail = tmp;
  114.     }
  115.     else head = tail = tmp;
  116. }
  117.  
  118. int DeleteElement(Node*& head, Node*& tail, int elem)
  119. {
  120.     if (head == nullptr)
  121.         cout << "Очередь пуста" << endl;
  122.     else
  123.     {
  124.         Node* tmp = head;
  125.         elem = head->Value;
  126.         head = head->Next;
  127.         delete tmp;
  128.     }
  129.  
  130.     return elem;
  131. }
  132.  
  133. void DeleteAll(Node*& head, Node*& tail)
  134. {
  135.     if (head == nullptr)
  136.         cout << "Очередь пуста" << endl;
  137.     else
  138.     {
  139.         while (head != nullptr)
  140.         {
  141.             Node* tmp = head;
  142.             head = head->Next;
  143.             delete tmp;
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement