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

Dani AI

Generated

Brief summary and fixes for the missing pieces in 's queue: the destructor, enqueue and dequeue look fine; what's missing is a deep-copying copy constructor and a safe assignment operator. Two important notes before the code: change the QNode constructor parameter from int to T so the node can hold any template type, and include <utility> (or <algorithm>) if you use std::swap. 's pointer to the older thread is on the right track—use the copy-and-swap idiom for a concise, exception-safe assignment.

Here is a simple, correct copy constructor that creates a fresh list by iterating the other queue and reusing your existing enqueue:

template<class T>
DynamicQueue<T>::DynamicQueue(const DynamicQueue<T>& other)
    : front(0), back(0)
{
    for (QNode<T>* cur = other.front; cur; cur = cur->next)
        enqueue(cur->data);
}

And here is an easy, safe assignment operator using copy-and-swap; it leverages the copy constructor above and gives strong exception safety:

template<class T>
DynamicQueue<T>& DynamicQueue<T>::operator=(DynamicQueue<T> other) // pass-by-value
{
    std::swap(front, other.front);
    std::swap(back, other.back);
    return *this;
}

Cautions: if T's copy constructor can throw, copy-and-swap ensures the original object remains unchanged. If you prefer not to use pass-by-value, implement operator= by clearing the current nodes (delete loop) then copying like the copy ctor, but make sure to handle self-assignment and exceptions carefully.

take a loot a bit at this thread.

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.