开发者

Copy Constructor for Simple Single Linked List including Nodes C++

开发者 https://www.devze.com 2023-03-01 18:24 出处:网络
I\'m not very good at this, and I am a bit stuck making the copy constructor for a single linked list and the nodes 开发者_StackOverflow社区that go with it.

I'm not very good at this, and I am a bit stuck making the copy constructor for a single linked list and the nodes 开发者_StackOverflow社区that go with it.

Here is my header file:

#pragma once

#include <iostream>
using namespace std;

class Node
{
public:
    int data;
    Node* next;
    Node()
    {
        next = NULL;
        data = 0;
    }
    Node(const Node& copyNode); /*: data(copyNode.data), next(copyNode.next ? new Node(*copyNode.next) : NULL)*/

};

class SLLIntStorage
{
public:
    Node* head;
    Node* current;
    Node* tail;

    void Read(istream&);
    void Write(ostream&);
    void setReadSort(bool);
    void sortOwn();
    void print();

    void mergeSort(Node**);
    Node *merge(Node*, Node*);
    void split(Node*, Node**, Node**);

    bool _sortRead;
    int numberOfInts;

    SLLIntStorage(const SLLIntStorage& copying) //: head(copying.head ? new Node(*copying.head) : NULL)
    {

    }

    SLLIntStorage(void);
    ~SLLIntStorage(void);
};

inline ostream& operator<< (ostream& out, SLLIntStorage& n) 
{
    n.Write(out); 
    return out;
}
inline istream& operator>> (istream& in, SLLIntStorage& s) 
{
    s.Read(in); 
    return in;
}

Could anyone give me a hand on understanding how this works and what I could do to create it? Thank you.


To copy a linked list, you must iterate the entire linked list and make a copy of each of the nodes, and append that to the new list. Remember that you don't just copy the pointers, but you must copy the entire Node structure and any data that needs copying as well (e.g. if the datas are pointers, you'll need to do deep copying on those too).

So here's an example copy constructor for your SLLIntStorage class:

SLLIntStorage(const SLLIntStorage& copying) : head(NULL)
{
    Node* cur = copying.head;
    Node* end = NULL;

    while (cur)
    {
        Node* n = new Node;
        n->data = cur->data;

        if (!head) {
            head = n;
            end = head;
        } else {
            end->next = n;
            end = n;
        }

        cur = cur->next;
    }
}

Note that I didn't take into account the tail and current data members, etc. You'll have to account for those.


As it is homework, I will try to give the idea from which you can figure out what you need to do with the copy constructors.

Node(const Node& copyNode) : data(copyNode.data), 
                             next(copyNode.next)
{
    // ....
}

In the above snippet you are just actually making the next to point the location copyNode::next is pointing to. So, you run in to problems when any of the pointer deallocates the resource it is pointing to leaving the other dangling.

So, you should make the pointer next each instance to point to a location it independently holds. So, -

Node(const Node& copyNode) : data(copyNode.data), 
                             next(new Node)
{
    (*next) = *(copyNode.next) ;
    // ....
}  

Also read this thread which has an excellent explanation - Rule of Three

0

精彩评论

暂无评论...
验证码 换一张
取 消