473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 2718

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
4
by: Macca | last post by:
Hi, I've been reading the online documentation for the System.Collections.Queue object on how to implement a threadsafe Queue object. I understand the basics, by using the wrapper returned by...
4
by: pat | last post by:
Hi, I have been asked for an exam question to implement the constructor, copy constructor and destructor for the following class that describes an n-by-n matrix containing n squared integer...
15
by: Brett Wickard | last post by:
Ok, this should be simple, but how do I copy a reference type by value? Meaning SomeComplexObject s1 = new SomeComplexObject (); SomeComplexObject s2 = new SomeComplexObject (); s1.color =...
0
by: april86 | last post by:
Hello everyone! I am new to C# and i have a question: I need to copy one Queue to another and i tried this: public static void copyQueues(Queue q, Queue q_copy) { Array...
4
by: TwistedPair | last post by:
All, Been trying to get a little program to work, and I think I'm really close, but I'm afraid I need to be pointed in the right direction. The code below will watch a directory for files...
2
by: ecestd | last post by:
how do you implement a copy constructor for this pointer-based ADT queue #include "Queuep.h" #include <cassert> #include <new> using namespace std; Queue::Queue () : backPtr (0), frontPtr(0)...
2
by: ecestd | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.