queue example for dsa prep
chamanEiqbal
c_cpp
10 months ago
1.7 kB
6
Indexable
Never
#include <iostream> using namespace std; const int MAXSIZE = 10; class Queue { private: int front; int rear; int arr[MAXSIZE]; bool isFull() {return (rear+1 == MAXSIZE);} bool isEmpty(){return (front == -1) && (rear == -1);} public: Queue() { front = -1; rear = -1; } void enQueue(int val) { if(isFull()) { cerr << "ERROR: Queue is Full!" << endl; } else { if(isEmpty()) { front = 0; } rear++; arr[rear] = val; cout << val << " has been sucessfully enqueued at " << rear << " index!" << endl; } } void deQueue() { if(isEmpty()) { cerr << "ERROR: Queue is empty!" << endl; } else { if(front == rear) { cout << arr[front] << " is being dequeued." << endl; front = -1; rear = -1; } else { cout << arr[front] << " is being dequeued." << endl; front = front + 1; } } } void peek() { if(isEmpty()) { cerr << "ERROR: Cannot peek something that is empty!" << endl; } else { cout << "Front element of the queue is " << endl; cout << arr[front] << endl; cout << "At the index: " << endl; cout << front << endl; } } }; int main() { Queue *queue = new Queue(); queue->enQueue(1); queue->deQueue(); queue->deQueue(); return 0; }