Hey all!

I am having a problem copying Linked Lists.

My Struct has the following fields.
list->data
list->number
list->exit

Exit is the tail, data is the head.

I tried doing this..

copyList->data = list->data
but it resulted in a fail of epic proportions.

Can anyone give me a hand please?

If this is a list of your own making then you have to iterate each node in the original list, allocate memory for a new node and attach the new node to the new list. Sorry but there is not much more to tell you without the details of the nodes (the structure that represents each node)

If this is a list of your own making then you have to iterate each node in the original list, allocate memory for a new node and attach the new node to the new list. Sorry but there is not much more to tell you without the details of the nodes (the structure that represents each node)

OK if the values of the first list that are being copied in have to be temporarily stored:
How can I copy String from and To my lists?

Also..

How will my 2nd list know when to make a link to the next list?

struct Node
{
    char* data;
    int number;
    Node* next;
};

void listCopy( Node* list, Node*& listCopy )
{
/***** BEGIN STUDENT CODE *******/










/***** END STUDENT CODE *******/
}

is what our Prof has left us.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

I assume the first parameter is a pointer to the head of the original list, and the second parameter is a reference to the pointer of the new list. If that is true, then do something like this:

NOTE: I did not compile or test this, so don't be supprised it it contains one or more bugs. This will give you a general idea how to go about making a copy of the linked list.

Node* newnode;
Node* tail = NULL;
while( list )
{
    newnode = new Node;
    // what is data?  just a character pointer to a string? If yes, then this will work
    newnode->data = new char[strlen(list->data) + 1];
    strcpy(newnode->data,list->data);
    newnode->number = list->number;

    newnode->next = NULL;
    // now add newnode to listCopy
    if( *listCopy == NULL)
    {
          // empty list
          listCopy = newnode;    
          tail = newnode;
    }
    else
    {
         // not empty list, so add new node at tail
         tail->next = newnode;
        tail = newnode;
   }
   list = list->next; 
}

Thank you! It works.

I was wondering if you could help me to understand Linked lists by explaining what you did and when you did it?

A list is bunch of nodes related to each other by the fact that each node can point to the next node in the list, though there may not be another node to point to, and, indeed, there may not be any nodes at all in the list. The variables list and listCopy represent the first nodes in two lists. newNode represents, well, a new node. (NOTE: you should assign NULL to listCopy when it is declared because it is really a pointer to a node and it isn't pointing to anything before it is sent to the function to be used.) The information in every node in list will be copied into newNode one at a time and then newNode will be added to the end of listCopy. The last node of the copy of list will be represented by the variable called tail. All of the work will be done in the function called copyList() (NOTE: I don't think you should have a function and a variable called the same name so I am going to call the function copyList() and the first node of the copy of the list I'll still call listCopy.)

To do this you pass a copy of the first node in list (NOTE: I'm going to call the copy of the first node in list current rather than list so it's clear we are use a copy of the first node in the original list, not the actual first node in the original list) to copyList() and a reference to the address of the first node in listCopy to copyList(). (Note: since the address of listCopy will change in copyList() you need to use the first node in the copy of the list, that is listCopy, not a copy of the first node). The copying will be done as long as current isn't NULL. First you get an address of a new node to store in newNode so it can be added to the copy of the original list. Then you copy the values of data and number from current into newNode and assign NULL to newNode->next. Then you link newNode to the end of the copy of list. To do that you check if listCopy actually has an address or not. If it doesn't, that is, if *listCopy is NULL, you assign newNode to listCopy and tail. (NOTE you only use listCopy once in the whole process using this algorhithm). If it isn't, then you assign newNode to just tail->next to copy newNode to the copy of the original list and then assign newNode to tail so tail is updated as the last node in the copy of the original list. Finally, you go the next node of the original list by assigning current->next to current. When current becomes NULL because you've copied all the nodes in list, then you're done.

This article has been dead for over six months. Start a new discussion instead.