G
ganesh.tambat
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;
}
};
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;
}
};