Advertisement
avr39-ripe

queueArrayCircular

Dec 16th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template <class T, int size>
  4. class Queue
  5. {
  6.     T queue[size]{};
  7.     int begin;
  8.     int end;
  9.     int count;
  10. public:
  11.     Queue(): begin{0}, end{0}, count{0} {};
  12.     bool empty() {return count == 0;};
  13.     bool full() {return count == size;};
  14.     bool enqueue(const T& el);
  15.     bool dequeue(T& el);
  16.     int length() {return count;};
  17.     void show();
  18. };
  19.  
  20. template <class T, int size>
  21. bool Queue<T,size>::enqueue(const T& el)
  22. {
  23.     if (count < size)
  24.     {
  25.         queue[end] = el;
  26.         ++end %= size;
  27.         ++count;
  28.         return true;
  29.     }
  30.     return false;
  31. }
  32. template <class T, int size>
  33. bool Queue<T,size>::dequeue(T& el)
  34. {
  35.     if (count>0)
  36.     {
  37.         el = queue[begin];
  38.         queue[begin]=0; //HACK!
  39.         ++begin %= size;
  40.         --count;
  41.         return true;
  42.     }
  43.     return false;
  44. }
  45.  
  46. template <class T, int size>
  47. void Queue<T,size>::show()
  48. {
  49.     for (const T& el: queue)
  50.     {
  51.         std::cout << el << ' ';
  52.     }
  53.     std::cout << '\n';
  54. }
  55.  
  56. int main()
  57. {
  58.     Queue<int,10> q;
  59.     q.show();
  60.     std::cout << q.empty() << '\n';
  61.     std::cout << q.full() << '\n';
  62.  
  63.     for (int i{1}; i<9; ++i)
  64.     {
  65.         q.enqueue(i);
  66.         q.show();
  67.     }
  68.  
  69.     for (int i{1}; i<5; ++i)
  70.     {
  71.         int el{0};
  72.         q.dequeue(el);
  73.         std::cout << "El: " << el << '\n';
  74.         q.show();
  75.     }
  76.  
  77.     for (int i{1}; i<9; ++i)
  78.     {
  79.         if ( q.enqueue(i) )
  80.         {
  81.             q.show();
  82.         }
  83.         else
  84.         {
  85.             std::cout << "FULL!\n";
  86.         }
  87.     }
  88.  
  89.     for (int i{1}; i<20; ++i)
  90.     {
  91.         int el{0};
  92.         if (q.dequeue(el))
  93.         {
  94.             std::cout << "El: " << el << '\n';
  95.             q.show();
  96.         }
  97.         else
  98.         {
  99.             std::cout << "EMPTY!\n";
  100.         }
  101.     }
  102.  
  103.     for (int i{1}; i<20; ++i)
  104.     {
  105.         if ( q.enqueue(i) )
  106.         {
  107.             q.show();
  108.         }
  109.         else
  110.         {
  111.             std::cout << "FULL!\n";
  112.         }
  113.     }
  114.  
  115.     for (int i{1}; i<20; ++i)
  116.     {
  117.         int el{0};
  118.         if (q.dequeue(el))
  119.         {
  120.             std::cout << "El: " << el << '\n';
  121.             q.show();
  122.         }
  123.         else
  124.         {
  125.             std::cout << "EMPTY!\n";
  126.         }
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement