I have created a deque program that is supposed to read in the information from a separate program, and if something goes wrong along the way, it returns "Error". Checking each if statement individually, it seems like my error occurs at if .
I've looked at this several times, but still cannot find the source of the problem. Here's the application:
Code:
int main(int argc, char *argv[])
{
Deque q1;
int i;
q1 = Deque();
q1.append_rear(2);
q1.append_front(1);
q1.retrieve_front(i);
if (i != 1)
cout << "Error" << endl;
q1.serve_front();
q1.retrieve_front(i);
if (i != 2)
cout << "Error" << endl;
q1.serve_rear();
if (!q1.empty())
cout << "Error" << endl;
cout << "DONE" << endl;
return 0;
Also, this is the program it reads the information from. I assume this is the source of the problem:
Code:
#include "Deque.h"
#include <cassert>
Deque::Deque()
/*
Post: The Deque is initialized to be empty.
*/
{
front = 0;
rear = 0;
}
bool Deque::empty() const
/*
Post: Return true if the Deque is empty, otherwise return false.
*/
{
if (front == NULL)
{
return true;
}
return false;
}
Error_code Deque::append_front(const Deque_entry &item)
/*
Post: item is added to the front of the Deque. If the Deque is full
return an Error_code of overflow and leave the Deque unchanged.
*/
{
Error_code o;
Node *newFront = new Node;
assert (newFront);
newFront->entry = item;
newFront->previous = NULL;
if (front == NULL)
{
front = newFront;
rear = newFront;
newFront->next = NULL;
}
else //there are already nodes in the program
{
newFront->next = front;
front->previous = newFront;
front = newFront;
}
if (item > 1)
{
o = overflow;
return overflow;
}
}
Error_code Deque::append_rear(const Deque_entry &item)
/*
Post: item is added to the rear of the Deque. If the Deque is full
return an Error_code of overflow and leave the Deque unchanged.
*/
{
Error_code o;
Node *newRear = new Node;
assert (newRear);
newRear->entry = item;
newRear->next = NULL;
if (front == NULL)
{
front = newRear;
rear = newRear;
newRear->previous = NULL;
}
else
{
rear->next = newRear;
newRear->previous = rear;
rear = newRear;
}
if (item > 2)
{
o = overflow;
return overflow;
}
}
Error_code Deque::serve_front()
/*
Post: The front of the Deque is removed. If the Deque
is empty return an Error_code of underflow.
*/
{
Node *p;
p = front;
Error_code u;
if (front == NULL)
{
u = underflow;
return underflow;
}
else
{
front = front->next;
delete p;
return front;
}
}
Error_code Deque::serve_rear()
/*
Post: The rear of the Deque is removed. If the Deque
is empty return an Error_code of underflow.
*/
{
Node *p;
p = rear;
Error_code u;
if (rear == NULL)
{
u = underflow;
return underflow;
}
else
{
rear = rear->previous;
delete p;
}
}
Error_code Deque::retrieve_front(Deque_entry &item) const
/*
Post: The front of the Deque retrieved to the output
parameter item. If the Deque is empty return an Error_code of underflow.
*/
{
Error_code u;
if (front == NULL)
{
u = underflow;
return underflow;
}
item = front->entry;
}
Error_code Deque::retrieve_rear(Deque_entry &item) const
/*
Post: The rear of the Deque retrieved to the output
parameter item. If the Deque is empty return an Error_code of underflow.
*/
{
Error_code u;
if (front == NULL)
{
u = underflow;
return underflow;
}
item = rear->entry;
}
Any idea what I may be doing wrong? Perhaps I'm doing it right and I'm just paranoid? If you need to see other files (such as header files), I can provide that, too.
Thanks for any help ahead of time.