Home > Back-end >  How do I make a copy constructor for a forward list?
How do I make a copy constructor for a forward list?

Time:04-09

For my C class assignment, I have to implement a copy constructor for a forward list. However, there are instructions in the assignment that confuse me. Specifically, the part saying that I must create a new node for each value in the forward list. I am unsure of what it wants me to do exactly. If I create nodes that store the values, wouldn't that be the same as making a linked list?

I have no idea what a copy constructor would look like for a forward list, so can someone give me some pointers to where I can learn how?

// Copy constructor
// ***For you to implement

// The copy constructor takes as argument a const reference to a 
// another Forward_list "other" 
// The const means that the function should not modify other
// The function should make a "deep copy" of the other list,
// that is create a new node for every node in other and copy 
// the data of other into these new nodes.  
template <typename T>
Forward_list<T>::Forward_list(const Forward_list& other)
{
    
}

CodePudding user response:

if I create nodes that store the values wouldn't that be the same as making a linked list?

Yes. The purpose of a copy constructor of a linked list is to make a copy of the linked list that is being passed into the constructor.

Isn't the other list just a forward list, then why would I need to create nodes for it?

You aren't creating nodes for the other list. You are creating nodes for the list that you are constructing.

Wouldn't that be just creating a linked list copy of the forward list?

Yes. A copy constructor should create a copy.

CodePudding user response:

if I create nodes that store the values wouldn't that be the same as making a linked list?

Yes. A forward list is a single-linked list, which means you can move through the list in only one direction - forward, because each node only has a link to the next node in the list, not to the previous node. Whereas in a double-linked list, each node has both links, so you can move through the list in two directions.

I have no idea what a copy constructor would look like for a forward list

Just like for a double-linked list, but with 1 less field to initialize per node (a pointer to the previous node, which doesn't exist in a forward list). For example:

template <typename T>
Forward_list<T>::Forward_list(const Forward_list& other)
{
    head = NULL;
    node *previousNode = NULL;
    for(node *otherNode = other.head; otherNode != NULL; otherNode = otherNode->next)
    {
        node *newNode = new node;
        newNode->value = otherNode->value;
        newNode->next = NULL;
        if (!head)
            head = newNode;
        else
            previousNode->next = newNode;
        previousNode = newNode;
    }
}

Alternatively:

template <typename T>
Forward_list<T>::Forward_list(const Forward_list& other)
{
    head = NULL;
    node **newNode = &head;
    for(node *otherNode = other.head; otherNode != NULL; otherNode = otherNode->next)
    {
        *newNode = new node;
        (*newNode)->value = otherNode->value;
        (*newNode)->next = NULL;
        newNode = &((*newNode)->next);
    }
}
  • Related