Question: Need help with printing a queue in reverse. I have most of the code written, but I cannot for the life of me figure out
Need help with printing a queue in reverse. I have most of the code written, but I cannot for the life of me figure out how to print this queue in the asked manner! My instructor said hed prefer I not use a stack, but instead change the order of the linked list, but I will accept any printing method since Im out of ideas at this point. Also, when complete, please fill in this printing option at "case 5." Thanks and as always, I will rate immediately following an answer, and if the answer doesnt work, I will first contact the poster before leaving anything negative.
For this assignment you are required to either A) modify your linked list code to become a queue or B) modify the provided stack code to become a queue. Your program should implement a C++ queue class with the following member functions:
-Void enq(int)
-Void deq()
-Void front()
-Bool isEmpty()
-Void printq()
Your program will take in a command file called cmd.txt which will instruct your program what operations to run
File Format
Commands
-1 enq
-2 deq
-3 front
-4 isEmpty
-5 printq
Example File
1 1
5
1 2
5
1 3
5
1 4
5
3
2
5
2
5
3
Expectations
You should not use any already implemented code such as a library for your linked list
Your code should be well formatted with proper spacing and proper naming
Your code should have well named variables. No as bs or cs as names unless it is for something like a loop counter
Your code should have the same output formatting you see below
Example Output

#include
class queue{ public: queue(); void enq(int); void deq(); int front(); bool isEmpty(); void printq(); //print queue in reverse call private: struct node{ int val; node* next; }; node* topPtr; };
queue::queue() { topPtr = NULL; }
void queue::enq(int x) { if (topPtr == NULL) { topPtr = new node; topPtr->val = x; topPtr->next = NULL; } else { node* tmp; tmp = topPtr; while (tmp->next != NULL) { tmp = tmp->next; } tmp->next = new node; tmp = tmp->next; tmp->next = NULL; tmp->val = x; } }
void queue::deq() { node* rem = topPtr; topPtr = topPtr->next; delete(rem); }
int queue::front() { return topPtr->val; }
bool queue::isEmpty() { if (topPtr == NULL) return true; else return false; }
void queue::printq() {
}
int main() { ifstream cmds("cmd.txt"); int cmd, op; queue s; bool isEmpty;
while (cmds >> cmd) { switch (cmd) { case 1: cmds >> op; s.enq(op); break; case 2: s.deq(); break; case 3: cout
} } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
