Question: I need to create an InQueue.cpp file for the IntQueue class using linked list pointers. I need an .cpp file that will give me the
I need to create an InQueue.cpp file for the IntQueue class using linked list pointers. I need an .cpp file that will give me the output down below.
IntQueue.h
#pragma once #ifndef QueMain_H #define QueMain_H
class IntQueue { private: struct QueueNode { int data; // data value QueueNode* next; // Ptr to next node };
std::string hex(void* val); // converts pointer to hex string; used by print and others QueueNode* frontNode;// Ptr to first node QueueNode* backNode; // Ptr to last node public: IntQueue() { // Constructor frontNode = nullptr; backNode = nullptr; } ~IntQueue(); // Destructor void enqueue(int val); void dequeue(int& val); void front(int& val); void back(int& val); void print(); }; // (end class IntQueue
#endif /* QueMain_H */
QueMain.cpp
#include #include #include #include #include
#include "IntQueue.h"
using namespace std;
void outputHelpMessage(); // Displays descriptions of the menu commands int inputInteger(string promptText); // Prompt the user for an integer void testAllQueue(); // Tests the queue member functions
int main() { atexit([] {system("pause"); });
IntQueue queue;
char command; // command input by user int dataValue;
while (true) {
cout << " Command: "; cin >> command;
cout << fixed << setprecision(2);
switch (command) {
case 'e': dataValue = inputInteger("Enter number to enqueue: "); queue.enqueue(dataValue); break; case 'd': queue.dequeue(dataValue); cout << "Dequeue " << dataValue << endl; break; case 'f': queue.front(dataValue); cout << "Front " << dataValue << endl; break; case 'b': queue.back(dataValue); cout << "Back " << dataValue << endl; break; case 'p': queue.print(); break; case 'h': outputHelpMessage(); break; case 'q': return EXIT_SUCCESS; break; case 't': testAllQueue(); break; default: cout << "Invalid command: " << command << endl; } // end switch } // end while
return 0; } // (end function 'main')
void outputHelpMessage() { // Help text. cout << "Supported commands: " << " e enqueue a value into the queue. " << " d dequeue a value from the queue. " << " f see the value at the front of the queue. " << " b see the value at the back of the queue. " << " p PRINT the list contents. " << " h print this help text. " << " q quit (end the program). " << " t test queue scenarios. " << endl;
} // (end function 'outputHelpMessage')
int inputInteger(string promptText) { int val; int isFail;
do { cout << promptText; cin >> val; isFail = cin.fail() || cin.peek() != ' '; cin.clear(); cin.ignore(numeric_limits::max(), ' '); if (isFail) { cout << "Invalid integer input. Try again. "; } } while (isFail); // Loop until successful
return val; } // end function inputInteger
void testAllQueue() {
// Create an instance of IntQueue IntQueue * queue = new IntQueue; // dynamic int val;
cout << " Test all queue scenarios. See code for expected output. ";
queue->print(); // print empty queue->dequeue(val); cout << "Dequeue value: " << val << endl; // dequeue empty queue->front(val); cout << "Front value: " << val << endl; // front empty queue->back(val); cout << "Back value: " << val << endl;; // back empty cout << "Enqueueing a 12. "; queue->enqueue(12); // enqueue empty
queue->print(); // print not empty, front->12<-back queue->front(val); cout << "Front value: " << val << endl;; // front not empty queue->back(val); cout << "Back value: " << val << endl;; // back not empty cout << "Enqueueing a 22. "; queue->enqueue(22); // enqueue not empty, front->12->22<-back queue->print(); // print not empty queue->front(val); cout << "Front value: " << val << endl;; // front not empty queue->back(val); cout << "Back value: " << val << endl;; // back not empty
cout << "Enqueueing a 6. "; queue->enqueue(6); // enqueue not empty queue->print(); // print not empty, front->12->22->6<-back queue->front(val); cout << "Front value: " << val << endl;; // front not empty queue->back(val); cout << "Back value: " << val << endl;; // back not empty
queue->dequeue(val); cout << "Dequeue value: " << val << endl; // dequeue more than one queue->print(); // print not empty, front->22->6<-back queue->front(val); cout << "Front value: " << val << endl;; // front not empty queue->back(val); cout << "Back value: " << val << endl;; // back not empty
queue->dequeue(val); cout << "Dequeue value: " << val << endl; // dequeue more than one queue->print(); // print not empty, front->6<-back queue->front(val); cout << "Front value: " << val << endl;; // front not empty queue->back(val); cout << "Back value: " << val << endl;; // back not empty
queue->dequeue(val); cout << "Dequeue value: " << val << endl; // dequeue one queue->print(); // print empty queue->front(val); cout << "Front value: " << val << endl;; // front empty queue->back(val); cout << "Back value: " << val << endl;; // back empty
// Enqueue some values so we can test destructor. cout << "Enqueueing a 5. "; queue->enqueue(5); // enqueue empty cout << "Enqueueing a 10. "; queue->enqueue(10); // enqueue not empty cout << "Enqueueing a 15. "; queue->enqueue(15); // enqueue not empty queue->print(); // print not empty, front->5->10->15<-back
cout << "Destroying queue." << endl; delete queue;
cout << "That's all folks... "; return; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
