Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct node {
  7.     int priority;
  8.     string info;
  9.     struct node *link;
  10. };
  11.  
  12. class Priority_Queue {
  13. private:
  14.     node *front;
  15. public:
  16.     Priority_Queue() {
  17.         front = NULL;
  18.     }
  19.     void insert(string item, int priority) {
  20.         node *tmp, *q;
  21.         tmp = new node;
  22.         (*tmp).info = item;
  23.         (*tmp).priority = priority;
  24.         if (front == NULL || priority < (*front).priority) {
  25.             (*tmp).link = front;
  26.             front = tmp;
  27.         }
  28.         else {
  29.             q = front;
  30.             while ((*q).link != NULL && (*q).priority <= priority) {
  31.                 q = (*q).link;
  32.             }
  33.             (*tmp).link = (*q).link;
  34.             (*q).link = tmp;
  35.         }
  36.     }
  37.  
  38.     void del() {
  39.         node *tmp;
  40.         if (front == NULL) {
  41.             cout << "Kolejka jest pusta\n";
  42.         }
  43.         else {
  44.             tmp = front;
  45.             cout << "Usunieta rzecz to: " << (*tmp).info << "\n";
  46.             front = (*front).link;
  47.             free(tmp);
  48.         }
  49.     }
  50.  
  51.     void display() {
  52.         node *ptr;
  53.         ptr = front;
  54.         if (front == NULL) {
  55.             cout << "Kolejka jest pusta\n";
  56.         }
  57.         else {
  58.             cout << "Twoja kolejka to: \n";
  59.             cout << "Priorytet      rzecz\n";
  60.             while (ptr != NULL) {
  61.                 cout << (*ptr).priority << "          " << (*ptr).info << endl;
  62.                 ptr = (*ptr).link;
  63.             }
  64.         }
  65.     }
  66. };
  67.  
  68. int main() {
  69.     int choice, priority;
  70.     string item;
  71.     Priority_Queue pq;
  72.     do {
  73.         cout << "1. Wstaw\n"
  74.             << "2. Usun\n"
  75.             << "3. Wyswietl\n"
  76.             << "4. Wyjscie\n"
  77.             << "Co chcesz zrobic?\n";
  78.         cin >> choice;
  79.         switch (choice) {
  80.         case 1:
  81.             cout << "Wprowadz rezcz, ktora ma byc dodana do kolejki: \n";
  82.             cin.ignore();
  83.             getline(cin, item);
  84.             cout << "Wprowadz priorytet:\n";
  85.             cin >> priority;
  86.             pq.insert(item, priority);
  87.             break;
  88.         case 2:
  89.             pq.del();
  90.             break;
  91.         case 3:
  92.             pq.display();
  93.             break;
  94.         case 4:
  95.             return 0;
  96.             break;
  97.         default:
  98.             cout << "Bledny wybor! Sprobuj ponownie!";
  99.             break;
  100.         }
  101.     } while (choice != 4);
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement