Concatenation Linked List

Here is my program.. I am unable to concatenate the 2 linked lists using operator overloading. I have got the output of the concatenation once and then again i am not getting it. it is compiling but stucks in run time. 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <string>

using namespace std;

class LinkedList
{
    public:
    struct Node
    {
        string value;
        Node *next;
    }*p;

    LinkedList()
    {
        p = NULL;
    }

    /*~LinkedList()
    {
        delete p;
    }*/
    void printAll();
    void addLast(string name);
    void addFirst(string name);
    void removeFirst();
    void removeLast();
    void getFirst();
    void getLast();

   LinkedList operator+(const LinkedList &l);
};


LinkedList LinkedList::operator+(const LinkedList &l)
{
    LinkedList l1 = *this;
    Node *tmp= l1.p;

    while(tmp!=NULL)
    {
        tmp=tmp->next;
    }
    tmp->next = l.p;

    return l1;
}

void LinkedList::getFirst()
{
    if(p!=NULL)
    {
        cout<<"The first node in the list is: "<<p->value<<endl;
    }
}

void LinkedList::getLast()
{
    Node *tmp=p;
    while(tmp!=NULL)
    {
        tmp=tmp->next;
    }

    if(tmp->next==NULL)
    {
        cout<<"The last node in the list is: "<<tmp->value<<endl;
    }
}

void LinkedList::printAll()
{
    if(p==NULL)
    {
        cout<<"There are no nodes in the list"<<endl;
        return;
    }
    else
    {
        Node *tmp = p;
        cout<<endl<< "The nodes in the list are: ";

        while(tmp!=NULL)
        {
            cout << tmp->value<<" ";
            tmp=tmp->next;
        }
    }
}

void LinkedList::addFirst(string name)
{
    if(name.length()>4)
    {
        cout<<" The string having more than 4 characters could not be inserted into the list"<<endl;
        return;
    }
    else
    {
            Node* tmpFirst = new Node;

            tmpFirst->value=name;
            tmpFirst->next=p;

            p=tmpFirst;
    }
}

void LinkedList::addLast(string name)
{
    if(name.length()>4)
    {
        cout<<" The string having more than 4 characters could not be inserted into the list"<<endl;
        return;
    }
    else
    {
        Node *tmpLast = new Node;
        tmpLast->value=name;
        tmpLast->next=NULL;

        if(p==NULL)
            p=tmpLast;
        else
        {
            Node *tmp=p;

            while(tmp->next!=NULL)
            {
                tmp=tmp->next;
            }
            tmp->next=tmpLast;
        }
    }
}

void LinkedList::removeFirst()
{
    if(p==NULL)
    {
        cout<<"The list is empty"<<endl;
        return;
    }
    else
    {
        Node *tmp=p;
        p=p->next;
        tmp->next=NULL;

        delete tmp;
    }
}

void LinkedList::removeLast()
{
    if(p==NULL)
        return;
    else
    {
        Node *tmp=p;
        Node *tmp1=p;

        while(tmp->next!=NULL)
        {
            tmp=tmp->next;
        }
        while(tmp1->next!=tmp)
        {
            tmp1=tmp1->next;
        }
        tmp1->next=NULL;
    }
}

int main()
{
    LinkedList list1;
    LinkedList list2;
    LinkedList list3;

        list1.addFirst("asda");
        list2.addLast("fdgs");
        list1.printAll();
        list2.printAll();
        list1+list2;
        list1.printAll();
}
Last edited on
The loop on line 41 causes the iteration node to be null once the loop ends, so the assignment on line 45 dereferences a null pointer. Try to come up with a condition for the loop that will fix this.
it worked Helios.. In while statement i kept while(tmp->next!=NULL) and it worked :)
when i add the copy constructor to the same code, its stucking at the run time. I dnt understand why


#include <iostream>
#include <string>

using namespace std;

class LinkedList
{
public:
struct Node
{
string value;
Node *next;
}*p;

LinkedList()
{
p = NULL;
}

/*~LinkedList()
{
delete p;
}*/
void printAll();
void addLast(string name);
void addFirst(string name);
void removeFirst();
void removeLast();
void getFirst();
void getLast();

LinkedList operator+(const LinkedList &l);
LinkedList(const LinkedList &l);
};

LinkedList::LinkedList(const LinkedList &l)
{
Node* tmp = l.p;

while (tmp!=NULL)
{
Node* tmpcopy = new Node;
tmpcopy->value = tmp->value;
tmp = tmp->next;
}
}

LinkedList LinkedList::operator+(const LinkedList &l)
{
LinkedList l1 = *this;
Node *tmp = l1.p;

while(tmp->next!=NULL)
{
tmp=tmp->next;
}
tmp->next = l.p;

return l1;
}

void LinkedList::getFirst()
{
if(p!=NULL)
{
cout<<"The first node in the list is: "<<p->value<<endl;
}
}

void LinkedList::getLast()
{
Node *tmp=p;
while(tmp!=NULL)
{
tmp=tmp->next;
}

if(tmp->next==NULL)
{
cout<<"The last node in the list is: "<<tmp->value<<endl;
}
}

void LinkedList::printAll()
{
if(p==NULL)
{
cout<<"There are no nodes in the list"<<endl;
return;
}
else
{
Node *tmp = p;
cout<<endl<< "The nodes in the list are: ";

while(tmp!=NULL)
{
cout << tmp->value<<" ";
tmp=tmp->next;
}
}
}

void LinkedList::addFirst(string name)
{
if(name.length()>4)
{
cout<<" The string having more than 4 characters could not be inserted into the list"<<endl;
return;
}
else
{
Node* tmpFirst = new Node;

tmpFirst->value=name;
tmpFirst->next=p;

p=tmpFirst;
}
}

void LinkedList::addLast(string name)
{
if(name.length()>4)
{
cout<<" The string having more than 4 characters could not be inserted into the list"<<endl;
return;
}
else
{
Node *tmpLast = new Node;
tmpLast->value=name;
tmpLast->next=NULL;

if(p==NULL)
p=tmpLast;
else
{
Node *tmp=p;

while(tmp->next!=NULL)
{
tmp=tmp->next;
}
tmp->next=tmpLast;
}
}
}

void LinkedList::removeFirst()
{
if(p==NULL)
{
cout<<"The list is empty"<<endl;
return;
}
else
{
Node *tmp=p;
p=p->next;
tmp->next=NULL;

delete tmp;
}
}

void LinkedList::removeLast()
{
if(p==NULL)
return;
else
{
Node *tmp=p;
Node *tmp1=p;

while(tmp->next!=NULL)
{
tmp=tmp->next;
}
while(tmp1->next!=tmp)
{
tmp1=tmp1->next;
}
tmp1->next=NULL;
}
}

int main()
{
LinkedList list1;
LinkedList list2;
LinkedList list3;
LinkedList list4;



list1.addFirst("asda");
list2.addLast("fdgs");
list1.printAll();
list2.printAll();
list4 = list2;
list4.printAll();
list3 = list1+list2;
list3.printAll();

}
Topic archived. No new replies allowed.