I am tring to remove three string from a que using nodes. When I try to remove the third one it does so, but then I get a debugging error. I think the problem is that I am not deleting the last node but I cannot figure out what I am doing 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<<que3.remove()<<endl;
     cout<<que3.remove()<<endl;
cout<<que3.remove()<<endl; //this causes the run time error
 
    if(!que1.isEmpty())//this causes the run time error
    {
    Que que4(que1);
    cout<<que4.remove()<<endl;
    cout<<que4.remove()<<endl;
    }
    else
    {
        cout<<"Is empty"<<endl;
    }

 
	
	return 0;

}

here is the que.cpp file

Que::~Que()
{
   
    Node * temp = front;

	while (front)
		{
		  temp = front->getLink();
		  delete front;
		  front = 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   //I am pretty sure this is what is causing the error
    {
	Node *temp;
	temp=rear;
	rear = rear->getLink();
	string val = temp->getData();
	delete temp;//deletes node
	return val;
    }
}
void Que::print()
{
	printHelper(rear);
		
}
bool Que::isEmpty() const
{
	if(!front)
		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;
	}

}

I don't think this is needed but I am including the header file

#ifndef QUEUE
#define QUEUE
#include "Node.h"
class Que
{
private:
    Node* front;
    Node *rear;
	void printHelper(Node *);
   
public:
    Que();
    Que(const Que &);
    ~Que();
    void add(string);
    string remove();
    bool isEmpty() const;
    void operator=(const Que &);
    void print();

};

#endif

line 96: rear = rear->getLink(); will fail if rear is null.

what does the function GetLink() do?

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.