Problem with Circular Linked Queue Program

S

shanknbake

Here is my code. I've noted where the program crashes. I'm doing this
program as a project for school.

//cqueue.h file
//HEADER FILE
http://rafb.net/paste/results/Nh0aLB77.html
--------------------------------------------------------------------
//cqueue.cpp file
//IMPLEMENTATION OF cqueue CLASS
http://rafb.net/paste/results/A2gXAr73.html
-------------------------------------------------------------------
//driver.cpp file
//DRIVER FILE
http://rafb.net/paste/results/iZbqug40.html

The problem appears to be in my driver file under Deletepassenger and
showpassenger functions.
Plz let me know if something pops out at u
thanks
-Kevin
 
D

doug turnbull

shanknbake said:
Here is my code. I've noted where the program crashes. I'm doing this
program as a project for school.

//cqueue.h file
//HEADER FILE
http://rafb.net/paste/results/Nh0aLB77.html
--------------------------------------------------------------------
//cqueue.cpp file
//IMPLEMENTATION OF cqueue CLASS
http://rafb.net/paste/results/A2gXAr73.html
-------------------------------------------------------------------
//driver.cpp file
//DRIVER FILE
http://rafb.net/paste/results/iZbqug40.html

The problem appears to be in my driver file under Deletepassenger and
showpassenger functions.
Plz let me know if something pops out at u
thanks
-Kevin

Is it kosher to get help from others like this on your school project,
I know for many schools it is an honor code violation. The kinds of
help and who you can ask may be restricted by your school or instructor.
 
S

shanknbake

I agree completely doug, however I'm not asking for someone to write
the code for me..but merely to look over the code I've written and
point out an obvious mistake...which I'm over looking.
 
A

Alan Johnson

shanknbake said:
Here is my code. I've noted where the program crashes. I'm doing this
program as a project for school.

//cqueue.h file
//HEADER FILE
http://rafb.net/paste/results/Nh0aLB77.html
--------------------------------------------------------------------
//cqueue.cpp file
//IMPLEMENTATION OF cqueue CLASS
http://rafb.net/paste/results/A2gXAr73.html
-------------------------------------------------------------------
//driver.cpp file
//DRIVER FILE
http://rafb.net/paste/results/iZbqug40.html

The problem appears to be in my driver file under Deletepassenger and
showpassenger functions.
Plz let me know if something pops out at u
thanks
-Kevin

Your enqueue logic seems suspect to me. You have:
tempPtr->next=NULL;

if(IsEmpty())
{
rear = tempPtr;
count++;
}
else if(!IsFull())
{
rear->next=rear;
rear = tempPtr;
count++;
}

If your queue is empty, then you wind up with a queue with a single
node whose next pointer points to NULL, which doesn't fit the
definition of a circular queue.

Assuming that first problem is fixed, the else case doesn't seem
correct either. You wind up with rear pointing to the new node, and
the rest of the queue is leaked.

Just off the top of my head (meaning not compiled or tested) I would
expect it to look something like:

if (IsEmpty())
{
rear = tempPtr->next = tmpPtr;
count++;
}
else if (!IsFull())
{
tempPtr->next = rear->next;
rear = tempPtr;
count++;
}

Note especially the base case of 1 node. It's next pointer should
point back to itself, or else there is nothing circular about the
queue.
 
A

Alan Johnson

Alan said:
Note especially the base case of 1 node. It's next pointer should
point back to itself, or else there is nothing circular about the
queue.

This also seems to plague your deque function and your destructor.
Mentally step through your dequeue function for the case when count==1
and think about what happens.

Also, your destructor tries to clean up by looping through your list
until it hits NULL, which just won't ever happen in a circular queue.
I suggest reusing your dequeue logic (after you fix it) for the
destructor. That is, just keep calling dequeue until count == 0.
 
S

shanknbake

thanks Alan,
Let me put these ideas into effect and see what i come up with. I'll
keep you posted of any success.
-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top