1

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 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.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Tanya Tazzy Hegarty
  • 111
  • 3
  • 6
  • 15

2 Answers2

4

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.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • sorry but im a bit simple, i understand your code but I was wondering if you could expand please? Im not sure what your getting at, not your fault, my brain and im a beginning lol – Tanya Tazzy Hegarty May 02 '11 at 11:18
  • 1
    @Tanya image a linked list object. It just holds a pointer to the beginning of a linked list. To make a copy of that linked list **so that modifying one doesn't modify the other one**, you have to go to each item of the linked list and make a _new copy_ of that item, and add it to the new linked list you're making. – Seth Carnegie May 02 '11 at 19:10
1

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

Community
  • 1
  • 1
Mahesh
  • 34,573
  • 20
  • 89
  • 115