Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this assignment, you need to design a queue that supports push and pop operations in the front, middle, and back. We already gave you
In this assignment, you need to design a queue that supports push and pop operations in the front, middle, and back. We already gave you a draft FrontMiddleBackQueue.cpp code to start with. You need to use the linked List implementation of queues. You need to complete the undefined functions in this file and submit the complete code. Specifically, you need to Implement theFrontMiddleBack class and its methods: FrontMiddleBack Initializes the queue. void pushFrontint val Adds val to the front of the queue. void pushMiddleint val Adds val to the middle of the queue. void pushBackint val Adds val to the back of the queue. int popFront Removes the front element of the queue and returns it If the queue is empty, return int popMiddle Removes the middle element of the queue and returns it If the queue is empty, return int popBack Removes the back element of the queue and returns it If the queue is empty, return void reverse Reverse the queue Note: For pushMiddle and popMiddle functions, when there are two middle position choices, the operation is performed on the frontmost middle position choice. For example: Pushing into the middle of results in Poping the middle of results Examples: current queue q: qpushMiddle; qpushMiddle; qpopFront; return and remove qreverse; qpopMiddle; return and remove qpopMiddle; return and remove qpopBack; return and remove qpopFront; return The queue is emptyin How to test: In FrontMiddleBackQueue.cpp you can find a main function including some test cases. After completing the code, you need to get the following output by running that main function. FrontMiddleBackQueue.cpp file #include using namespace std; class Node public: int val; Node next; Nodeint x val x; next NULL; ; class FrontMiddleBackQueue public: Node frontback; int size; FrontMiddleBackQueue void pushFrontint val void pushMiddleint val void pushBackint val int popFront int popMiddle int popBack void reverse ; int main FrontMiddleBackQueue q; Test case : Pushing elements to the front, middle, and back of the queue qpushFront; qpushMiddle; qpushBack; qpushMiddle; qpushMiddle; qpushFront; Expected output: Node temp qfront; whiletemp cout tempval ; temp tempnext; cout endl; Test case : Popping elements from the front, middle, and back of the queue cout qpopFront endl; Expected output: cout qpopMiddle endl; Expected output: cout qpopBack endl; Expected output: cout qpopMiddle endl; Expected output: Expected output after popping: temp qfront; whiletemp cout tempval ; temp tempnext; cout endl; Edge case: Popping elements from an empty queue FrontMiddleBackQueue q; cout qpopFront endl; Expected output: cout qpopMiddle endl; Expected output: cout qpopBack endl; Expected output: Edge case: Popping elements from a queue of size FrontMiddleBackQueue q; qpushFront; cout qpopFront endl; Expected output: qpushFront; cout qpopMiddle endl; Expected output: qpushFront; cout qpopBack endl; Expected output: FrontMiddleBackQueue q; Test case : reversing the queue qpushFront; qpushFront; qpushFront; qpushFront; qreverse; temp qfront; whiletemp cout tempval ; temp tempnext; cout endl; return ;
In this assignment, you need to design a queue that supports push and pop operations in the
front, middle, and back. We already gave you a draft FrontMiddleBackQueue.cpp code to start
with. You need to use the linked List implementation of queues. You need to complete the
undefined functions in this file and submit the complete code.
Specifically, you need to Implement theFrontMiddleBack class and its methods:
FrontMiddleBack Initializes the queue.
void pushFrontint val Adds val to the front of the queue.
void pushMiddleint val Adds val to the middle of the queue.
void pushBackint val Adds val to the back of the queue.
int popFront Removes the front element of the queue and returns it If the queue is
empty, return
int popMiddle Removes the middle element of the queue and returns it If the queue is
empty, return
int popBack Removes the back element of the queue and returns it If the queue is
empty, return
void reverse Reverse the queue
Note: For pushMiddle and popMiddle functions, when there are two middle position choices, the
operation is performed on the frontmost middle position choice. For example:
Pushing into the middle of results in
Poping the middle of results Examples:
current queue q:
qpushMiddle;
qpushMiddle;
qpopFront; return and remove
qreverse;
qpopMiddle; return and remove
qpopMiddle; return and remove
qpopBack; return and remove
qpopFront; return The queue is
emptyin
How to test:
In FrontMiddleBackQueue.cpp you can find a main function including some test cases. After
completing the code, you need to get the following output by running that main function.
FrontMiddleBackQueue.cpp file
#include
using namespace std;
class Node
public:
int val;
Node next;
Nodeint x
val x;
next NULL;
;
class FrontMiddleBackQueue
public:
Node frontback;
int size;
FrontMiddleBackQueue
void pushFrontint val
void pushMiddleint val
void pushBackint val
int popFront
int popMiddle
int popBack
void reverse
;
int main
FrontMiddleBackQueue q;
Test case : Pushing elements to the front, middle, and back of the queue
qpushFront;
qpushMiddle;
qpushBack;
qpushMiddle;
qpushMiddle;
qpushFront;
Expected output:
Node temp qfront;
whiletemp
cout tempval ;
temp tempnext;
cout endl;
Test case : Popping elements from the front, middle, and back of the queue
cout qpopFront endl; Expected output:
cout qpopMiddle endl; Expected output:
cout qpopBack endl; Expected output:
cout qpopMiddle endl; Expected output:
Expected output after popping:
temp qfront;
whiletemp
cout tempval ;
temp tempnext;
cout endl;
Edge case: Popping elements from an empty queue
FrontMiddleBackQueue q;
cout qpopFront endl; Expected output:
cout qpopMiddle endl; Expected output:
cout qpopBack endl; Expected output:
Edge case: Popping elements from a queue of size
FrontMiddleBackQueue q;
qpushFront;
cout qpopFront endl; Expected output:
qpushFront;
cout qpopMiddle endl; Expected output:
qpushFront;
cout qpopBack endl; Expected output:
FrontMiddleBackQueue q;
Test case : reversing the queue
qpushFront;
qpushFront;
qpushFront;
qpushFront;
qreverse;
temp qfront;
whiletemp
cout tempval ;
temp tempnext;
cout endl;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started