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");
}
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.
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.
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.