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

  • SEARCH
  • Home
  • 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 8831985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:25:46+00:00 2026-06-14T08:25:46+00:00

I have a linked list class and I have trouble with two things: my

  • 0

I have a linked list class and I have trouble with two things:

  1. my copy assignment operator doesn’t copy the exact list
  2. I have no idea how to do the overloaded output with the linked list, I know how it works in basics, but I’m lost with my linked list.

UPDATE:
why is the copy assignment operator giving me reverse of my list?

list& list::operator =(const list &l){
    while (p!=NULL)
       del();
    Node* current=l.p;
    while(current!=NULL){
       insert(current->x); 
       current=current->next;
       }

when i have 10–2–NULL
It prints 2–10–NULL

  • 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-14T08:25:48+00:00Added an answer on June 14, 2026 at 8:25 am

    At the end of your assignment operator you set p to current which must be a null pointer, or you wouldn’t have exited the while loop. p should already be set by the previous calls to insert.

    What happens if the list already has elements when you assign to it? Do you really want to append, or do you want to remove the existing elements first?

    In your print member the loop runs until q is NULL, then after it’s finished you test whether q is NULL … of course it is. So you always print NULL after any list.

    In main you have this, which destroys the list:

    l1.~list();
    

    So the next line is undefined behaviour, because you access an object that has already been destroyed, then the object goes out of scope again and its destructor runs, which is also undefined behaviour because the object has already been destroyed. You don’t need to call a destructor manually, that’s the whole point of destructors, they clean up automatically when the object goes out of scope.

    Edit: Your assignment operator copies the list in reverse because you go through it in order but insert puts each element at the start of the copied list, so you copy the first element, then put the second element before it, then put the third before that etc.

    To copy the list you need to put the copied elements in the same order, i.e. insert each one at the end not the beginning.

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

Sidebar

Related Questions

So I want to copy a whole linked list classes, I have trouble figuring
I have the following function defined inside my linked list class. The declaration in
I have a linked list class that stores a linked collection of entities. I've
I have implemented a simple linked list class and I would now like to
What I want to do is have a singly linked-list class with a default
I have been trying to implement my own linked list class for didactic purposes.
So basically I have a Linked List class that has all of the constructor
Hi I am writing a linked list data type. I have an inner class
This is an assignment. I have to create a circular linked list and remove
I have a linked list of Foo objects. Foo is a base class, which

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.