Linked List Problem in delete and memory leak

please check the program and correct the delete option and memory leak.Thanks in advance.

#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data; // will store information
node *next; // the reference to the next node
};
node *head = NULL; //empty linked list
int info = 0, node_number = 0, counter = 0;
int display();
int insertfirst()
{ cout<<"ENTER ANY NUMBER:";
cin>>info; //take input data
node *temp; //create temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; //store data in first field
temp->next=head; //store the address of the pointer head for second field
head = temp; //transfer the address of temp to head
counter++;
display();
}
int insertlast()
{
if(head==NULL)
{
cout<<"List is Empty";
}
else
{
cout<<"ENTER ANY NUMBER:";
cin>>info; // take input data
node *temp1; // create a temporary node
temp1 = head; // transfer the address of 'head' to 'temp1'
while(temp1->next!=NULL) // go to the last node
temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'
node *temp; // create a temporary node
temp=(node*)malloc(sizeof(node));
temp->data = info; // store data(first field)
temp->next = NULL; // second field will be null(last node)
temp1->next = temp;
counter++;
display();
}
}
int insertpos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
node *temp;
temp = (node*)malloc(sizeof(node));
temp->data = info;
temp->next = temp1->next;
temp1->next = temp;
counter++;
display();
}
int insertreppos()
{
if(head==NULL)
{
cout<<"List is empty";
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
cout<<"ENTER ANY NUMBER:";
cin>>info;
node *temp1;
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next;
if( temp1 == NULL )
{
cout<<node_number<<" node is not exist"<< endl;
return 0;
}
}
temp1->data = info;
display();
}
int delfirst()
{
if(head==NULL)
{
return 0;
}
else if(head->next==NULL)
{
head = NULL;
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
return 0;
}
node *temp;
temp = head;
head = temp->next;
free(temp);
cout<<"The first node of the Linked List is deleted"<<endl;
counter--;
display();
}
int dellast()
{
if(head==NULL)
{
display();
}
else if(head->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
else
{
node *temp1,*old_temp;
temp1 = head;
while(temp1->next!=NULL)
{
old_temp=temp1;
temp1= temp1->next;
}
old_temp->next=NULL;
free(temp1);
cout<<"The last node of the Link List is deleted : "<<endl;
counter--;
display();

}
int delpos()
{
if(head==NULL)
{
return 0;
}
else if(head ->next==NULL)
{
head = NULL;
cout<<"The last node of the Linked List is deleted"<<endl;
counter--;
display();
return 0;
}
cout<<"ENTER THE NODE NUMBER:";
cin>>node_number;
if(node_number > counter)
{
cout<<"No such node is exist";
return 0;
}
node *temp1;
temp1 = head;
for(int j=0;j<node_number;j++)
{
head=temp1->next;
if(j==node_number)
{
free(temp1);
counter--;
}
}
cout<<node_number<<" node of the Linked List is deleted"<<endl;
display();

}
int deletelist()
{
node *temp;
if(head==NULL)
return 0;
while(head!=NULL)
{
temp=head->next;
delete temp;
head=temp;
}
}
int display()
{
node *temp;
temp=head;
if(temp==NULL)
{
cout<<"List is empty";
return 0;
}
cout<<"\nThe elements are \n"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
int main()
{
char ch;
do{
cout<<"\n\n";
cout<<"1.Insert at first\n";
cout<<"2.Insert at last\n";
cout<<"3.Insert after specified number of node\n";
cout<<"4.Delete at first node\n";
cout<<"5.Delete at last node\n";
cout<<"6.Delete specified number of node\n";
cout<<"7.Display\n";
cout<<"8.Replace specified number of node\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case '1': insertfirst();
break;
case '2': insertlast();
break;
case '3': insertpos();
break;
case '4': delfirst();
break;
case '5': dellast();
break;
case '6': delpos();
break;
case '7': display();
break;
case '8' : insertreppos();
break;
default : cout<<"Invalid option";

}
cout<<"\ndo you want continue (y/n) : ";
cin>>ch;
}while(ch=='y');
deletelist();
display();
return 0;
}
http://www.cplusplus.com/articles/jEywvCM9/

Your code is rather difficult to read due to the complete lack of whitespace, even after it is reformatted.

Pay attention to the error(s) your compiler reports. Fix them, then get back to us. Or, if you can't fix them, at least let us know what they are and what you've tried to fix them.

There is no point in worrying about a memory leak in code that won't even compile.
Last edited on
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include<iostream>
#include<stdlib.h>

using namespace std;

struct node
{
    int data; // will store information
    node *next; // the reference to the next node
};

node *head = NULL; //empty linked list

int info = 0, node_number = 0, counter = 0;

int display();

int insertfirst()
{   cout<<"ENTER ANY NUMBER:";
    cin>>info; //take input data
    node *temp; //create temporary node
    temp=(node*)malloc(sizeof(node));
    temp->data = info; //store data in first field
    temp->next=head; //store the address of the pointer head for second field
    head = temp; //transfer the address of temp to head
    counter++;
    display();
}

int insertlast()
{
    if(head==NULL)
    {
        cout<<"List is Empty";
    }
    else
    {
        cout<<"ENTER ANY NUMBER:";
        cin>>info; // take input data
        node *temp1; // create a temporary node
        temp1 = head; // transfer the address of 'head' to 'temp1'

        while(temp1->next!=NULL) // go to the last node
            temp1 = temp1->next;//tranfer the address of 'temp1->next' to 'temp1'

        node *temp; // create a temporary node
        temp=(node*)malloc(sizeof(node));
        temp->data = info; // store data(first field)
        temp->next = NULL; // second field will be null(last node)
        temp1->next = temp;
        counter++;
        display();
    }
}

int insertpos()
{
    if(head==NULL)
    {
        cout<<"List is empty";
    }

    cout<<"ENTER THE NODE NUMBER:";
    cin>>node_number;
    cout<<"ENTER ANY NUMBER:";
    cin>>info;
    node *temp1;
    temp1 = head;

    for( int i = 1 ; i < node_number ; i++ )
    {
        temp1 = temp1->next;

        if( temp1 == NULL )
        {
            cout<<node_number<<" node is not exist"<< endl;
            return 0;
        }
    }

    node *temp;
    temp = (node*)malloc(sizeof(node));
    temp->data = info;
    temp->next = temp1->next;
    temp1->next = temp;
    counter++;
    display();
}

int insertreppos()
{
    if(head==NULL)
    {
        cout<<"List is empty";
    }

    cout<<"ENTER THE NODE NUMBER:";
    cin>>node_number;
    cout<<"ENTER ANY NUMBER:";
    cin>>info;
    node *temp1;
    temp1 = head;

    for( int i = 1 ; i < node_number ; i++ )
    {
        temp1 = temp1->next;

        if( temp1 == NULL )
        {
            cout<<node_number<<" node is not exist"<< endl;
            return 0;
        }
    }

    temp1->data = info;
    display();
}

int delfirst()
{
    if(head==NULL)
    {
        return 0;
    }
    else if(head->next==NULL)
    {
        head = NULL;
        cout<<"The first node of the Linked List is deleted"<<endl;
        counter--;
        return 0;
    }

    node *temp;
    temp = head;
    head = temp->next;
    free(temp);
    cout<<"The first node of the Linked List is deleted"<<endl;
    counter--;
    display();
}

int dellast()
{
    if(head==NULL)
    {
        display();
    }
    else if(head->next==NULL)
    {
        head = NULL;
        cout<<"The last node of the Linked List is deleted"<<endl;
        counter--;
        display();
        return 0;
    }
    else
    {
        node *temp1,*old_temp;
        temp1 = head;

        while(temp1->next!=NULL)
        {
            old_temp=temp1;
            temp1= temp1->next;
        }

        old_temp->next=NULL;
        free(temp1);
        cout<<"The last node of the Link List is deleted : "<<endl;
        counter--;
        display();

    }
}

int delpos()
{
    if(head==NULL)
    {
        return 0;
    }
    else if(head ->next==NULL)
    {
        head = NULL;
        cout<<"The last node of the Linked List is deleted"<<endl;
        counter--;
        display();
        return 0;
    }

    cout<<"ENTER THE NODE NUMBER:";
    cin>>node_number;

    if(node_number > counter)
    {
        cout<<"No such node is exist";
        return 0;
    }

    node *temp1;
    temp1 = head;

    for(int j=0; j<node_number; j++)
    {
        head=temp1->next;

        if(j==node_number)
        {
            free(temp1);
            counter--;
        }
    }

    cout<<node_number<<" node of the Linked List is deleted"<<endl;
    display();

}

int deletelist()
{
    node *temp;

    if(head==NULL)
        return 0;

    while(head!=NULL)
    {
        temp=head->next;
        delete temp;
        head=temp;
    }
}

int display()
{
    node *temp;
    temp=head;

    if(temp==NULL)
    {
        cout<<"List is empty";
        return 0;
    }

    cout<<"\nThe elements are \n"<<endl;

    while(temp!=NULL)
    {
        cout<<temp->data<<"\t";
        temp=temp->next;
    }
}

int main()
{
    char ch;

    do {
        cout<<"\n\n";
        cout<<"1.Insert at first\n";
        cout<<"2.Insert at last\n";
        cout<<"3.Insert after specified number of node\n";
        cout<<"4.Delete at first node\n";
        cout<<"5.Delete at last node\n";
        cout<<"6.Delete specified number of node\n";
        cout<<"7.Display\n";
        cout<<"8.Replace specified number of node\n";
        cout<<"Enter your choice: ";
        cin>>ch;

        switch(ch)
        {
        case '1':
            insertfirst();
            break;
        case '2':
            insertlast();
            break;
        case '3':
            insertpos();
            break;
        case '4':
            delfirst();
            break;
        case '5':
            dellast();
            break;
        case '6':
            delpos();
            break;
        case '7':
            display();
            break;
        case '8' :
            insertreppos();
            break;
        default :
            cout<<"Invalid option";

        }

        cout<<"\ndo you want continue (y/n) : ";
        cin>>ch;
    } while(ch=='y');

    deletelist();
    display();
    return 0;
}

Here are the obvious problems I can see:
1. The list exists as a global. This is bad because your functions will only work for that list, and just that one list. If you want to manipulate more lists, you need to make more functions or add branches in the functions.
2. Your I/O code and list manipulation code are mixed. This causes your operations to not be clearly defined, which leads to problem #3:
3. You have duplicate code. I can see at least three places where you malloc() nodes. Never duplicate code. Any bugs you run into will need to be fixed in each of the copies, and you'll always forget to update one (not necessarily always the same one).
I would not bother debugging memory leaks before these issues are resolved.

My recommendation is that you implement these functions and then write your I/O and other logic around them:
1
2
3
4
5
6
7
8
9
10
11
12
//Note: A node *& is a reference to a pointer. It lets you transparently modify
//the pointer that was passed as parameter. Feel free to change it to node **,
//if you prefer.
//Note: The functions that return bool return true on success.
void add_head(node *&list, int data);
void add_tail(node *&list, int data);
bool add_at_pos(node *&list, int pos, int data);
bool remove_head(node *&list);
bool remove_tail(node *&list);
bool remove_at_pos(node *&list, int pos);
void display_list(node *list);
bool replace_at_pos(node *&list, int pos, int data);
Topic archived. No new replies allowed.