I want to move the front element of a queue to the back of the queue.
The program bombs out with this message:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1732
1767
1734
1764
1766
Assertion failed: queueFront != NULL, file C:\Martin\MalikChapter8\Chapter 8 Sou
rce Code\linkedQueue\linkedQueue.h, line 149
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Process returned 3 (0x3) execution time : 2.865 s
Press any key to continue.
#include<iostream>
#include "linkedQueue.h"
#include<queue>
usingnamespace std;
int move_to_rear(int Item);
int main()
{
int Item;
linkedQueueType<int> myQueue;
linkedQueueType<int> yourQueue;
myQueue.addQueue(1732);
myQueue.addQueue(1767);
myQueue.addQueue(1734);
myQueue.addQueue(1764);
myQueue.addQueue(1766);
//yourQueue = myQueue;
while(!myQueue.isEmptyQueue())
{
cout<< myQueue.front() <<" "<< endl;
myQueue.deleteQueue();
}
myQueue.addQueue(1766);
myQueue.addQueue(1734);
myQueue.addQueue(1732);
myQueue.addQueue(1767);
myQueue.addQueue(1764);
move_to_rear(1766);
cout << myQueue.back();
}
int move_to_rear(int Item)
{
linkedQueueType<int> myQueue;
constint first = myQueue.front(); //getting the first
myQueue.deleteQueue(); //removing him
myQueue.addQueue(first); //adding it to the back to the queue(which will be the rear
}
//Header file linkedQueue.h
#include <iostream>
#include <cassert>
#include<queue>
usingnamespace 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();
//Function to determine if the queue is empty.
//Postcondition: Returns true if the queue is empty;
// otherwise, it returns false
bool isFullQueue();
//Function to determine if the queue is full.
//Postcondition: Returns true if the queue is full;
// otherwise, it returns false
void destroyQueue();
//Function to delete all elements from the queue
//Postcondition: queueFront = NULL, queueRear = NULL
void initializeQueue();
//Initialize the queue to an empty state
//Postcondition: queueFront = NULL, queueRear = NULL
Type front();
//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();
//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 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
};
template<class Type>
linkedQueueType<Type>::linkedQueueType() //default constructor
{
queueFront = NULL; // set front to null
queueRear = NULL; // set rear to null
}
template<class Type>
bool linkedQueueType<Type>::isEmptyQueue()
{
return(queueFront == NULL);
}
template<class Type>
bool linkedQueueType<Type>::isFullQueue()
{
returnfalse;
}
template<class Type>
void linkedQueueType<Type>::destroyQueue()
{
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
}
template<class Type>
void linkedQueueType<Type>::initializeQueue()
{
destroyQueue();
}
template<class Type>
void linkedQueueType<Type>::addQueue(const Type& newElement)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>; //create the node
assert(newNode != NULL);
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()
{
assert(queueFront != NULL);
return queueFront->info;
}
template<class Type>
Type linkedQueueType<Type>::back()
{
assert(queueRear!= NULL);
return queueRear->info;
}
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
cerr<<"Cannot remove from an empty queue"<<endl;
}//end deleteQueue
template<class Type>
linkedQueueType<Type>::~linkedQueueType() //destructor
{
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
}
template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=
(const linkedQueueType<Type>& otherQueue)
{
//Write the definition of to overload the assignment operator
nodeType<Type> *temp = otherQueue.queueFront;
while(temp->link != NULL)
{
queueFront->info = temp->info;
queueFront->link = new nodeType<Type>;
queueFront = queueFront->link;
temp = temp->link;
}
}
//copy constructor
template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
//Write the definition of the copy constructor
if(otherQueue.isEmptyQueue())
{//ifthe copy to be made is NULL
queueFront = NULL; //queueFront & queueTail are both null
queueRear = NULL;
} // end if
else
{
linkedQueueType temp_ptr_old = otherQueue.queueFront;
linkedQueueType temp_ptr_new;
queueRear = new nodeType<Type>;
queueRear->info = temp_ptr_old->info;
queueRear->link = NULL;
queueFront = queueRear;
//First node created and filled with data
// New nodes are now created after this first node
temp_ptr_old = temp_ptr_old->link;
//temp_ptr_old now points to the second node
//or NULL if there is no second node
while(temp_ptr_old != NULL)
{
temp_ptr_new = new nodeType<Type>;
temp_ptr_new->info = temp_ptr_old->data;
temp_ptr_new->link = NULL;
queueRear->link = temp_ptr_new;
queueRear = temp_ptr_new ;
temp_ptr_old = temp_ptr_old->link;
}
}
}
int move_to_rear(int Item)
{
linkedQueueType<int> myQueue;
constint first = myQueue.front(); //getting the first
myQueue.deleteQueue(); //removing him
myQueue.addQueue(first); //adding it to the back to the queue(which will be the rear
}
Line 3: this is not the same variable as myQueue defined in function main(). It's a local variable in move_to_rear() that's a completely new queue. It just happens to have the same name as the one in main.
Line 5: Since myQueue is the local variable and you haven't added anything to it, it is empty. Thus myQueue.front() doesn't refer to any item and you get an error.
Why does move_to_rear() take a parameter? Do you really mean to extract an item from the middle of the queue and move it to the rear? If so then a queue is not the right data structure.
Rename addQueue, isFullQueue, initializeQueue etc. to add, isFull, initialize, etc. The "Queue" in the names is redundant.
isFullQueue() has a bug.
destroyQueue(), initializeQueue() deleteQueue() and the destructor all do the same thing. Consider having one clear() method. The destructor can call clear() and the others are unnecessary. I see that deleteQueue() prints a message if the queue wasn't empty. That doesn't seem necessary.
The assignment operator needs to check for assignment to self. Do this every time you write any assignment operator. Also it should clear the current contents of the queue before copying from the other one. Finally, the code that you have is all wrong. The easy way to do it is to use the existing methods:
Implement the copy constructor with the assignment operator. Just be sure to initialize the front and rear pointers first:
1 2 3 4 5 6
template<class Type>
linkedQueueType<Type>::linkedQueueType(const linkedQueueType<Type>& otherQueue)
{
front = rear = nullptr; // Assuming to rename queueFront and queueRear to front and rear
*this = otherQueue; // use assignment operator
}
If you do all this, I think you'll find that the code is much smaller.
There is something seriously wrong with the function: move_to_rear because if I comment it out:
1 2 3 4 5
{
constint first = myQueue.front(); //getting the first
myQueue.deleteQueue(); //removing him
myQueue.addQueue(first); //adding it to the back to the queue(which will be the rear
}