473,385 Members | 1,780 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,385 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 3026
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.