Question: Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include #include #include #include QueueADT.h using namespace std; // Definition of the node
Data Structures using C++
Consider the following class
#ifndef LINKEDQUEUETYPE_H
#define LINKEDQUEUETYPE_H
#include
#include
#include
#include "QueueADT.h"
using namespace std;
// Definition of the node
template
struct NodeType
{
ItemType info;
NodeType
};
template
class LinkedQueueType: public QueueADT
{
public:
// Constructor
LinkedQueueType();
// Default constructor.
// Post: An empty queue has been created. queueFront = NULL;
// queueBack = NULL;
LinkedQueueType(const LinkedQueueType
// Copy constructor for deep copy of the linked implementation
// of queues.
// Destructor
~LinkedQueueType();
// Delete all the queue items.
// Post: All the items of the queue are removed.
// Action responsibilities
void resetQueue();
// Reset the queue to an empty queue.
// Post: Queue is empty. queueFront = NULL; queueBack = NULL;
void add(const ItemType& newItem);
// Function to add newItem to the queue.
// Pre: The queue exists and is not full.
// Post: The queue is changed and newItem is added to the
// back of the queue.
void remove();
// Function to remove the first element of the queue.
// Pre: The queue exists and is not empty.
// Post: The queue is changed and the first (i.e., front) item is
// removed from the queue.
// Knowledge responsibilities
bool isEmpty() const;
// Post: Returns true if queue is empty; false otherwise.
bool isFull() const;
// Post: Returns true if there is room for another item;
// false otherwise.
ItemType front() const;
// Function to return the first element of the queue.
// Pre: The queue exists and is not empty.
// Post: If the queue is empty, the program terminates; otherwise,
// the first element of the queue is returned.
ItemType back() const;
// Function to return the last element of the queue.
// Pre: The queue exists and is not empty.
// Post: If the queue is empty, the program terminates; otherwise,
// the last element of the queue is returned.
// Operator overloading
const LinkedQueueType
(const LinkedQueueType
// Overload the assignment operator.
protected:
NodeType
// (pointing to first node)
NodeType
// (pointing to last node)
private:
void copyQueue(const LinkedQueueType
// Function to make a copy of otherQueue.
// A deep copy of otherQueue is created and assigned to this queue.
};
//**************************************************************
template
LinkedQueueType
{
queueFront = NULL;
queueBack = NULL;
}
//**************************************************************
template
LinkedQueueType
(const LinkedQueueType
{
queueFront = NULL;
queueBack = NULL;
copyQueue(otherQueue);
}
//**************************************************************
template
LinkedQueueType
{
resetQueue();
}
//**************************************************************
template
void LinkedQueueType
{
NodeType
while ( queueFront != NULL ) // while there are items in queue
{
temp = queueFront; // set temp to point to the current node
queueFront = queueFront->next; // advance queueFront to the next
// node
delete temp; // deallocate memeory occupied by temp
}
queueBack = NULL;
}
//**************************************************************
template
void LinkedQueueType
{
NodeType
try
{
newNode = new NodeType
newNode->info = newItem; // store newItem in the node
newNode->next = NULL;
if (queueFront == NULL) // if initially the queue is empty
{
queueFront = newNode;
queueBack = newNode;
}
else
{
queueBack->next = newNode;
queueBack= queueBack->next;
}
}
catch(bad_alloc)
{
cout << "ERROR: Cannot allocate memory! ";
exit(EXIT_FAILURE);
}
}
//**************************************************************
template
void LinkedQueueType
{
NodeType
if ( !isEmpty() )
{
temp = queueFront; // set temp to point to the first node
queueFront = queueFront->next; // advance queueFront to the
// next node
delete temp; // delete the first node
if ( queueFront == NULL ) // if after deletion the queue is empty
queueBack = NULL; // set queueBack to NULL
}
else
cout << "Cannot remove from an empty queue." << endl;
}
//**************************************************************
template
bool LinkedQueueType
{
return ( queueFront == NULL );
}
//**************************************************************
template
bool LinkedQueueType
{
NodeType
// is more room
try
{
newNode = new NodeType
delete newNode;
return false; //more room available, not full
}// end try
catch (bad_alloc)
{
return true; // no more room available
}
}
//**************************************************************
template
ItemType LinkedQueueType
{
if ( !isEmpty() )
return queueFront->info; // return the first item
else // if queue is empty, terminate the program
{
cout << "ERROR: Cannot return first item from an empty queue! ";
exit(EXIT_FAILURE);
}
}
//**************************************************************
template
ItemType LinkedQueueType
{
if ( !isEmpty() )
return queueBack->info; // return the last item
else // if queue is empty, terminate the program
{
cout << "ERROR: Cannot return last item from an empty queue! ";
exit(EXIT_FAILURE);
}
}
//**************************************************************
template
const LinkedQueueType
(const LinkedQueueType
{
if ( this != &otherQueue ) // avoid self-copying
copyQueue(otherQueue);
return *this;
}
//**************************************************************
template
void LinkedQueueType
(const LinkedQueueType
{
NodeType
if ( queueFront != NULL ) // if queue is nonempty, make it empty
resetQueue();
if ( otherQueue.queueFront != NULL ) // if otherQueue is nonempty
{
try
{
current = otherQueue.queueFront; // set current to point to
// the queue to be copied
// copy the queueFront item of the queue
queueFront = new NodeType
queueFront->info = current->info; // copy the info
queueFront->next = NULL; // set next to NULL
queueBack = queueFront; // set queueBack to point to
// the node
current = current->next; // set current to point
// to the next node
// copy the remaining of the queue
while ( current != NULL)
{
newNode = new NodeType
newNode->info = current->info;
newNode->next = NULL;
queueBack->next = newNode;
queueBack = newNode;
current = current->next;
}//end while
}// end try
catch (bad_alloc)
{
cout << "ERROR: Cannot allocate memory! ";
exit(EXIT_FAILURE);
}
}//end else
}
#endif
What is the effect of the following statements? If a statement is invalid, explain why it is invalid.
(a) LinkedQueueType
(b) LinkedQueueType
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
