problems with dequeue function for my linked queue class program

I am working on this linked queue class program and it runs without any compiler errors but there is a break point in my dequeue function. It is pointing towards:
"item = qFront-> info;" line 83
in the dequeue function and I am not sure how to fix this error. Can anyone help fix my code? My code is posted below. Thanks.

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

#include <iostream>
#include <cstddef> // For NULL
#include <new> // For bad_alloc
using namespace std;

template <class ItemType> 
struct NodeType 
{ 
ItemType info; 
NodeType <ItemType> * next; 
;};

template <class ItemType> 
class QueType 
{

public: 
QueType (); // CONSTRUCTOR 
~ QueType (); // DESTRUCTOR 
bool IsEmpty () const; 
bool IsFull () const; 
void Enqueue (ItemType item); 
void Dequeue (ItemType & item); 
void MakeEmpty (); 
void Print();
QueType (const QueType& anotherQue);
private: 
NodeType <ItemType> * qFront; 
NodeType <ItemType> * qRear; 
;};


template <class ItemType> QueType <ItemType>:: QueType () // CONSTRUCTOR 
{ 
qFront = NULL; 
qRear = NULL; 
} 
template <class ItemType> QueType <ItemType>:: ~ QueType () // destructor 
{ 
MakeEmpty (); 
}



template <class ItemType> bool QueType <ItemType>:: IsEmpty () const 
{ 
return (qFront == NULL); 
}


template <class ItemType> void QueType <ItemType>:: Enqueue (ItemType newItem) 
// Adds newItem to the rear of the queue. 
// Pre: Queue has been initialized. 
// Queue is not full. 
// Post: newItem is at rear of queue. 
{ 
NodeType <ItemType> * ptr;


ptr = new NodeType <ItemType>; 
ptr-> info = newItem; 
ptr-> next = NULL; 
if (qRear == NULL) 
qFront = ptr; 
else qRear-> next = ptr; 
qRear = ptr; 
}


template <class ItemType> void QueType <ItemType>:: Dequeue (ItemType & item) 
// Removes element from from front of queue 
// And returns it in item. 
// Pre: Queue has been initialized. 
// Queue is not empty. 
// Post: Front element has been removed from queue. 
// Item is a copy of removed element. 
{ 
NodeType <ItemType> * tempPtr;


tempPtr = qFront; 
item = qFront-> info; 
qFront = qFront-> next; 
if (qFront == NULL) qRear = NULL; 
delete tempPtr; 
}


template <class ItemType> void QueType <ItemType>:: MakeEmpty () 
// Post: Queue is empty; all elements deallocated. 
{ 
NodeType<ItemType>* tempPtr;
while(qFront != NULL) {
tempPtr = qFront;
qFront = qFront->next;
delete tempPtr;
}
qRear=NULL;

} 




template <class ItemType> bool QueType <ItemType>:: IsFull () const 
// Returns true if there is no room for another NodeType 
// Node on the free store; false otherwise. 
{ 
NodeType <ItemType> * location; 
try 
{ 
location = new NodeType <ItemType>; 
delete location; 
return false; 
} 
catch (bad_alloc exception) 
{ 
return true; 
} 
}


template<class ItemType>
QueType<ItemType>::QueType (const QueType& anotherQue)
{
NodeType<ItemType>* ptr1;
NodeType<ItemType>* ptr2;

if (anotherQue.qFront == NULL)
qFront = NULL;
else
{
qFront = new NodeType<ItemType>;
qFront->info = anotherQue.qFront->info;
ptr1 = anotherQue.qFront->next;
ptr2 = qFront;
while (ptr1 != NULL)
{
ptr2->next = new NodeType<ItemType>;
ptr2 = ptr2->next;
ptr2->info = ptr1->info;
ptr1 = ptr1->next;
}
ptr2->next = NULL;
qRear = ptr2;
} 
}

template<class ItemType>
void QueType<ItemType>::Print()
{
NodeType<ItemType> *tempPtr;
if ( !IsEmpty() )
{
tempPtr = qRear->next;
cout << "The queue contains: ";
while ( tempPtr != qRear )
{
cout << tempPtr->info << " ";
tempPtr = tempPtr->next;
}

cout << tempPtr->info << endl << endl;
}
else 
cout << "The queue is empty." << endl << endl;
}



int main()
{
QueType<int>IntQueue;
int x;
IntQueue.MakeEmpty();
IntQueue.Dequeue(x);
IntQueue.Enqueue(10);
IntQueue.Enqueue(20);
IntQueue.Enqueue(30);
IntQueue.Enqueue(40);

IntQueue.Dequeue(x);

cout << "The int queue contains: " << endl;
IntQueue.Print();
if(IntQueue.IsFull() == false)
cout << "The int queue is not full !" << endl;
else
cout << "The int queue is full !" << endl;

QueType<float>FloatQueue;
float y;
FloatQueue.MakeEmpty();
FloatQueue.Dequeue(y);
FloatQueue.Enqueue(7.1);

FloatQueue.Enqueue(2.3);

FloatQueue.Enqueue(3.1);
FloatQueue.Dequeue(y);
cout << "The float queue contains:
 FloatQueue2.Print();


	system("pause");
	return 0;
}
 
Last edited on
This is a very general question that probably requires a thorough understanding of your code to answer. Try to break it down into a more specific question. Trace back through the code yourself (using printf, cout, or gdb) to identify when there is a problem, for what types of input, during which iteration, etc. Also, I think you are misusing the term "breakpoint" -- that usually refers to a point in the code that the programmer sets for the program to pause execution.
Topic archived. No new replies allowed.