Hi all,
So I'm working on this program to use a circular doubly linked list to implement a queue class. It went fairly well, but I'm stuck on the copy constructor, which causes runtime errors whenever I try to test it. Nonetheless, I really don't see what I am doing wrong.
Here's the header/implementation file for the class (all one file because I'm using g++):
//#include "CharQueueTemplate.h"
#include <cassert>
#include <new>
#include <cstddef>
using namespace std;
template <typename T>
class CharQueue
{
public:
virtual bool isEmpty() const = 0;
virtual void enqueue(const T & newItem) = 0;
virtual void dequeue(void) = 0; // dequeue an item and return its value
virtual T getFront(void) const = 0; // return the value of the first item in the queue (but do not dequeue it)
};
template <typename T> class Queue : public CharQueue<T>
{
private:
struct qNode {
T element;
qNode *next;
qNode *prev;
};//end qNode
qNode *front;
qNode *back;
qNode *head;
public:
Queue();//default constructor
Queue(const Queue<T>& q);//copy constructor
virtual ~Queue();//destructor
virtual bool isEmpty() const;//checks to see if the queue is empty
virtual void enqueue(const T & value);//places value at the back of the queue (prior to the head pointer in this case)
virtual void dequeue();//removes the front value from the queue
//virtual void dequeue(T& head);
virtual T getFront() const;
bool operator==(const Queue & source);
};//end Queue
template <typename T> Queue<T>::Queue()
{
head = NULL;
}//end default constructor
//evil copy constructor
template <typename T> Queue<T>::Queue(const Queue & aq)
{
/*if(!aq.isEmpty())
{
qNode *original = aq.head;
qNode *newhead = new qNode;//points to the head of the list
newhead->element = original->element;
qNode *oldh = original;
qNode *newh = newhead;
while(original != oldh->prev) {
//newhead->next = original->next;
newhead=newhead->next;
original=original->next;
newhead->element = original->element;
}//end while
cout << "list copied" << endl;
head = newh;
//front = newhead;
//qNode *nCurrent = newhead;
//qNode *temp = new qNode;
/*while(original != aq.back)
{
original = original->next;
nCurrent->next= new qNode;
nCurrent = nCurrent->next;
nCurrent->element = original->element;
nCurrent->next = NULL;
back=nCurrent;//possible error
}//end while
}//end if
*/
T value;
qNode * orig = aq.head;
do{
value = orig->element;
qNode *temp = new qNode;
temp->element = value;
//temp->next = head;
//temp->prev = head->prev;
//head->prev->next = temp;
//back = temp;
if(isEmpty()) {
//front = temp;
head= temp;
head->next=head;
head->prev =head;
}//end if
else{
//back->next = temp;
head->prev->next=temp;
temp->prev=head->prev;
head->prev=temp;
head->prev->next=head;
}
cout << "smurf" << endl;
orig = orig->next;
}
while(orig->next!=aq.head);
}//end copy constructor
template <typename T> Queue<T>::~Queue()
{
while(!isEmpty())
{
dequeue();
}
}//end destructor
template <typename T> bool Queue<T>::isEmpty() const
{
return (head == NULL);
}//end isEmpty
template <typename T> void Queue<T>::enqueue(const T& value)
{
try
{
//make new node
qNode *temp = new qNode;
temp->element = value;
//temp->next = head;
//temp->prev = head->prev;
//head->prev->next = temp;
//back = temp;
if(isEmpty()) {
//front = temp;
head= temp;
head->next=head;
head->prev =head;
}//end if
else{
//back->next = temp;
head->prev->next=temp;
temp->prev=head->prev;
head->prev=temp;
head->prev->next=head;
}
//back = temp;
}//end try
catch (bad_alloc e)
{
throw string("Memory cannot be allocated.");
}//end catch
}//end enqueue
template <typename T> void Queue<T>::dequeue()
{
if(!isEmpty())
{
qNode *temp = head;
if(head->next == head)
{
head=NULL;
}
else
{
head->prev->next = head->next;
head->next->prev = head->prev;
head=head->next;
delete temp;
}//end else
}//end if
}//end dequeue
template <typename T> T Queue<T>::getFront() const
{
if(!isEmpty())
{
return head->element;
}//end if
}//end getFront
template <typename T> bool Queue<T>::operator == (const Queue & source)
{
if(head==NULL && source.head!=NULL)
{
return false;
}//end if
qNode * newone = head;
qNode * oldone = source.head;
while(newone->next != head && oldone->next != source.head)
{
if(newone->element != oldone->element)
{
return false;
}//end if
}//end while
return true;
}//end operator overload
//end q.h
If anyone could point out what I'm doing wrong, it would be greatly appreciated. This has me climbing the walls.