Advertisement
AdamBB

Queue

Jan 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int dane[5];
  6. int ile;
  7. int glowa;
  8. int ogon;
  9.  
  10. //------------------------------------------------------------------------
  11.  
  12. void wyswietl_kolejke()
  13. {
  14.     cout<<endl;
  15.     cout<<"-------------------"<<endl;
  16.     cout<<"ZAWARTOSC KOLEJKI: "<<endl;
  17.     cout<<"-------------------"<<endl;
  18.  
  19.  
  20.     if (ile==0)
  21.     {
  22.         cout<<"pusta";
  23.     }
  24.  
  25.     else
  26.     {
  27.  
  28.         int indeks;
  29.  
  30.         for (int i=0; i<ile; i++)
  31.         {
  32.             indeks=glowa+i;
  33.             if (glowa+i>=5) indeks=glowa+i-5;
  34.             cout<<dane[indeks]<<" ";
  35.         }
  36.  
  37.     }
  38.  
  39.     cout<<endl<<"-------------------"<<endl<<endl;
  40. }
  41.  
  42. //------------------------------------------------------------------------
  43.  
  44. void push()
  45. {
  46.     if (ile>=5)
  47.     {
  48.         cout << "Kolejka pelna!";
  49.     }
  50.     else
  51.     {
  52.         cout<<endl<< "PUSH (jaka liczbe wstawic do kolejki): ";
  53.         cin>>dane[ogon];
  54.         ogon=(ogon+1)%5;
  55.         ile=ile+1;
  56.     }
  57. }
  58.  
  59. //------------------------------------------------------------------------
  60.  
  61. void pop()
  62. {
  63.     if (ile==0)
  64.     {
  65.         cout<<"Kolejka jest pusta!";
  66.     }
  67.     else
  68.     {
  69.         cout<<endl<<"POP - nastapi usuniecie z kolejki liczby: "<<dane[glowa];
  70.         glowa=(glowa+1)%5;
  71.         ile=ile-1;
  72.     }
  73.  
  74.  
  75. }
  76. //------------------------------------------------------------------------
  77.  
  78. void size()
  79. {
  80.     cout<<endl<<"Liczba elementow w kolejce: "<<ile;
  81. }
  82.  
  83. //------------------------------------------------------------------------
  84.  
  85. void empty()
  86. {
  87.  
  88.     if (ile==0) cout<<endl<<"EMPTY (kolejka pusta?) ->  TRUE";
  89.     else cout<<endl<<"EMPTY (kolejka pusta?) ->  FALSE";
  90. }
  91.  
  92. //------------------------------------------------------------------------
  93.  
  94. int main()
  95. {
  96.  
  97.     int wybor;
  98.     ile=0;
  99.     glowa=0;
  100.     ogon=0;
  101.  
  102.     do
  103.     {
  104.  
  105.         wyswietl_kolejke();
  106.  
  107.         cout<<"glowa="<<glowa<<"   ogon="<<ogon<<"   ile="<<ile<<endl<<endl;
  108.  
  109.         cout << "MENU GLOWNE KOLEJKI:"<<endl;
  110.         cout << "------------------------------------------"<<endl;
  111.         cout << "1. PUSH (dodaje element na koniec kolejki) "<<endl;
  112.         cout << "2. POP (usuwa element z poczatku kolejki) "<<endl;
  113.         cout << "3. SIZE (ile elementow w kolejce) "<<endl;
  114.         cout << "4. EMPTY (czy kolejka jest pusta?) "<<endl;
  115.         cout << "5. Koniec programu "<<endl;
  116.         cout << "------------------------------------------"<<endl;
  117.         cout << "Wybor: ";
  118.         cin >> wybor;
  119.  
  120.         switch (wybor)
  121.         {
  122.             case 1:
  123.                 push();
  124.                 break;
  125.  
  126.             case 2:
  127.                 pop();
  128.                 break;
  129.  
  130.             case 3:
  131.                 size();
  132.                 break;
  133.  
  134.             case 4:
  135.                 empty();
  136.                 break;
  137.         }
  138.  
  139.     }
  140.     while (wybor != 5);
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement