Program compiles OKAY but bombs out when running it

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.
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include<iostream>
#include "linkedQueue.h"
#include<queue>
using namespace 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;

 const int 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 149 in linkedQueue.h is posted here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//Header file linkedQueue.h

#include <iostream>
#include <cassert>
#include<queue>

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();
		//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()
{
	return false;
}

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;
         }
    }
}
assert(queueFront != NULL);


Your program deliberately ends if, at this point, queueFront is NULL.

So make sure that at this point, queueFront isn't NULL.
Last edited on
1
2
3
4
5
6
7
8
int move_to_rear(int Item)
{
     linkedQueueType<int> myQueue;

 const int 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.
Other comments:

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:
1
2
3
4
5
6
7
8
9
10
11
12
template<class Type>
const linkedQueueType<Type>& linkedQueueType<Type>::operator=
				(const linkedQueueType<Type>& otherQueue)
{
    if (&otherQueue != this) {
        clear();
        for (nodeType<Type> node = otherQueue.front; node; node = node->link) {
            add(node->info);
        }
    }
    return *this;
}


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
{
const int 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
}


1
2
3
4
5
6
7
8
9
10

    myQueue.addQueue(1766);
    myQueue.addQueue(1734);
    myQueue.addQueue(1732);
    myQueue.addQueue(1767);
    myQueue.addQueue(1764);
   // move_to_rear(1766);
  cout << myQueue.back();
}
//} 

I get all the desired output:
1
2
3
4
5
1732
1764
Process returned 0 (0x0)   execution time : 0.076 s
Press any key to continue.

1732 for myQueue.front() and
1764 for myQueue.back()

Please help me find out what is wrong with the function :move_to_rear()
Where does myQueue come from? As @dhayden pointed out: it must not be a local variable.
There is something seriously wrong with the function: move_to_rear
Did you see my comments on it in my first post? Did you fix the issues?
Topic archived. No new replies allowed.