472,973 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

operator overloading and destructor problem

Hi,

Please see below a piece of code. Here I am trying to create a linked
list by attaching two linked list together. I have overloaded operator
+ for this. Now the output always says that the list is empty. I added
copy constructor and overloaded assignment operator but still the
behavior remain same. Can anyone guide me on this ?

Thanks
Ganesh

class LinkedList
{
private:
class Node
{
public:
int data;
Node * Next;
Node()
{
data = 0;
Next = NULL;
}
};
struct Node * Head;
public :
int AddNode(int data);
int RemoveNode(int data);
void Display(void);
void Reverse(void);

LinkedList(LinkedList &oldlist)
{
if(oldlist.Head != NULL)
{
Head = new Node;
Head->data = oldlist.Head->data;

if(oldlist.Head->Next != NULL)
{
Node * tmpoldlist = oldlist.Head->Next;
Node * tmpprevnewlist = Head;
Node * tmpnextnewlist = tmpprevnewlist;

while(tmpoldlist != NULL)
{
tmpnextnewlist = new Node;
tmpnextnewlist->data = tmpoldlist->data;
tmpnextnewlist->Next = NULL;
tmpprevnewlist->Next = tmpnextnewlist;
tmpprevnewlist = tmpnextnewlist;
tmpoldlist = tmpoldlist->Next;
}
}
}
}

LinkedList operator=(LinkedList oldlist)
{
LinkedList mylist;

if(oldlist.Head != NULL)
{
mylist.Head = new Node;
mylist.Head->data = oldlist.Head->data;

if(oldlist.Head->Next != NULL)
{
Node * tmpoldlist = oldlist.Head->Next;
Node * tmpprevnewlist = mylist.Head;
Node * tmpnextnewlist = tmpprevnewlist;

while(tmpoldlist != NULL)
{
tmpnextnewlist = new Node;
tmpnextnewlist->data = tmpoldlist->data;
tmpnextnewlist->Next = NULL;
tmpprevnewlist->Next = tmpnextnewlist;
tmpprevnewlist = tmpnextnewlist;
tmpoldlist = tmpoldlist->Next;
}
}
}
return mylist;
}

LinkedList operator+(LinkedList tlist4)
{
LinkedList tlist3;
Node * tmpNode = NULL, *tmpNode1 = NULL;

if(Head != NULL)
{
tmpNode = Head;
do
{
tlist3.AddNode(tmpNode->data);
tmpNode = tmpNode->Next;
}
while(tmpNode != NULL);
}

tmpNode = NULL;

if(tlist4.Head != NULL)
{
tmpNode1 = tlist4.Head;
do
{
tlist3.AddNode(tmpNode1->data);
tmpNode1 = tmpNode1->Next;
}
while(tmpNode1 != NULL);
}

return tlist3;
}

LinkedList()
{
Head = NULL;
}

~LinkedList()
{
Node * old;
while(Head != NULL)
{
old = Head;
if(Head->Next != NULL)
{
Head = Head->Next;
}
else
{
break;
}
delete(old);
}
delete Head;
}
};

Jul 23 '05 #1
3 3009
On 2005-06-04, ga***********@gmail.com <ga***********@gmail.com> wrote:
Hi,

Please see below a piece of code. Here I am trying to create a linked
list by attaching two linked list together. I have overloaded operator
+ for this.


You don't have the AddNode method, so your code will not compile.

You have a lot of special casing. Why do this ? The code for operator+
could look like this:

LinkedList operator+ (const LinkedList& L){
LinkedList result(*this);
for (const Node* n = L.Head; n; n = n->Next)
result.AddNode(n->data);
return result;
}

This version is simpler than yours, right ? 4 lines of code against however
many yours uses. That's the number one reason your code has bugs. You make
it unnecessarily complex, which increases the chance you'll mess something up.

BTW, don't pass objects by value if const reference will do.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #2
On 4 Jun 2005 11:10:05 -0700, ga***********@gmail.com wrote:
Hi,

Please see below a piece of code. Here I am trying to create a linked
list by attaching two linked list together. I have overloaded operator
+ for this. Now the output always says that the list is empty. I added
copy constructor and overloaded assignment operator but still the
behavior remain same. Can anyone guide me on this ?
Yes ... use std::list instead!

I didn't even try to debug your code, there are so many beginner's
mistakes in it. You really need to read Scott Meyers books "Effective
C++" and "More Effective C++", and especially Stroustrup's "The C++
Programming Language".

Here are some comments interspersed within your code:
class LinkedList
{
private:
// class Node
// (everything is public, so struct is clearer)
struct Node
{
int data;
Node * Next;
Node()
// prefer an initialization list:
// data(0), Next(0)
{
data = 0;
Next = NULL;
}
};
Node * Head;
public :
int AddNode(int data);
int RemoveNode(int data);
void Display(void); // void argument is superfluous
// that probably should be:
// void Display() const;
void Reverse(void);
// void Reverse();

LinkedList(LinkedList &oldlist) // copy ctor should take LinkedList const &
{
if(oldlist.Head != NULL)
// if (oldlist.Head) is better style
{
Head = new Node;
Head->data = oldlist.Head->data;

if(oldlist.Head->Next != NULL)
{
Node * tmpoldlist = oldlist.Head->Next;
Node * tmpprevnewlist = Head;
Node * tmpnextnewlist = tmpprevnewlist; // = Head ???

while(tmpoldlist != NULL)
// while(tmpoldlist)
{
tmpnextnewlist = new Node; // what about the previous assignment??
tmpnextnewlist->data = tmpoldlist->data;
tmpnextnewlist->Next = NULL; // prefer 0 to the NULL macro
tmpprevnewlist->Next = tmpnextnewlist;
tmpprevnewlist = tmpnextnewlist; // huh ??
tmpoldlist = tmpoldlist->Next;
}
}
}
}
// copy ctor's canonical signature is:
// LinkedList & operator=(LinkedList const & oldlist)
LinkedList operator=(LinkedList oldlist) // should take LinkedList const &;
// you are creating two unnecessary
// temporary objects here!!
{
// guard against self-assignment here!!
// if (this != &oldlist)
// {
LinkedList mylist;

if(oldlist.Head != NULL)
{
mylist.Head = new Node;
mylist.Head->data = oldlist.Head->data;

if(oldlist.Head->Next != NULL)
{
Node * tmpoldlist = oldlist.Head->Next;
Node * tmpprevnewlist = mylist.Head;
Node * tmpnextnewlist = tmpprevnewlist;

while(tmpoldlist != NULL)
{
tmpnextnewlist = new Node;
tmpnextnewlist->data = tmpoldlist->data;
tmpnextnewlist->Next = NULL;
tmpprevnewlist->Next = tmpnextnewlist;
tmpprevnewlist = tmpnextnewlist;
tmpoldlist = tmpoldlist->Next;
}
}
}
// }
return mylist;
}

LinkedList operator+(LinkedList tlist4)
// this should not be a member function ...
// ... but if it is, it should be const ...
// ... and its argument should be const & in either case.
{
LinkedList tlist3;
Node * tmpNode = NULL, *tmpNode1 = NULL;

if(Head != NULL)
{
tmpNode = Head;
do
{
tlist3.AddNode(tmpNode->data);
tmpNode = tmpNode->Next;
}
while(tmpNode != NULL);
}

tmpNode = NULL;

if(tlist4.Head != NULL)
{
tmpNode1 = tlist4.Head;
do
{
tlist3.AddNode(tmpNode1->data);
tmpNode1 = tmpNode1->Next;
}
while(tmpNode1 != NULL);
}

return tlist3;
}

LinkedList() // : Head(0)
{
Head = NULL;
}

~LinkedList()
{
Node * old;
while(Head != NULL)
{
old = Head;
if(Head->Next != NULL)
{
Head = Head->Next;
}
else // unnecessary else
{
break;
}
delete(old); // delete old;
}
delete Head;
}
};


--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #3
On Sat, 04 Jun 2005 21:46:36 +0200, Bob Hairgrove
<in*****@bigfoot.com> wrote:
// copy ctor's canonical signature is:
// LinkedList & operator=(LinkedList const & oldlist)


Of course, this should read: "assignment operator", not "copy ctor"

--
Bob Hairgrove
No**********@Home.com
Jul 23 '05 #4

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

Similar topics

6
by: - Steve - | last post by:
If you want to see all the code it's at http://planetevans.com/c However I think I have all the relevant parts here. main() makes the following call IntArray c = a + b; // IntArray is my...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
4
by: hjast | last post by:
I'm trying to implement a = operater to set one circle or rectangle to another and this is giving me all kind of bugs. #include <iostream.h> class Shape { private: int x_Center,...
3
schmals
by: schmals | last post by:
I am building a bigint class and currently having an issue with overloading operators simple +, -, and * operators with my destructor. The bigint class contains a dynamically allocated array of...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.