Advertisement
spacerose

Untitled

Sep 22nd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Queue
  6. {
  7.     float info;
  8.     Queue* next;
  9. };
  10.  
  11. float Print(Queue*& head) {
  12.     Queue* tmp = head;
  13.     return head->info;
  14. }
  15.  
  16. void Push(Queue*& head, Queue*& last, float info)
  17. {
  18.     Queue* newElem = new Queue;
  19.     newElem->info = info;
  20.     newElem->next = NULL;
  21.     if (head != NULL)
  22.         last = last->next = newElem;
  23.     else
  24.         head = last = newElem;
  25. }
  26.  
  27. void Empty(int sz)
  28. {
  29.     if (sz==0) cout << "Очередь пуста";
  30.     else
  31.         cout << "Очередь не пуста";
  32. }
  33.  
  34. void Full(int sz)
  35. {
  36.     if (sz == 7) cout << "Очередь полна, заполнение невозможно";
  37.     else
  38.         cout << "Очередь не полна";
  39. }
  40. float Pop(Queue*& head, Queue*& last)
  41. {
  42.     Queue* tmp = head;
  43.     head = head->next;
  44.     float result = tmp->info;
  45.     delete tmp;
  46.     if (head == NULL)
  47.         last = NULL;
  48.     return result;
  49. }
  50.  
  51. int main()
  52. {
  53.     Queue* qHead = NULL, * qLast = NULL;
  54.     string command;
  55.     float number;
  56.  
  57.     setlocale(LC_ALL, "rus");
  58.     int size=0;
  59.     cout << "Введите команды управления очередью:\nPUSH - добавление элементa\nPOP - удаление элемента из очереди и его вывод\nPRINT - вывод первого в очереди\nEMPTY - проверка пустоты очереди\nFULL - проверка полноты очереди\nEND - вывод элементов очереди\n\n";
  60.     while (true)
  61.     {
  62.         cin >> command;
  63.         if (command == "END")
  64.             break;
  65.         else if (command == "PUSH")
  66.         {
  67.             if (size < 7) {
  68.                 size++;
  69.                 cin >> number;
  70.                 Push(qHead, qLast, number);
  71.             }
  72.             else
  73.                 Full(size);
  74.         }
  75.         else if (command == "POP")
  76.         {
  77.             if (size > 0) {
  78.                 size--;
  79.                 cout << Pop(qHead, qLast) << endl;
  80.             }
  81.             else
  82.                 Empty(size);
  83.         }
  84.         else if (command == "PRINT")
  85.             cout << Print(qHead) << endl;
  86.         else if (command == "FULL")
  87.             Full(size);
  88.         else if (command == "EMPTY")
  89.             Empty(size);
  90.  
  91.         else
  92.             cout << "Такой команды нет!\n";
  93.     };
  94.  
  95.     cout << "В очереди осталось: ";
  96.     while (qHead != NULL)
  97.         cout << Pop(qHead, qLast) << ' ';
  98.     cout << endl;
  99.     system("pause");
  100.     return 0;
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement