Queue Implementation Program Break Point Error

I Have Implemented Abstract DataType Queue
But There Is A Problem With Display Function
I Can not UnderStand What Is That Error "Break Point"
Please Help me

Here Is The Code






#include<iostream>
#include<string>
using namespace std;
struct etype{
string name;
int age;
};
struct celltype{
etype element;
celltype *next;
};
struct Queue{
celltype *front;
celltype *rear;
};
void MAKENULL(Queue* &q)
{
//create queue
q->front=new celltype; //dummy element
q->front->next = NULL;
q->rear = q->front; //if rear and front point to the same cell then this cell is dummy
}
bool EMPTY(Queue *q)
{
if (q->rear==q->front) return true;
else return false;
}
etype FRONT(Queue *q)
{
etype x;
x.age = 0;
x.name = " ";
if (EMPTY(q))
{
cout << "queue is empty ";
return x;
}
return q->front->next->element;
}
void ENQUEUE(etype x, Queue* &q)
{
q->rear->next = new celltype;
q->rear = q->rear->next;
q->rear->element = x;
q->rear->next = NULL;
}
void DEQUEUE(Queue* &q)
{
if (EMPTY(q))
{
cout << "queue is empty ";
return;
}
q->front->next = q->front->next->next;
}
void Display(Queue *q)
{
cout << "***********************************"<<endl;
celltype *current = q->front->next; //store the original front od the queue
etype x;
while(!EMPTY(q))
{
x = FRONT(q);
cout << "name: " <<x.name<< endl;
cout << "age: " <<x.age<< endl;
DEQUEUE(q);
}
q->front = current;
}
void Disposed_Greater_20(Queue* &q1)
{
Queue *q2 = new Queue;
MAKENULL(q2);

etype x;
while (!EMPTY(q1))
{
x = FRONT(q1);
if (x.age < 20) ENQUEUE(x, q2);
DEQUEUE(q1);
}
q1 = q2;
}
void main()
{
Queue *q1=new Queue;
MAKENULL(q1);
etype x;
for (int i = 0; i < 3; i++)
{
cout << "enter name: "; cin >> x.name;
cout << "enter age: "; cin >> x.age;
ENQUEUE(x,q1);
}
Display(q1);
system("pause");
}
Holy memory leakage batman. Ever heard of delete?

Why must you modify a queue to print it?

In DEQUEUE, why don't you update q->rear when you're on the last element?

Typically, ALL CAPS for identifiers are reserved for macros.
Please use code tags. They are on the right side of the text box, and have the button that looks like this: <>
It makes your code so much easier to look at.
I love my IDE. It even 'prettified' the code for us.
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
#include<iostream>
#include<string>
using namespace std;
struct etype {
	string name;
	int age;
};
struct celltype {
	etype element;
	celltype *next;
};
struct Queue {
	celltype *front;
	celltype *rear;
};
void MAKENULL(Queue* &q)
{
	//create queue
	q->front = new celltype; //dummy element
	q->front->next = NULL;
	q->rear = q->front; //if rear and front point to the same cell then this cell is dummy
}
bool EMPTY(Queue *q)
{
	if (q->rear == q->front) return true;
	else return false;
}
etype FRONT(Queue *q)
{
	etype x;
	x.age = 0;
	x.name = " ";
	if (EMPTY(q))
	{
		cout << "queue is empty ";
		return x;
	}
	return q->front->next->element;
}
void ENQUEUE(etype x, Queue* &q)
{
	q->rear->next = new celltype;
	q->rear = q->rear->next;
	q->rear->element = x;
	q->rear->next = NULL;
}
void DEQUEUE(Queue* &q)
{
	if (EMPTY(q))
	{
		cout << "queue is empty ";
		return;
	}
	q->front->next = q->front->next->next;
}
void Display(Queue *q)
{
	cout << "***********************************" << endl;
	celltype *current = q->front->next; //store the original front od the queue
	etype x;
	while (!EMPTY(q))
	{
		x = FRONT(q);
		cout << "name: " << x.name << endl;
		cout << "age: " << x.age << endl;
		DEQUEUE(q);
	}
	q->front = current;
}
void Disposed_Greater_20(Queue* &q1)
{
	Queue *q2 = new Queue;
	MAKENULL(q2);

	etype x;
	while (!EMPTY(q1))
	{
		x = FRONT(q1);
		if (x.age < 20) ENQUEUE(x, q2);
		DEQUEUE(q1);
	}
	q1 = q2;
}
void main()
{
	Queue *q1 = new Queue;
	MAKENULL(q1);
	etype x;
	for (int i = 0; i < 3; i++)
	{
		cout << "enter name: "; cin >> x.name;
		cout << "enter age: "; cin >> x.age;
		ENQUEUE(x, q1);
	}
	Display(q1);
	system("pause");
}


For future reference: http://www.cplusplus.com/forum/beginner/1/

Holy memory leakage batman.

I'm sorry. It's not rude to laugh, is it?

"Break Point"

I assume you're in Debug mode and running this in your debugger. There was an uncaught exception, most likely. Why not post exactly what the error says? We can help more that way. I've really only ever gotten this issue from de-referencing a null pointer. That's not to say that other things can't cause it, though. I'm guessing you're de-referencing null pointers, since the issue is in 'Display'.

I'll let you sit on Cire's comment about delete. You're not deleting any nodes you make null. Try to keep your functions from not having all caps. When I see all caps, I immediately look at the top of the file for a macro, pre-processor definition (#define), or constant.
Well ... Can Any One Run This Code And Get The Error ?!

I Can't Understand What Are You Saying about Delete ?!

any solution please

Thanks Guys
http://www.cplusplus.com/doc/tutorial/dynamic/

Whatever you allocate with 'new' also needs to be de-allocated with 'delete'.

When you print the queue, there is no reason to empty the queue. All you have to do is iterate through it and look at each node.
mohamedsul wrote:
any solution please

Yes.


cire wrote:
In DEQUEUE, why don't you update q->rear when you're on the last element?

Fix that.
And How Can I Update q->rear ?!

what can i do with it ?!

DEQUEUE Is For moving the front pointer until it goes to the last node which q->rear points to it
and that is the empty condition ((q->rear=q->front))

So how can i update q->rear ?
Last edited on
DEQUEUE Is For moving the front pointer until it goes to the last node

No, it's not. Dequeue is for removing the node after the dummy node. When you remove the last node, the dummy node remains but rear doesn't point to it.
Just Please Edit This Code To Work ... I Dont Know What To Do !!!
1
2
3
4
5
6
7
8
9
10
11
void DEQUEUE(Queue* &q)
{
	if (EMPTY(q))
	{
		cout << "queue is empty ";
		return;
	}
	q->front->next = q->front->next->next;
	if (some_condition)
		q->rear = q->front;
}


Just Please Edit This Code To Work ... I Dont Know What To Do !!!

Use your brain.
Dont Talk Like That To Me :( :(

Really Iv'e done this code by my self ... but i really cant know the error in DEQUEUE

anyway thanks for replying to me .. you've helped me alot

i did all my tries with no hope writing the condition

thanks :)
Topic archived. No new replies allowed.