473,666 Members | 2,634 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(co nst Queue& Q) throw(OutOfStor ageException)
{


/////////// 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(OutOfStor ageException)
{
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 OutOfStorageExc eption("Memory allocation failed.");
} // end try/catch
} // end enqueue

void Queue::dequeue( ) throw(OutOfData Exception)
{
if (isEmpty() )
{
throw OutOfDataExcept ion("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(OutOfData Exception)
{
if (isEmpty() )
{
throw OutOfDataExcept ion("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(OutOfData Exception)
{
if (isEmpty() )
{
throw OutOfDataExcept ion("Empty queue, cannot getFront");
}
else
{
// queue is not empty; retrieve front
queueFront = frontPtr->item;
} // end if
} // end getFront

Oct 28 '07 #1
2 2927
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(co nst Queue& Q) throw(OutOfStor ageException)
{
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(co nst Queue& Q) throw(OutOfStor ageException)


///////////
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3369
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;
42
5758
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
7
1596
by: vj | last post by:
Hi! I recently came across this intresting behaviour shown by Visual C++ 6.0 compiler regarding Copy Constructors. Please tell me that is this the standard behaviour shown by all compilers or its limited only to VC++. Listing 1 ================== #include <iostream> using namespace std;
4
17183
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look like? (The background is I don't know (I don't care indeed) all members in DataGrid, so I don't want to copy all members in DataGrid one by one.) public class GridEx : DataGrid { public GridEx()
0
1167
by: olsongt | last post by:
This one made me smile. From: http://aima.cs.berkeley.edu/python/utils.html#Queue class Queue: """Queue is an abstract class/interface. There are three types: Stack(): A Last In First Out Queue. FIFOQueue(): A First In First Out Queue. PriorityQueue(lt): Queue where items are sorted by lt, (default <).
139
14144
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
2
2333
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) { }
5
7971
by: amitmool | last post by:
hi, i have used the queue library file and try to use the template as template <class QueueItem> queue <QueueItem>::~queue() // line 25 { } template <class QueueItem> void queue<QueueItem>::push(const QueueItem& entry) // line 42
16
4178
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a private variable which is a priority queue. In class foo's header file, I declared it as: class foo { private:
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8780
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7378
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.