Please implement three basic operations of circular queue, which are "push", "pop", and "front".
Note that a circular queue with capacity n can only contain n-1 elements.
You may use the code template below and replace /*TODO*/
section with your code.
Template.cpp
#include <iostream> #include <sstream> class Queue { // Initialize front and rear int rear, front; // Circular Queue int size; int *arr; public: Queue(int s) { front = rear = 0; size = s; arr = new int[s]; } void enQueue(int value); int deQueue(); void showFront(); }; /* Function to create Circular queue */ void Queue::enQueue(int value) { if(/*TODO*/) { std::cout << "Full" << std::endl; return; } /*TODO*/ } // Function to delete element from Circular Queue int Queue::deQueue() { if (/*TODO*/) { return -1; } /*TODO*/ } // Function displaying the elements // of Circular Queue void Queue::showFront() { if (/*TODO*/) { std::cout << "Empty" << std::endl; return; } /*TODO*/ } int main(){ int size; std::string line_string; std::cin >> size; Queue q(size); while(std::getline(std::cin, line_string)){ std::istringstream iss(line_string); std::string instruction; iss >> instruction; if(instruction == "push"){ int number; iss >> number; q.enQueue(number); } else if (instruction == "pop"){ q.deQueue(); } else if (instruction == "front"){ q.showFront(); } } return 0; }
The first line of input contains a 32-bit integer to specify the *capacity* of circular queue.
The capacity of the circular queue is only specified in the first line of the input and occurs only once.
Then, there are a number of circular queue operations (<50000) specified in the subsequent input lines.
push i
: Push an integer i into the circular queue. We do not need to output anything if this operation is successfully carried out. However, if the circular queue is full, output "Full" and do nothing to the queue.
pop
: Pop one element from the circular queue. We do not need to output anything if this operation is successfully carried out.
We do not need to output anything when the circular queue is empty.
front
: Output the front element of the queue. Output "Empty" and do nothing to the queue if the queue is empty.
Note: You should append a newline character(\n
) after each output.