Linked list Copy Constructor


Copy constructor is not working... it crashes at the mentioned place.

what can possibly be wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
template <class T>
class Node
{
public:
    T info;
    Node<T> * next;
public:

    Node()
    {
        next = NULL;
    }

    Node( T val )
    {
        info = val;
        next = NULL;
    }
};

template <class T>
class LSLL
{
private:
    Node<T> * head;
public:
    LSLL();
    ~LSLL();
    LSLL( LSLL<T>&);
    LSLL & operator = ( const LSLL & l2 );
    void display();
    void insertAtHead( T val );
    void insertAtTail( T val );
    void insertAfter( T val, T key );
    void insertBefore( T val, T key );

};


template <class T>
LSLL<T> :: LSLL()
{
    head=0;
}

template <class T>
LSLL<T> :: ~LSLL()
{
    Node<T> * p = head;
    Node<T> * del = head;

    while( p != NULL )
    {
        p = p -> next;
        delete del;
        del = p;
    }
}
template <class T>
LSLL<T>::LSLL( LSLL<T>& l2)
{
    Node<T> * temp= l2.head;
    while(temp != NULL)
    {
        this->insertAtTail(temp->info);           //crashes here
        temp = temp->next;
    }
}



You're not initializing head in the copy constructor.
you are a champ Athar :)
Topic archived. No new replies allowed.