The if statment I am using in my main program is not working, it should return the else value, but is returning the information in the if part of the statment and then it causes a run time error. I am not sure what is going wrong.

que1.add("Fish");
	que1.add("Whale");
	que1.add("Shark");
	Que que3(que1);
	que = que1;
	cout<<que1.remove()<<endl;
	cout<<que1.remove()<<endl;
	cout<<que.remove()<<endl;
	cout<<que.remove()<<endl;
    cout<<que.remove()<<endl;
	cout<<que3.remove()<<endl;
	cout<<que3.remove()<<endl;
	cout<<que3.remove()<<endl;

	if(!que1.isEmpty())//error happens here
	{  
	Que que4(que1);
	cout<<que4.remove()<<endl;
	cout<<que4.remove()<<endl;
	}
	else
	{
	cout<<"Is empty"<<endl;
	}

here is the class file. I don't understand why isEmpty is not working.

Que::~Que()
{
   
    /*Node * temp = rear;

	while (rear)
		{
		  temp = rear->getLink();
		  delete rear;
		  rear = temp;
		}*/

}
Que::Que()
{
	front=NULL;
	rear = NULL;
}
Que::Que(const Que &obj)
{
	Node *walker;
	Node *copy;
	walker= obj.rear;
	if(obj.isEmpty()){//check to see if node is null
		front=NULL;
		rear=NULL;
	return;
	}
	copy=new Node(walker->getData(), NULL);//set to copy first node
	rear =copy;
	walker=walker->getLink();
	while(walker) //if node not empty
	{

		copy->setLink(new Node(walker->getData(), NULL));//set values for new node
		walker=walker->getLink(); //creates link
		copy=copy->getLink();
	}
	front = copy; //set value to front

}

void Que::operator=(const Que & obj)
{
	Node *walker;
	Node *copy;
	walker= obj.rear;
	if(obj.isEmpty()){//check to see if node is null
		front=NULL;
		rear=NULL;
	return;
	}
	copy=new Node(walker->getData(), NULL);//set to copy first node
	rear =copy;
	walker=walker->getLink();
	while(walker)
	{

		copy->setLink(new Node(walker->getData(), NULL));
		walker=walker->getLink();
		copy=copy->getLink();
	}
	front = copy;

}
void Que::add(string file)
{
    if(front==NULL && rear==NULL) //if node 
    {
	front = new Node(file, NULL); //creates first node
    rear=front;
    }
    else
    {
    front->setLink(new Node(file, NULL));  //sets link for node
	front=front->getLink();
    }
}
string Que::remove()
{
   
    
    if(front==NULL && rear ==NULL) //nothing happens if empty
        return "";
    else if(front==rear && front!=NULL)  //gets node if only one
    {
     
        string val = rear->getData();
        delete rear;//deletes node
        return val;
    }
    else 
    {
	
	string val = front->getData();
    Node *temp;
	temp=rear;
    while(temp->getLink()!=front)
	temp=temp->getLink();
	delete front;//deletes node
    front =temp;
    front->setLink(NULL);
	return val;
    }
}
void Que::print()
{
	printHelper(rear);
		
}
bool Que::isEmpty() const//this should work, but it doesn't
{
	if(!rear)
		return true;
	else
		return false;
}
void Que::printHelper(Node *temp)
{
	if(!temp)
    {
    return;
    }
	else
    {
	printHelper(temp->getLink());  //sets to next node
	cout<<temp->getData()<<endl;
	}

}

What is the error? Why don't you put a statement to check the length in all of the queue addition/removal functions. This should help you track down the problem.

Also, why did you post this twice?

Dave

in the code you posted above you add 3 values to que1 but you only remove 2 values from it so it shouldn't be empty. to be more specific lines 6 and 7 are for que1 but line 8 is just que

Edited 6 Years Ago by NathanOliver: n/a

This article has been dead for over six months. Start a new discussion instead.