Please help, below is the assignment and code... Download the file linkedQueue.z
ID: 3549032 • Letter: P
Question
Please help, below is the assignment and code...
Download the file linkedQueue.zip. Modify the linkedQueue.h file by
implementing the details for the class method called highestPriority(int
position), which will take the item at the given position in the queue and move it to
the front of the queue. If the given position is greater than the length of the queue, then
the original queue will remain unchanged. Note that once the item is moved, all of the
remaining items in the queue will stay in their original order.
Hint: Again, you only need to modify the linkedQueue.h file by adding the necessary
code to implement the TODO areas as noted in the comments. The prototype for the
function and everything else in the program will remain unchanged (include the driver.cpp
file). Consider using two while-loops, two temporary queues, and two temporary item
variables for this implementation.
Remember that the main queue will be in the this pointer variable. You can create the
temporary queues with the original contents of the main queue as follows:
linkedQueueType<Type> tmpQueue1 = *this;
linkedQueueType<Type> tmpQueue2 = *this; 3
After creating the temporary queues, clear the main queue, using the initializeQueue()
method:
this->initializedQueue();
Output: The output for the program after the function is implemented should appear as
follows:
Original queue:
3.14 0.11 7.18 0.29 9.62
Queue after moving item 2 to front:
0.11 3.14 7.18 0.29 9.62
Queue after moving item 7 to front:
0.11 3.14 7.18 0.29 9.62
Queue after moving item 5 to front:
9.62 0.11 3.14 7.18 0.29
** Press any key to continue **
//Header file linkedQueue.h
#ifndef H_linkedQueue
#define H_linkedQueue
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedQueueType
{
public:
const linkedQueueType<Type>& operator=
(const linkedQueueType<Type>&);
//Overload the assignment operator.
bool isEmptyQueue() const;
//Function to determine whether the queue is empty.
//Postcondition: Returns true if the queue is empty,
// otherwise returns false.
bool isFullQueue() const;
//Function to determine whether the queue is full.
//Postcondition: Returns true if the queue is full,
// otherwise returns false.
void initializeQueue();
//Function to initialize the queue to an empty state.
//Postcondition: queueFront = NULL; queueRear = NULL
Type front() const;
//Function to return the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the first element of the
// queue is returned.
Type back() const;
//Function to return the last element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: If the queue is empty, the program
// terminates; otherwise, the last element of the
// queue is returned.
void print();
// Function to print the items in the queue
void highestPriority(int position);
// Moves an item at the given position to the front of the
// queue.
void addQueue(const Type& queueElement);
//Function to add queueElement to the queue.
//Precondition: The queue exists and is not full.
//Postcondition: The queue is changed and queueElement is
// added to the queue.
void deleteQueue();
//Function to remove the first element of the queue.
//Precondition: The queue exists and is not empty.
//Postcondition: The queue is changed and the first element
// is removed from the queue.
linkedQueueType();
//Default constructor
linkedQueueType(const linkedQueueType<Type>& otherQueue);
//Copy constructor
~linkedQueueType();
//Destructor
private:
nodeType<Type> *queueFront; //pointer to the front of the queue
nodeType<Type> *queueRear; //pointer to the rear of the queue
};
//Default constructor
template<class Type>
linkedQueueType<Type>::linkedQueueType()
{
queueFront = NULL; //set front to null
queueRear = NULL; //set rear to null
} //end default constructor
template<class Type>
bool linkedQueueType<Type>::isEmptyQueue() const
{
return(queueFront == NULL);
} //end
template<class Type>
bool linkedQueueType<Type>::isFullQueue() const
{
return false;
} //end isFullQueue
template <class Type>
void linkedQueueType<Type>::initializeQueue()
{
nodeType<Type> *temp;
while (queueFront!= NULL) //while there are elements left
//in the queue
{
temp = queueFront; //set temp to point to the
//current node
queueFront = queueFront->link; //advance first to
//the next node
delete temp; //deallocate memory occupied by temp
}
queueRear = NULL; //set rear to NULL
} //end initializeQueue
template <class Type>
void linkedQueueType<Type>::print()
{
Type item;
linkedQueueType<Type> tmpQueue = *this;
cout << " ";
while (tmpQueue.isEmptyQueue() == false)
{
item = tmpQueue.front();
tmpQueue.deleteQueue();
cout << item << " ";
}
cout << endl;
}
template <class Type>
void linkedQueueType<Type>::highestPriority(int position)
{
// TODO: Implement the details of this function that moves
// a given item at value of the position variable to
// the front of the queue. Note that all of the other items
// in the queue will remain in their same relative position.
}
template <class Type>
void linkedQueueType<Type>::addQueue(const Type& newElement)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>; //create the node
newNode->info = newElement; //store the info
newNode->link = NULL; //initialize the link field to NULL
if (queueFront == NULL) //if initially the queue is empty
{
queueFront = newNode;
queueRear = newNode;
}
else //add newNode at the end
{
queueRear->link = newNode;
queueRear = queueRear->link;
}
}//end addQueue
template <class Type>
Type linkedQueueType<Type>::front() const
{
assert(queueFront != NULL);
return queueFront->info;
} //end front
template <class Type>
Type linkedQueueType<Type>::back() const
{
assert(queueRear!= NULL);
return queueRear->info;
} //end back
template <class Type>
void linkedQueueType<Type>::deleteQueue()
{
nodeType<Type> *temp;
if (!isEmptyQueue())
{
temp = queueFront; //make temp point to the
//first node
queueFront = queueFront->link; //advance queueFront
delete temp; //delete the first node
if (queueFront == NULL) //if after deletion the
//queue is empty
queueRear = NULL; //set queueRear to NULL
}
else
cout << "Cannot remove from an empty queue" << endl;
}//end deleteQueue
//Destructor
template <class Type>
linkedQueueType<Type>::~linkedQueueType()
{
nodeType<Type> *temp;
while (queueFront != NULL) //while there are elements
//left in the queue
{
temp = queueFront; //set temp to point to the
//current node
queueFront = queueFront->link; //advance first to
//the next node
delete temp; //deallocate memory occupied by temp
}
queueRear = NULL; // set rear to null
} //end destructor
template <class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=
(const linkedQueueType<Type>& otherQueue)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list.
if (this != &otherQueue) //avoid self-copy
{
if (queueFront != NULL) //if the list is not empty, destroy the list
initializeQueue();
if (otherQueue.queueFront == NULL) //otherList is empty
{
queueFront = NULL;
queueRear = NULL;
}
else
{
current = otherQueue.queueFront; //current points to the
//list to be copied
//copy the front element
queueFront = new nodeType<Type>; //create the node
queueFront->info = current->info; //copy the info
queueFront->link = NULL; //set the link field of
//the node to null
queueRear = queueFront; //make rear point to the first node
current = current->link; //make current point to the next
//node of the list being copied
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
queueRear->link = newNode;
queueRear = newNode;
current = current->link;
}//end while
}//end else
}//end else
return *this;
} //end assignment operator
//copy constructor
template <class Type>
linkedQueueType<Type>::linkedQueueType
(const linkedQueueType<Type>& otherQueue)
{
nodeType<Type> *newNode; //pointer to create a node
nodeType<Type> *current; //pointer to traverse the list
if (otherQueue.queueFront == NULL) //otherList is empty
{
queueFront = NULL;
queueRear = NULL;
}
else
{
current = otherQueue.queueFront; //current points to the
//list to be copied
//copy the first node
queueFront = new nodeType<Type>; //create the node
queueFront->info = current->info; //copy the info
queueFront->link = NULL; //set the link field of
//the node to null
queueRear = queueFront; //make rear point to the
//front node
current = current->link; //make current point to the
//next node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType<Type>; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of
//newNode to null
queueRear->link = newNode; //attach newNode after rear
queueRear = newNode; //make rear point to
//the actual rear node
current = current->link; //make current point to
//the next node
}//end while
}//end else
}//end copy constructor
#endif