copy constructor for linked list

Hi ,
I need help for creating copy constructor for linked list with structure member.
It compiles correctly but gives runtime error . The problem here is in memory management but I am not able to handle it.Please help me...
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <new>

using namespace std;

class Array {
    struct node {
        string str;
        int item;
        struct node *next;
    }*p;
    public :
        Array()
        {
            p = NULL;
        }

        ~Array()
        {
            node *s;
            if(p == NULL)
                return;
            while(p != NULL)
            {
                s = p->next;
                delete p;
                p = s;
            }
        }

        void getValue(int i)
        {
            int k=0;
            node *s = p;
            
            while(s != NULL)
            {
                if(k == i)
                    cout << s->str << " " << s->item << endl;
                s = s->next;
                k++;
            }
        }

        void putValue(int j,string st)
        {
            node *s,*t;
            if(p == NULL)
            {
                t = new node;
                t->item = j;
                t->str = st;
                t->next = NULL;
                p = t;
            }
            else 
            {
                t = p;
                while(t->next != NULL)
                    t = t->next;
                s = new node;
                s->item = j;
                s->str = st;
                s->next = NULL;
                t->next = s;
            }
        }

        Array(const Array &a);
};

Array::Array(const Array &a)
{
    node *s,*t,*r;
    s = a.p;
      
    while(s != NULL)
    {
        if(t == NULL)
        {
            t = new node;
            t->item = s->item;
            t->str = s->str;
            t->next = NULL;
            p = t;
        }
        else 
        {
            t = p;
            while(t->next != NULL)
                t = t->next;
            r = new node;
            r->item = s->item;
            r->str = s->str;
            r->next = NULL;
            t->next = r;
        }
        s = s->next;
    }
}

int main()
{
    Array num;
    int i;
    string t = "Hello";
    
    for(i=0;i<10;i++) num.putValue(i,t);
    for(i=9;i>=0;i--) num.getValue(i);
    cout << "\n";

    Array x(num);
    cout << "\n";
    for(i=9;i>=0;i--) x.getValue(9-i);
    
    getch();
    return 0;
}



Last edited on
In your copy constructor you use 't' before it is initialised.
"Array(const Array &a,string st)" is not called as you expected. Since only one parameter is put, the call at #144 was not calliing yours, but Array(const Array &a) which is implicitly generated to copy all parameters to the "x" including "p" and all its pointers ( their addresses).

The run time error could happen when their descontructors are called to delete those addresses at the end of the whole app. ( The descontructor is called implicitly during the ending)


By the way, in the copy constructor, just as the normal constructor does, may you need to firstly add one line to declare "p = NULL".
Last edited on
Topic archived. No new replies allowed.