468,491 Members | 2,072 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

operator+ , Linked list

33
I have two linked list and I want to Concatenates these two linked lists, placing the elements of the second list after the elements of the first list. For example, if list1={1,2,3} and list2={4,5,6} then calling:

list3 = list1 + list2;
would result in list3={1,2,3,4,5,6}.

so I want to define operator+ for these linked list
any idea how I can do this?
Feb 13 '08 #1
8 4370
Ganon11
3,652 Expert 2GB
You would override

Expand|Select|Wrap|Line Numbers
  1. List& List::operator+(const List& rightHandSide);
with the appropriate code.

Now, how can you get the last element of the List to point to the first element of rightHandSide?
Feb 13 '08 #2
APEJMAN
33
You would override

Expand|Select|Wrap|Line Numbers
  1. List& List::operator+(const List& rightHandSide);
with the appropriate code.

Now, how can you get the last element of the List to point to the first element of rightHandSide?
I am confused
I dont know
Feb 13 '08 #3
APEJMAN
33
Lets say I have

//Operator + , concatenates two linked lists
template <typename T>
LinkedList<T>& LinkedList<T>::operator+ (const LinkedList<T>& right)
{

}

I am trying to write the + operator so that I can add two linked list together like
list3=list1+list2;

any idea?
Thanks
Feb 14 '08 #4
Laharl
849 Expert 512MB
Think about it in terms of moving the pointers in the list. Which pointer(s) would need to be moved?

EDIT: Please don't post two threads on the same topic.
Feb 14 '08 #5
APEJMAN
33
we dont need to move any thing
just to make this work
link3=link1+link2

the declaration for the link list is:
Node<T>* newNode=new Node<T>(value)

any idea?
thanks
Feb 14 '08 #6
Ganon11
3,652 Expert 2GB
How are you confused? With which part? To help you, we need a little more description than "I don't know."
Feb 14 '08 #7
Ganon11
3,652 Expert 2GB
As Laharl said, please don't make two threads on the same topic. It doesn't help you get your help faster, it only makes things very confusing for the people trying to help you - thus causing them not to want to help. In short, you only need one thread for your topic.
Feb 14 '08 #8
APEJMAN
33
I fixed it
here is the code
this can add 2 linked list to gether

template <typename T>
LinkedList<T> LinkedList<T>::operator+ (const LinkedList<T>& right)
{
LinkedList result(*this);
for (int i=0 ; i<right.size() ; i++)
result.push_back(right[i]);
return result;
}
Feb 14 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ganesh.tambat | last post: by
5 posts views Thread by Dream Catcher | last post: by
6 posts views Thread by Steve Lambert | last post: by
16 posts views Thread by EM.Bateman | last post: by
reply views Thread by Atos | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.