469,926 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How can you implement a copy constructor for ADT queue

how do you implement a copy constructor for this pointer-based ADT
queue

#include <cassert // for assert
#include <new // for bad_alloc

using namespace std;
//private:{Queue::Queue(const Queue& Q)}
Queue::Queue() : backPtr(0), frontPtr(0)
{
} // end default constructor

Queue::Queue(const Queue& Q) throw(OutOfStorageException)
{


/////////// Implementation
here!!!!!!///////////////


} // 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 28 '07 #1
2 2672
On 2007-10-28 18:13, ecestd wrote:
how do you implement a copy constructor for this pointer-based ADT
queue

#include <cassert // for assert
#include <new // for bad_alloc

using namespace std;
//private:{Queue::Queue(const Queue& Q)}
Queue::Queue() : backPtr(0), frontPtr(0)
{
} // end default constructor

Queue::Queue(const Queue& Q) throw(OutOfStorageException)
{
Use Q.frontPtr to get the fist node in the other queue and start copying
elements from there.
void Queue::dequeue(QueueItemType& queueFront)
I do not see the point of this function, the exception safe way is to
use a combination of getFront() and dequeue() to remove elements from
the queue.
void Queue::getFront(QueueItemType& queueFront) const
Make this one return the first object instead of returning it through a
reference (or at the very least make the parameter a pointer instead of
a reference).

--
Erik Wikström
Oct 28 '07 #2
On Oct 28, 12:47 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-10-28 18:13, ecestd wrote:
how do you implement a copy constructor for this pointer-based ADT
queue
#include <cassert // for assert
#include <new // for bad_alloc
using namespace std;
//private:{Queue::Queue(const Queue& Q)}
Queue::Queue() : backPtr(0), frontPtr(0)
{
} // end default constructor
Queue::Queue(const Queue& Q) throw(OutOfStorageException)


///////////
what to put here so that it copies. I left this blank coz this is
where it has to be implemented. The program runs but then it crashes
if you type a string. Also what is the significance of cerr<<"type
whatever you want" <<endl? . The prorum after it runs it breaks at
this point tempPtr->next = 0; // defensive strategy
delete tempPtr; What could be the problem?

Use Q.frontPtr to get the fist node in the other queue and start copying
elements from there.
void Queue::dequeue(QueueItemType& queueFront)

I do not see the point of this function, the exception safe way is to
use a combination of getFront() and dequeue() to remove elements from
the queue.
void Queue::getFront(QueueItemType& queueFront) const

Make this one return the first object instead of returning it through a
reference (or at the very least make the parameter a pointer instead of
a reference).

--
Erik Wikström

Oct 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gandalf | last post: by
42 posts views Thread by Edward Diener | last post: by
7 posts views Thread by vj | last post: by
4 posts views Thread by Peter | last post: by
139 posts views Thread by ravi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.