#include<bits/stdc++.h> #define MAX_SIZE 5 using namespace std; class CircularQueue{ public: /** Initialize your data structure here. */ int queue[MAX_SIZE]; int fPtr, rPtr; int qsize; CircularQueue() { fPtr = rPtr = -1, qsize = 0; } bool push(int &val){ if(isFull()){ cout<<"Queue is full.\n"; return false; } if(fPtr == -1){ fPtr = rPtr = 0; } else{ rPtr = (rPtr+1) % MAX_SIZE; } queue[rPtr] = val; ++qsize; return true; } int front(){ return queue[fPtr]; } int rear(){ return queue[rPtr]; } int size(){ return qsize; } bool isEmpty(){ return fPtr == -1 && rPtr == -1; } bool isFull(){ return (rPtr+1) % MAX_SIZE == fPtr; } int pop(){ if(isEmpty()){ cout<<"Queue is empty.\n"; return INT_MAX; } int retVal = queue[fPtr]; if(rPtr == fPtr){ rPtr = fPtr = -1; } else{ fPtr = (fPtr+1) % MAX_SIZE; } --qsize; return retVal; } void print(){ if(isEmpty()){ cout<<"Queue is empty.\n"; return; } for(int i = fPtr, qSize = size(); qSize-- > 0; i = (i+1) % MAX_SIZE){ cout<<queue[i]<<(i ^ rPtr ? "->" : ""); } cout<<"\n"; } }; int main() { int opt, val; CircularQueue cq; cout<<"Operations available on the circular queue:\n"; cout<<"1. Push\n2. Pop\n3. Front\n4. Rear\n" "5. Print from front to rear\n" "6. Is queue empty\n7. Exit\n"; while(true){ cin>>opt; switch(opt){ case 1: cin>>val; cout<<"Element "<<val<<" is "<<(cq.push(val) ? "":"not")<<" pushed into the queue.\n"; break; case 2: val = cq.pop(); if(val == INT_MAX) cout<<"Front element can't be popped from the queue.\n"; else cout<<"Front element "<<val<<" popped from the queue.\n"; break; case 3: cout<<"The front element in the queue is: "<<cq.front()<<"\n"; break; case 4: cout<<"The rear element in the queue is: "<<cq.rear()<<"\n"; break; case 5: cout<<"The elements in the queue from front to rear:\n"; cq.print(); break; case 6: cout<<"Queue is "<<(cq.isEmpty() ? "": "not")<<" empty.\n"; break; case 7: exit(0); } cout<<"==========================================================\n"; } return 0; }
1 2 1 3 1 3 1 5 1 235 5 2 2 2 2 2 5 1 45 1 23 2 2 1 35 1 23 6 3 4 5 7
Operations available on the circular queue: 1. Push 2. Pop 3. Front 4. Rear 5. Print from front to rear 6. Is queue empty 7. Exit Element 2 is pushed into the queue. ========================================================== Element 3 is pushed into the queue. ========================================================== Element 3 is pushed into the queue. ========================================================== Element 5 is pushed into the queue. ========================================================== Element 235 is pushed into the queue. ========================================================== The elements in the queue from front to rear: 2->3->3->5->235 ========================================================== Front element 2 popped from the queue. ========================================================== Front element 3 popped from the queue. ========================================================== Front element 3 popped from the queue. ========================================================== Front element 5 popped from the queue. ========================================================== Front element 235 popped from the queue. ========================================================== The elements in the queue from front to rear: Queue is empty. ========================================================== Element 45 is pushed into the queue. ========================================================== Element 23 is pushed into the queue. ========================================================== Front element 45 popped from the queue. ========================================================== Front element 23 popped from the queue. ========================================================== Element 35 is pushed into the queue. ========================================================== Element 23 is pushed into the queue. ========================================================== Queue is not empty. ========================================================== The front element in the queue is: 35 ========================================================== The rear element in the queue is: 23 ========================================================== The elements in the queue from front to rear: 35->23 ==========================================================