Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8695985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:10:19+00:00 2026-06-13T01:10:19+00:00

So, I’ve been on the process of making a linked list class, and right

  • 0

So, I’ve been on the process of making a linked list class, and right now I’m trying to make it so that two lists can be united through simple a + operator. Here is the current code, whole:

#include <iostream>
using namespace std;

// ---/ List Class /--- //

template <class Type>
struct Node {
    Type core;
    Node<Type> *next;
};

template <class Type>
class List {
    public:
        Node<Type> *start, *end;
        unsigned int siz;

        static const int END;

    public:
        // Genesis
        List() {
            start = NULL;
            end = NULL;

            siz = 0;
        };

        // ---
        void push (const Type&, const int&);

        // DEBUG
        void show (void);

        // +-*/
        List operator+ (const List&);
        List& operator= (const List&);
        List& operator+= (const List&);

        // Abbadon
        ~List() {
            delete start;
            delete end;
        };
};

template <class Type>
const int List<Type>::END = -1;

// ---

template <class Type>
void List<Type>::push (const Type& elem, const int& pos = END) {
    Node<Type> *aux;

    aux = new Node<Type>;
    aux->core = elem;

    if (siz == 0) {
        aux->next = NULL;

        start = aux;
        end = aux;
    }
    else {
        if (pos == END) {
            aux->next = NULL;

            end->next = aux;
            end = end->next;
        }
        else if (pos == 0) {
            aux->next = start;

            start = aux;
        }
        else {
            Node<Type> *pesq = start;
            for (int i = 1; (i < pos) && (pesq->next != NULL); i++) {
                pesq = pesq->next;
            }

            aux->next = pesq->next;
            pesq->next = aux;
        }
    }

    siz++;
}

// DEBUG

template <class Type>
void List<Type>::show (void) {
    Node<Type> *pesq = start;
    while (pesq != NULL) {
        cout << pesq->core << endl;
        pesq = pesq->next;
    }

    cin.get();
}

// +-*/

template <class Type>
List<Type> List<Type>::operator+ (const List<Type>& nimda) {
    List<Type> aux = *this;

    aux.end->next = nimda.start;
    aux.end = nimda.end;

    aux.siz += nimda.siz;

    return aux;
}

template <class Type>
List<Type>& List<Type>::operator= (const List<Type>& nimda) {
    if (&nimda != this) {
        start = nimda.start;
        end = nimda.start;
        siz = nimda.siz;
    }
} 

template <class Type>
List<Type>& List<Type>::operator+= (const List<Type>& nimda) {
    *this = *this + nimda;  
    return *this;
}

// ---/ MAIN() /--- //

int main() {
    List<int> adabo;
    List<int> inakos;

    adabo.push(1);
    adabo.push(2);

    inakos.push(3);
    inakos.push(4);

    adabo = adabo + inakos;

    adabo.show();
}

Here are the overloaded operators:

template <class Type>
List<Type> List<Type>::operator+ (const List<Type>& nimda) {
    List<Type> aux = *this;

    aux.end->next = nimda.start;
    aux.end = nimda.end;

    aux.siz += nimda.siz;

    return aux;
}

template <class Type>
List<Type>& List<Type>::operator= (const List<Type>& nimda) {
    if (&nimda != this) {
        start = nimda.start;
        end = nimda.start;
        siz = nimda.siz;
    }
} 

template <class Type>
List<Type>& List<Type>::operator+= (const List<Type>& nimda) {
    *this = *this + nimda;  
    return *this;
}

And here is the main(), for testing:

// ---/ MAIN() /--- //

int main() {
    List<int> adabo;
    List<int> inakos;
    List<int> foo;

    adabo.push(1);
    adabo.push(2);

    inakos.push(3);
    inakos.push(4);

    foo = adabo + inakos;

    foo.show();
}

Its output would supposedly be the values 1, 2, 3, and 4, and it indeed happens when I put the show function in the operator of + before aux returns:

template <class Type>
List<Type> List<Type>::operator+ (const List<Type>& nimda) {
    List<Type> aux = *this;

    aux.end->next = nimda.start;
    aux.end = nimda.end;

    aux.siz += nimda.siz;

    aux.show() // Putting it here shows everything as expected

    return aux;
}

It seems, however, that ‘aux’ is lost somewhere between its return of the function and the actual value received by ‘foo’ in main(), causing foo.show() to display random data endlessly.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:10:20+00:00Added an answer on June 13, 2026 at 1:10 am

    Nevermind, found the problem!

    What was happening was that operator= was making a ‘duplicate’, if one so can say, of the List to be passed over. The ‘duplicate’ List, when modified, would modify the original List as well, so in here…

    template <class Type>
    List<Type> List<Type>::operator+ (const List<Type>& nimda) {
        List<Type> aux = *this; // ...aux, which would become a duplicate of the class...
    
        aux.end->next = nimda.start;
        aux.end = nimda.end;
    
        aux.siz += nimda.siz;
    
        return aux;
    } // ...when destructed here, would destruct *this just as well, making all data be lost.
    

    To solve this problem, instead of blandly passing over the pointers in the overloaded =…

    template <class Type>
    List<Type>& List<Type>::operator= (const List<Type>& nimda) {
        if (&nimda != this) {
            start = nimda.start;
            end = nimda.start;
            siz = nimda.siz;
        }
    } 
    

    …now it is made so that each element of the List is copied one by one:

    template <class Type>
    List<Type>& List<Type>::operator= (const List<Type>& nimda) {
        if (&nimda != this) {
            kill();
    
            Node<Type> *aux = nimda.start;
            while (aux != NULL) {
                push(aux->core);
                aux = aux->next;
            }
        }
    }
    

    This way, the receiving List gets a copy of all the elements of the original List without becoming itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.