operator+ , Linked list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • APEJMAN
    New Member
    • Feb 2008
    • 33

    operator+ , Linked list

    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?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You would override

    [CODE=cpp]List& List::operator+ (const List& rightHandSide);[/CODE]

    with the appropriate code.

    Now, how can you get the last element of the List to point to the first element of rightHandSide?

    Comment

    • APEJMAN
      New Member
      • Feb 2008
      • 33

      #3
      Originally posted by Ganon11
      You would override

      [CODE=cpp]List& List::operator+ (const List& rightHandSide);[/CODE]

      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

      Comment

      • APEJMAN
        New Member
        • Feb 2008
        • 33

        #4
        Operator + Linked LIst

        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+lis t2;

        any idea?
        Thanks

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          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.

          Comment

          • APEJMAN
            New Member
            • Feb 2008
            • 33

            #6
            we dont need to move any thing
            just to make this work
            link3=link1+lin k2

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

            any idea?
            thanks

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              How are you confused? With which part? To help you, we need a little more description than "I don't know."

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                • APEJMAN
                  New Member
                  • Feb 2008
                  • 33

                  #9
                  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_bac k(right[i]);
                  return result;
                  }

                  Comment

                  Working...