Array Queue Implementation

#include <iostream> using namespace std; class ArrayQueue { private: int capacity; int front; int rear; int *storage; public: ArrayQueue(int capacity) { storage = new int[capacity]; this->capacity = capacity; front = -1; rear = -1; } ~ArrayQueue() { delete[] storage; } void enQueue(int value) { if(isFull()) throw string("Queue is full"); if (front == -1) front = 0; rear++; storage[rear % capacity] = value; } void deQueue(int &valueOut) { if (isEmpty()) throw string("Queue is empty"); valueOut = storage[front % capacity]; front++; } int getFront() { if (isEmpty()) throw string("Queue is empty"); return storage[front]; } int getRear() { if (isEmpty()) throw string("Queue is empty"); return storage[rear]; } bool isEmpty() { return (front == rear); } bool isFull() { return (rear - front == capacity); } int getSize() { return rear - front; } void print2Console() { if (rear > front) { for (int i = front; i <= rear; i++) { cout << storage[i] << " "; } cout << endl; } } }; int main() { cout << "Array Queue" << endl; ArrayQueue *myQueue = new ArrayQueue(10); int val; myQueue->enQueue(7); myQueue->enQueue(9); myQueue->enQueue(10); myQueue->enQueue(8); myQueue->print2Console(); myQueue->deQueue(val); myQueue->print2Console(); delete myQueue; return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.