Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Towards the end of the lecture, we discuss about the Queue implementation and the code is literally given to us there. I want you

C++

Towards the end of the lecture, we discuss about the Queue implementation and the code is literally given to us there. I want you to implement the queue properly and have several operations:

Enqueue some numbers, then dequeue them, then enqueue some others as well. In the end, keep on dequeueing until the queue is empty. You should be printing out every operation of enqueue or dequeue:

Ex:

Enqueueing 5... Dequeueing 5... Enqueueing 11.... Enqueueing 17.... Dequeueing 11.... Dequeueing 17.... Queue is empty.

Lecture slides are below.

Note from professor:

Hi everyone, I got questions from several students about the Queue implementation you are supposed to program from the slides. Now, there is an ItemType.h header which it is not discussing in depth about.

If you realized, it is merely the type of the queue. So if we had, say, Employee class to create a queue of Employees, we would include "employee.h" and create a queue of employees. So ItemType in this example is used for a 'generic' or template type.

For you, I tasked you to create an integer queue, so if you have issues, merely ignore ItemType and convert all 'ItemType's in the Queue implementation into 'int' so that you create an integer queue.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Class specification for Queue // Header file for Queue ADT. class FullQueue {}; class EmptyQueue {}; struct Node Type; #include "ItemType.h" class QueType { public: QueType(); // Class constructor. // Because there is a default constructor, the precondition // that the queue has been initialized is omitted. QueType (int max); // Parameterized class constructor. - QueType(); // Class destructor. void MakeEmpty(); // Function: Initializes the queue to an empty state. // Post: Queue is empty. Class specification for Queue bool IsEmpty() const; // Function: Determines whether the queue is empty. // Post: Function value = (queue is empty) bool IsFull() const; // Function: Determines whether the queue is full. // Post: Function value = (queue is full) void Enqueue (ItemType newItem); // Function: Adds newItem to the rear of the queue. // Post: If (queue is full) FullQueue exception is thrown // else newItem is at rear of queue. void Dequeue (ItemType & item); // Function: Removes front item from the queue and returns it in item. // Post: if (queue is empty) EmptyQueue exception is thrown // and item is undefined else front element has been removed from queue and // item is a copy of removed element. private: Node Type* front; Node Type* rear; }; struct Node Type { ItemType info; Node Type* next; }; Queue Implementation QueType: : QueType() // Class constructor. // Post: front and rear are set to NULL. { front = NULL; rear = NULL; } void QueType: : MakeEmpty() // Post: Queue is empty; all elements have been deallocated. { Node Type* tempPtr; while (front != NULL) { tempPtr = front; front = front->next; delete tempPtr; } rear = NULL; } // Class destructor. QueType::-QueType() { MakeEmpty(); } Queue Implementation (Cont.) bool QueType:: IsFull() const Node Type: location; try location - new Node Type; delete location; return false; . catch (bad_alloc exception) return true; } bool QueType: : 18 Empty() const return (front == NULL); void QueType: : Enqueue (ItemType newItem.) 14 (ISFull()) throw FullQueue (); else NodeTyperewNode; newNode = new Node Type; newNode->info - newItem; newNode->next = NULL; if (rear -- NULL) frant - newNode; else rear->next = newNode; rear = newNode; Queue Implementation (Cont.) void QueType: : Dequeue (ItemType & item) // Removes front item from the queue and returns it in item. // Pre: Queue has been initialized and is not empty. // Post: If (queue is not empty) the front of the queue has been removed and a copy returned in item; // othersiwe a EmptyQueue exception has been thrown. { if (IsEmpty()) throw EmptyQueue (); else { NodeType* tempPtr; tempPtr = front; item = front->info; front = front->next; if (front == NULL) rear = NULL; delete tempPtr; } Sample Queue Code / Test driver #include #include "QueType.h" using namespace std; int main() { ifstream inFile; ofstream outFile; string inFileName; string out.FileName; string outputLabel; string command; // file containing operations // file containing output // input file external name // output file external name // operation to be executed ItemType item; Que Type queue; int numCommands; // Prompt for file names, read file names, and prepare files cout > inFileName; infile.open(inFileName.c_str()); cout > outFileName; outFile.open(outFileName.c_str()); cout > outputLabel; out File > command; Sample Queue Code mumCommands - 0; while (command !- "Quit") try 1t command -- "Enqueue") in File >> Stem queue. Enqueue (item); out File > command; };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions