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 7754887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:25:38+00:00 2026-06-01T12:25:38+00:00

I need to implement an auxilliary function, named copyList, having one parameter, a pointer

  • 0

I need to implement an auxilliary function, named copyList, having one parameter, a pointer to a ListNode. This function needs to return a pointer to the first node of a copy of original linked list. So, in other words, I need to code a function in C++ that takes a header node of a linked list and copies that entire linked list, returning a pointer to the new header node. I need help implementing this function and this is what I have right now.

Listnode *SortedList::copyList(Listnode *L) {

    Listnode *current = L;  //holds the current node

    Listnode *copy = new Listnode;
    copy->next = NULL;

    //traverses the list
    while (current != NULL) {
       *(copy->student) = *(current->student);
       *(copy->next) = *(current->next);

        copy = copy->next;
        current = current->next;
    }
    return copy;
}

Also, this is the Listnode structure I am working with:

struct Listnode {    
  Student *student;
  Listnode *next;
};

Note: another factor I am running into with this function is the idea of returning a pointer to a local variable.

  • 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-01T12:25:39+00:00Added an answer on June 1, 2026 at 12:25 pm

    The first question you need to ask yourself is what the copy semantics are. In particular, you’re using a Student* as node contents. What does copying node contents mean? Should we copy the pointer so that the two lists will point to (share) the same student instances, or should you perform a deep copy?

    struct Listnode {    
      Student *student; // a pointer?  shouldn't this be a `Student` object?
      Listnode *next;
    };
    

    The next question you should ask yourself is how you will allocate the nodes for the second list. Currently, you only allocate 1 node in the copy.

    I think you code should look more like:

    Listnode *SortedList::copyList(Listnode *L) {
    
        Listnode *current = L;
    
        // Assume the list contains at least 1 student.
        Listnode *copy = new Listnode;
        copy->student = new Student(*current->student);
        copy->next = NULL;
    
        // Keep track of first element of the copy.
        Listnode *const head = copy;
    
        // 1st element already copied.
        current = current->next;
    
        while (current != NULL) {
           // Allocate the next node and advance `copy` to the element being copied.
           copy = copy->next = new Listnode;
    
           // Copy the node contents; don't share references to students.
           copy->student = new Student(*current->student);
    
           // No next element (yet).
           copy->next = NULL;
    
           // Advance 'current' to the next element
           current = current->next;
        }
    
        // Return pointer to first (not last) element.
        return head;
    }
    

    If you prefer sharing student instances between the two lists, you can use

    copy->student = current->student;
    

    instead of

    copy->student = new Student(*current->student);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a function that takes as a parameter a list of
I need to implement a 4-to-1 function in Veriog. The input is 4 bits,
When the class with a member which is a pointer, we need implement a
I need to implement a web application hosted on sharepoint. This is a client
I need to implement the website visitor count Usercontrol.Can any one help me regarding
I need to implement Settings Layout for my application. Generally this should be ListView
I need to implement a hash table and hash function using JavaScript. The goal
I need to implement a step function (i.e. piecewise constant). There are a few
Need help implement this scenario in Hibernate inheritance Cow and Dog are inheriting Animal
I need to implement a C# method that needs to validate an XML against

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.