spacerose

очередь на ОЛС

Sep 11th, 2020 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Queue
  6. {
  7.     int info;
  8.     Queue *next;
  9. };
  10.  
  11. void Push(Queue *&first, Queue *&last, int info)
  12. {
  13.     Queue *newElem = new Queue;
  14.     newElem->info = info;
  15.     newElem->next = NULL;
  16.     if (first != NULL)
  17.         last = last->next = newElem;
  18.     else
  19.         first = last = newElem;
  20. }
  21.  
  22. int Pop(Queue *&first, Queue *&last)
  23. {
  24.     Queue *tmp = first;
  25.     first = first->next;
  26.     int result = tmp->info;
  27.     delete tmp;
  28.     if (first == NULL)
  29.         last = NULL;
  30.     return result;
  31. }
  32.  
  33. int main()
  34. {
  35.     Queue *qFirst = NULL, *qLast = NULL;
  36.     string command;
  37.     int number;
  38.  
  39.     setlocale(LC_ALL, "rus");
  40.     cout << "Вводите команды управления очередью (признак окончания ввода - END):\n";
  41.     while (true)
  42.     {
  43.         cin >> command;
  44.         if (command == "END")
  45.             break;
  46.         else if (command == "PUSH")
  47.         {
  48.             cin >> number;
  49.             Push(qFirst, qLast, number);
  50.         }
  51.         else if (command == "POP")
  52.             Pop(qFirst, qLast);
  53.         else
  54.             cout << "Такой команды нет!\n";
  55.     };
  56.  
  57.     cout << "В очереди осталось: ";
  58.     while (qFirst != NULL)
  59.         cout << Pop(qFirst, qLast) << ' ';
  60.     cout << endl;
  61.     system("pause");
  62.     return 0;
  63. }
  64.  
Add Comment
Please, Sign In to add comment