469,580 Members | 1,953 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,580 developers. It's quick & easy.

implementing a copy contructor for ADT queue

I did implement the copy constructor but still have a problem with
it. It is not working. What could be wrong?

#include "QueueP.h"
#include <cassert // for assert
#include <new // for bad_alloc
#include <iostream>
//typedef std::queue<QueueItemTypeQueue;
using namespace std;
//private:{Queue::Queue(const Queue& Q)}
Queue::Queue() : backPtr(0), frontPtr(0)//copy constructor initializes
the data members
{
} // end default constructor

Queue::Queue(const Queue& Q) throw(OutOfStorageException)// because
the compiler-generated default constructorwould not necessarily
initialize data members to appropriate values, you must create your
own. pg 198
//:backPtr(const Queue& Q)
//:frontPtr (const Queue& Q)
if (Q.frontPtr == 0)
frontPtr = 0; // original Queue is empty

else
{ // copy first node
try
{
frontPtr = new QueueNode;
frontPtr->item = Q.frontPtr->item;

// copy rest of Queue
QueueNode *newPtr = frontPtr; // new Queue pointer
// newPtr points to last node in new Queue
// origPtr points to nodes in original Queue
for (QueueNode *origPtr = Q.frontPtr->next;
origPtr != 0;
origPtr = origPtr->next)
{ newPtr->next = new QueueNode;
newPtr = newPtr->next;
newPtr->item = origPtr->item;
} // end for

newPtr->next = 0;
} // end try
catch (bad_alloc e)
{
throw OutOfStorageException(
"OutOfStorageException: Cannot allocate memory in copy
constructor.");
} // end catch
} // end if
} // end copy constructor

Queue::~Queue()
{
while (!isEmpty() )
{
dequeue();
} // end while
assert ( (backPtr == 0) && (frontPtr == 0) );
} // end destructor

bool Queue::isEmpty() const
{
return backPtr == 0;
} // end isEmpty

void Queue::enqueue(const QueueItemType& newItem)
throw(OutOfStorageException)
{
try
{
QueueNode *newPtr = new QueueNode;

newPtr->item = newItem;

newPtr->next = 0;

if (isEmpty() )
{
frontPtr = newPtr;
}
else
{
backPtr->next = newPtr;
} // end if

backPtr = newPtr;
}
catch(bad_alloc e)
{
throw OutOfStorageException("Memory allocation failed.");
} // end try/catch
} // end enqueue

void Queue::dequeue() throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot dequeue");
}
else
{ // queue is not empty; remove front
QueueNode *tempPtr = frontPtr;
if (frontPtr == backPtr) // special case?
{ // yes, one node in queue
frontPtr = 0;
backPtr = 0;
}
else
{
frontPtr = frontPtr->next;
} // end if
tempPtr->next = 0; // defensive strategy
delete tempPtr;
// }
} // end if
} // end dequeue

void Queue::dequeue(QueueItemType& queueFront)
throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot dequeue");
}
else
{ // queue is not empty; retrieve front
queueFront = frontPtr->item;
dequeue(); // delete front
} // end if
} // end dequeue

void Queue::getFront(QueueItemType& queueFront) const
throw(OutOfDataException)
{
if (isEmpty() )
{
throw OutOfDataException("Empty queue, cannot getFront");
}
else
{
// queue is not empty; retrieve front
queueFront = frontPtr->item;
} // end if
} // end getFront

Oct 29 '07 #1
0 2534

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gandalf | last post: by
8 posts views Thread by luis molina Micasoft | last post: by
4 posts views Thread by pat | last post: by
15 posts views Thread by Brett Wickard | last post: by
4 posts views Thread by TwistedPair | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.