Question: #include class Node { public: int value; Node * next; Node ( int value = 0 ) { this - > value = value; this
#include
class Node
public:
int value;
Node next;
Nodeint value
thisvalue value;
thisnext nullptr;
;
class Queue
private:
Node frontnode;
Node rearnode;
public:
Queue
thisfrontnode nullptr;
thisrearnode nullptr;
bool isEmpty
return thisfrontnode nullptr;
void enqueueint value
Node newnode new Nodevalue;
if thisrearnode nullptr
If the queue is empty, both front and rear nodes will point to the new node
thisfrontnode thisrearnode newnode;
else
Attach the new node to the end of the queue and update the rear node
thisrearnodenext newnode;
thisrearnode newnode;
int dequeue
if thisisEmpty
throw std::outofrangedequeue from empty queue";
int value thisfrontnodevalue;
thisfrontnode thisfrontnodenext;
if thisfrontnode nullptr
If the queue becomes empty after dequeue, reset the rearnode as well
thisrearnode nullptr;
return value;
int front
if thisisEmpty
throw std::outofrangefront from empty queue";
return thisfrontnodevalue;
;
int main
Queue queue;
std::cout std::boolalpha queue.isEmpty std::endl; Output: true
queue.enqueue;
queue.enqueue;
queue.enqueue;
std::cout std::boolalpha queue.isEmpty std::endl; Output: false
std::cout queue.front std::endl; Output:
std::cout queue.dequeue std::endl; Output:
std::cout queue.front std::endl; Output:
std::cout queue.dequeue std::endl; Output:
std::cout queue.front std::endl; Output:
std::cout queue.dequeue std::endl; Output:
std::cout std::boolalpha queue.isEmpty std::endl; Output: true
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
