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;
	}

}

Recommended Answers

All 3 Replies

What is que ? Is it a pointer, or is it another Que ? If it's a pointer, then you should be .remove() ing 5 times and it should be empty. If it's another Que , then of course que1 is not empty, you've only .remove() d twice, and it's not empty.
Basically, this is all I can say whilst not knowing the rest of the code. Some of your code is a little, well, difficult to understand just by looking at it on its own. At any rate, I hope this helps you get in the mindset of figuring it out.

Thanks that makes sense, i still can't figure out why I am getting a run tim error.

Hi aaronmk2 :-)

I think I have found the reason for your runtime error.

Take a look at your remove-function, where you delete the last node in the Que. You extract the string and delete the node but you NEVER set the 'front' and 'rear' to NULL.

The next time you call remove, the pointers are still non-NULL, and you try to access a node that does not exist.

commented: Thanks, now my program runs fine +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.