From 01b8a7b7e68d724c7e946984eaa11b2061dc00ad Mon Sep 17 00:00:00 2001 From: Stefan Hermeniuc Date: Sun, 30 Sep 2018 23:12:20 +0300 Subject: [PATCH] Circular Queue - C++ --- CPP/data-structures/Circular_Queue.cpp | 181 +++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 CPP/data-structures/Circular_Queue.cpp diff --git a/CPP/data-structures/Circular_Queue.cpp b/CPP/data-structures/Circular_Queue.cpp new file mode 100644 index 0000000..6dbdd0d --- /dev/null +++ b/CPP/data-structures/Circular_Queue.cpp @@ -0,0 +1,181 @@ +/** Circular Queue */ +#define DEFAULT_SIZE 100 + +#include +#include + +template +class Queue { +private: + T *container; + int numElems; + int size; + int head, tail; + +public: + /** Constructor */ + Queue() { + container = new T[DEFAULT_SIZE]; + numElems = 0; + this->size = DEFAULT_SIZE; + head = tail = 0; + } + + /** 2nd Constructor */ + Queue(int size) { + container = new T[size]; + numElems = 0; + this->size = size; + head = tail = 0; + } + + /** Destructor */ + ~Queue() { + delete[] container; + } + + /** Assignment operator */ + Queue &operator=(const Queue ©) { + this->container = new T[copy.size]; + + for (int i = 0; i < copy.size; i++) { + this->container[i] = copy.container[i]; + } + + this->size = copy.size; + this->numElems = copy.numElems; + this->head = copy.head; + this->tail = copy.tail; + + return *this; + } + + /** Copy Constructor */ + Queue(const Queue ©) { + this->container = new T[copy.size]; + + for (int i = 0; i < copy.size; i++) { + this->container[i] = copy.container[i]; + } + + this->size = copy.size; + this->numElems = copy.numElems; + this->head = copy.head; + this->tail = copy.tail; + } + + /** Insert an element into the circular queue */ + void enQueue(T x) { + if (isFull()) { + std::cout << "Can't enqueue, queue is full!\n"; + return; + } + + container[tail] = x; + tail = (tail + 1) % size; + numElems++; + } + + /** Delete an element from the circular queue */ + void deQueue() { + if (isEmpty()) { + std::cout << "Can't dequeue, queue is empty!\n"; + return; + } + + numElems--; + head = (head + 1) % size; + } + + /** Get the front item from the queue */ + int front() { + if (isEmpty()) { + return INT_MIN; + } + + return container[head]; + } + + /** Checks whether the circular queue is empty or not */ + bool isEmpty() { + return !numElems; + } + + /** Checks whether the circular queue is full or not */ + bool isFull() { + return numElems == size; + } + + /** Overloading << operator */ + template + friend std::ostream& operator<< (std::ostream& stream, Queue &obj); + +}; + +template +std::ostream& operator<< (std::ostream& stream, Queue &obj) { + for (int i = 0; i < obj.numElems; i++) { + T front = obj.front(); + stream << front << " "; + obj.deQueue(); + obj.enQueue(front); + } + stream << std::endl; + + return stream; +} + +int main() { + /** Checking functionality of Circular Queue */ + Queue q(4); + + /** Checking isEmpty */ + if (q.isEmpty()) { + std::cout << "Queue is empty\n"; + } + + /** Checking deQueue for empty queue */ + q.deQueue(); + + /** Checking enQueue */ + q.enQueue(2); + q.enQueue(3); + q.enQueue(-4); + q.enQueue(23); + + /** Checking operator<< overloading */ + std::cout << q; + + /** Checking enQueue for full queue */ + q.enQueue(2); + + /** Checking isFull */ + if (q.isFull()) { + std::cout << "Queue is full\n"; + } + + /** Checking deQueue */ + q.deQueue(); + + std::cout << q; + + /** Checking front */ + std::cout << q.front() << std::endl; + + /** Checking Copy Constructor */ + Queue p = q; + std::cout << p; + + /** Checking operator= overloading */ + Queue r; + + r = p; + + std::cout << r; + + q.enQueue(2); + r = q; + std::cout << r; + + return 0; +}