Advertisement
Guest User

Untitled

a guest
May 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 5
  4. struct queue {
  5.     int priority;
  6.     int data;
  7.     struct queue *next;
  8. };
  9.  
  10. queue *priorityQueue = NULL;
  11. int queueSize = 0;
  12.  
  13. bool isFull() {
  14.     return queueSize == MAX;
  15. }
  16.  
  17. bool isEmpty() {
  18.     return priorityQueue == NULL;
  19. }
  20.  
  21. void push(int i, int p) {
  22.     if (!isFull()) {
  23.         queue *t, *q;
  24.         t = new queue;
  25.         t->data = i;
  26.         t->priority = p;
  27.         if (isEmpty() || p < priorityQueue->priority) {
  28.             t->next = priorityQueue;
  29.             priorityQueue = t;
  30.         }
  31.         else {
  32.             q = priorityQueue;
  33.             while (q->next != NULL && q->next->priority <= p)
  34.                 q = q->next;
  35.             t->next = q->next;
  36.             q->next = t;
  37.         }
  38.         queueSize++;
  39.     }
  40.     else {
  41.         cout << "Kolejka jest pelna" << endl;
  42.     }
  43. }
  44.  
  45. void pop() {
  46.     queue *t;
  47.     if (isEmpty())
  48.         cout << "Kolejka jest pusta" << endl;
  49.     else {
  50.         t = priorityQueue;
  51.         cout << "Usuwany element to : " << t->data << endl;
  52.         priorityQueue = priorityQueue->next;
  53.         free(t);
  54.         queueSize--;
  55.     }
  56. }
  57.  
  58. void show() {
  59.     if (isEmpty())
  60.         cout << "Kolejka jest pusta" << endl;
  61.     else {
  62.         cout << "Kolejka:" << endl;
  63.         cout << "Priorytet | Liczba" << endl;
  64.         while (!isEmpty()) {
  65.             cout << priorityQueue->priority << " | " << priorityQueue->data << endl;
  66.             priorityQueue = priorityQueue->next;
  67.         }
  68.     }
  69. }
  70.  
  71. void peek() {
  72.     cout << "Pierwszy element: " << priorityQueue->priority << " " << priorityQueue->data << endl;
  73. }
  74.  
  75. int main() {
  76.     int choice, value, priority;
  77.     do {
  78.         cout << endl << "1) Wprowadz element do kolejki" << endl;
  79.         cout << "2) Usun element z kolejki" << endl;
  80.         cout << "3) Wyswietl elementy kolejki" << endl;
  81.         cout << "4) Wyswietl pierwszy element kolejki" << endl;
  82.         cout << "5) Wyjscie" << endl;
  83.         cout << "Wybierz opcje : ";
  84.         cin >> choice;
  85.         switch (choice) {
  86.         case 1:
  87.             cout << "Wprowadz liczbe : ";
  88.             cin >> value;
  89.             cout << "Wprowadz priorytet (mniej znaczy lepiej) : ";
  90.             cin >> priority;
  91.             push(value, priority);
  92.             break;
  93.         case 2:
  94.             pop();
  95.             break;
  96.         case 3:
  97.             show();
  98.             break;
  99.         case 4:
  100.             peek();
  101.             break;
  102.         case 5:
  103.             cout << "Wyjscie" << endl;
  104.             break;
  105.         default:
  106.             cout << "Zly wybor" << endl;
  107.         }
  108.     } while (choice != 5);
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement