Facebook
From Scanty Zebra, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 126
  1.  
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8.  
  9.  // element Declaration
  10.  
  11. struct element
  12. {
  13.         int priorytet;
  14.         int data;
  15.         struct element *link;
  16. };
  17.  
  18.  // Class Priority Queue
  19.  
  20. class Kolejka_priorytetowa
  21. {
  22.     private:
  23.         element *front;
  24.     public:
  25.         Kolejka_priorytetowa()
  26.         {
  27.             front = NULL;
  28.         }
  29.  
  30.         //Insert into Priority Queue
  31.  
  32.         void Dodawanie(int wart, int priorytet)
  33.         {
  34.             element *obecny, *q;
  35.             obecny = new element;
  36.             obecny->data = wart;
  37.             obecny->priorytet = priorytet;
  38.             if (front == NULL || priorytet < front->priorytet)
  39.             {
  40.                 obecny->link = front;
  41.                 front = obecny;
  42.             }
  43.             else
  44.             {
  45.                 q = front;
  46.                 while (q->link != NULL && q->link->priorytet <= priorytet)
  47.                     q=q->link;
  48.                 obecny->link = q->link;
  49.                 q->link = obecny;
  50.             }
  51.         }
  52.  
  53.         //Delete from Priority Queue
  54.  
  55.         void Usuwanie()
  56.         {
  57.             element *obecny;
  58.             if(front == NULL)
  59.                 cout<<"Kolejka jest pusta!\n";
  60.             else
  61.             {
  62.                 obecny = front;
  63.                 cout<<"Usunieto element: "<<obecny->data<<endl;
  64.                 front = front->link;
  65.                 free(obecny);
  66.             }
  67.         }
  68.  
  69.          // Print Priority Queue
  70.  
  71.         void Wyswietlanie()
  72.         {
  73.             element *wsk;
  74.             wsk = front;
  75.             if (front == NULL)
  76.                 cout<<"Kolejka jest pusta!\n";
  77.             else
  78.             {   cout<<"Kolejka :\n";
  79.                 cout<<"Priorytet       Element\n";
  80.                 while(wsk != NULL)
  81.                 {
  82.                     cout<<wsk->priorytet<<"                 "<<wsk->data<<endl;
  83.                     wsk = wsk->link;
  84.                 }
  85.             }
  86.         }
  87. };
  88. int main()
  89. {
  90.     int wyb, wart, priorytet;
  91.     Kolejka_priorytetowa pq;
  92.     do
  93.     {
  94.         cout<<"\n1.Dodawanie elementu 2.Usuwanie elementu 3.Wyswietlanie elementu 4.Wyjscie\n";
  95.         cin>>wyb;
  96.         switch(wyb)
  97.         {
  98.         case 1:
  99.             cout<<"Podaj wartosc : ";
  100.             cin>>wart;
  101.             cout<<"Dodaj priorytet : ";
  102.             cin>>priorytet;
  103.             pq.Dodawanie(wart, priorytet);
  104.             break;
  105.         case 2:
  106.             pq.Usuwanie();
  107.             break;
  108.         case 3:
  109.             pq.Wyswietlanie();
  110.             break;
  111.         case 4:
  112.             break;
  113.         default :
  114.             cout<<"Zly wybor\n";
  115.         }
  116.     }
  117.     while(wyb != 4);
  118.     return 0;
  119. }