0

Hi there everyone!! Struggling a bit with some Linked Lists combined with copy constructors as well as operator overloading. Here is the code of the header (.h) and the implementation file (.C). Hope that you can help!

///////////////////////////////////////////////////////////
//////////////////                     ////////////////////
//////////////////       Queue.h       ////////////////////   
//////////////////                     ////////////////////
///////////////////////////////////////////////////////////

#ifndef QUEUE_H
#define QUEUE_H

#include <string>

using namespace std;


template<class T>
class QNode
{
	public:
		QNode(int d, QNode* n=0)
		{
			data = d;
			next = n;
		}
		T data;
		QNode* next;
	
};

class EmptyException
{
	public:
		EmptyException()
		{
			reason = "Empty queue";
		}
		
	private:
		string reason;
};


template<class T>
class DynamicQueue
{
	public:
		//Creates an empty queue
		DynamicQueue();
		//Destructs the queue
		~DynamicQueue();
		//Copy constructor
		DynamicQueue(const DynamicQueue<T>&);
		//Assignment operator
		DynamicQueue<T>& operator=(const DynamicQueue<T>&);
		
		//Inserts items at the end
		void enqueue(T);
		
		//Deletes and returns items from the front
		T dequeue();
		
		//Checks if the list is empty
		bool isEmpty();
		
	
	private:
		QNode<T> *front,*back;
};

#include "queue.C"

#endif



///////////////////////////////////////////////////////////
//////////////////                     ////////////////////
//////////////////       Queue.C       ////////////////////   
//////////////////                     ////////////////////
///////////////////////////////////////////////////////////


template<class T>
DynamicQueue<T>::DynamicQueue()
{
	front = back = 0;
	
}

template<class T>
DynamicQueue<T>::~DynamicQueue()
{
	QNode<T>* tmp = front;
	while(tmp != 0)
	{
		tmp = front->next;
		delete front;
		front = tmp;
	}
	front = back = 0;
}
		
template<class T>		
DynamicQueue<T>::DynamicQueue(const DynamicQueue<T>& other)
{
	
       //WHAT MUST THE CODE BE HERE???
	
}
				
template<class T>
DynamicQueue<T>& DynamicQueue<T>::operator=(const DynamicQueue<T>& other)
{

	//WHAT MUST THE CODE BE HERE???
	
	return *this;
}

template<class T>
void DynamicQueue<T>::enqueue(T data)
{
	//If the queue is empty
	if(front == 0)
	{
		front = back = new QNode<T>(data);
	}
	else
	{
		back->next = new QNode<T>(data);
		back = back->next;
	}
	
}
						
template<class T>	
T DynamicQueue<T>::dequeue()
{
	/*There are three cases:
	1. The queue is empty, this case has been coded for you
	2. The queue contains 1 node (both front and back point to it)
	3. The queue contains more than 1 node
	*/
	if(front == 0)
	{
		EmptyException e;
		throw e;
	}
	else if(front == back)
	{
		T info = front->data;
		delete front;
		front = back = 0;
		return info;
	}
	else
	{
		T info = front->data;
		QNode<T>* tmp = front;
		front = front->next;
		tmp->next = 0;
		delete tmp;
		tmp = 0;
		return info;
	}
}
		

template<class T>
bool DynamicQueue<T>::isEmpty()            //To test if the List is empty
{
	return front == 0;
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Phillamon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.