Advertisement
JoshuaNHardison

queue.h

Oct 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3. #include<vector>
  4. using namespace std;
  5.  
  6. template <class X>
  7. class queue
  8. {
  9.     vector<X> arr;     
  10.     int capacity;  
  11.     int front;     
  12.     int rear;      
  13.     int count;
  14.    
  15. public:
  16.     queue(int size = SIZE);     // constructor
  17.  
  18.     void dequeue();
  19.     void enqueue(X x);
  20.     X Display();
  21.     bool isEmpty();
  22. };
  23.  
  24. template <class X>
  25. queue<X>::queue(int size)
  26. {
  27.     arr = vector<X>(size);
  28.     capacity = size;
  29.     front = 0;
  30.     rear = -1;
  31.     count = 0;
  32. }
  33.  
  34.  
  35. template <class X>
  36. void queue<X>::dequeue()
  37. {
  38.     if (isEmpty())
  39.         cout << "\nQueue is empty\n";
  40.  
  41.     cout << "Removing " << arr[front] << '\n';
  42.  
  43.     front = (front + 1) % capacity;
  44.     count--;
  45. }
  46.  
  47. template <class X>
  48. void queue<X>::enqueue(X item)
  49. {
  50.     if (isFull())
  51.     {
  52.         cout << "\nQueue is full\n";
  53.     }
  54.  
  55.     cout << "Inserting " << item << '\n';
  56.  
  57.     rear = (rear + 1) % capacity;
  58.     arr[rear] = item;
  59.     count++;
  60. }
  61.  
  62. // Utility function to return front element in the queue
  63. template <class X>
  64. X queue<X>::Display()
  65. {
  66.     if (isEmpty())
  67.         cout << "\nQueue is empty." << endl;
  68.     else
  69.         return arr;
  70. }
  71.  
  72.  
  73. template <class X>
  74. bool queue<X>::isEmpty()
  75. {
  76.     return (size() == 0);
  77. }
  78. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement