HELP!

Hello everyone, I have this working program with operations on circular doubly linked list. But I have one slight problem, for example when there is only one node in the list and when I try to remove it with my remove function, afterwards instead of printing that the list is now empty, it prints me garbage like 41096232 or crashes, why is that?

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
310
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
using namespace std;

struct node
{
    int info;
    node *next;
    node *prev;
}
*start, *last;
int counter = 0;

class double_clist
{
    public:
        node *create_node(int);
        void insert_file(int value);
        void insert_begin();
        void insert_last();
        void insert_pos();
        void delete_pos();
        void display();
        void display_file();
        double_clist()
        {
            start = 0;
            last = 0;
        }
};

int main()
{
    double_clist cdl;

    ifstream fin("1.txt");
    int value;
    if (!fin)
    {
        cout << "Error! Unable to read a text file " << "1.txt" << endl << endl;
        return -1;
    }
    while(fin >> value)
    {
        cdl.insert_file(value);
    }

    cout << "Formed circular doubly linked list:" << endl;
    cdl.display();
    cdl.display_file();

    int choice;
    while (1)
    {
        cout << "OPERATIONS ON CIRCULAR DOUBLY LINKED LIST!" << endl << endl;
        cout << "1. Insert element in the begining!" << endl << endl;
        cout << "2. Insert element in the end!" << endl << endl;
        cout << "3. Insert element in the middle!" << endl << endl;
        cout << "4. Remove element from the position!" << endl << endl;
        cout << "5. Display elements on the screen!" << endl <<endl;
        cout << "6. End program!" << endl << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        switch(choice)
        {
        case 1:
            cdl.insert_begin();
            cdl.display();
            break;
        case 2:
            cdl.insert_last();
            cdl.display();
            break;
        case 3:
            cdl.insert_pos();
            cdl.display();
            break;
        case 4:
            cdl.delete_pos();
            cdl.display();
            break;
        case 5:
            cdl.display();
            break;
        case 6:
            cdl.display_file();
            system("start results.txt");
            exit(1);
        default:
            cout << "Wrong choice!" << endl << endl;
        }
    }
    return 0;
}

node* double_clist::create_node(int value)
{
    counter++;
    struct node *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = 0;
    temp->prev = 0;
    return temp;
}

void double_clist::insert_file(int value)
{
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == 0)
    {
        cout << "Elements are successfully read from the text file!" << "1.txt" << "!" << endl << endl;
        start = last = temp;
        start->next = last->next = 0;
        start->prev = last->prev = 0;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

void double_clist::insert_begin()
{
    int value;
    cout << endl << "Enter the element which you want to insert at the beginning: ";
    cin >> value;
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == 0)
    {
        cout << "Element successfully inserted!" << endl << endl;
        start = last = temp;
        start->next = last->next = 0;
        start->prev = last->prev = 0;
    }
    else
    {
        temp->next = start;
        start->prev = temp;
        start = temp;
        start->prev = last;
        last->next = start;
        cout << "Element successfully inserted!" << endl << endl;
    }
}

void double_clist::insert_last()
{
    int value;
    cout << endl<<"Enter the element which you want to insert at the end: ";
    cin >> value;
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == 0)
    {
        cout << "Element successfully inserted!" << endl << endl;
        start = last = temp;
        start->next = last->next = 0;
        start->prev = last->prev = 0;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
        cout << "Element successfully inserted!" << endl << endl;
    }
}

void double_clist::insert_pos()
{
    int value, pos, i;
    cout << endl << "Enter the element which you want to insert: ";
    cin >> value;
    cout << endl << "Enter the position: ";
    cin >> pos;
    struct node *temp, *s, *ptr;
    temp = create_node(value);
    if (start == last && start == 0)
    {
        if (pos == 1)
        {
            start = last = temp;
            start->next = last->next = 0;
            start->prev = last->prev = 0;
        }
        else
        {
            cout << "Position is unavailable!" << endl << endl;
            counter--;
            return;
        }
    }
    else
    {
        if (counter < pos)
        {
             cout << "Position is unavailable!" << endl << endl;
             counter--;
             return;
        }
        s = start;
        for (i = 1;i <= counter;i++)
        {
            ptr = s;
            s = s->next;
            if (i == pos - 1)
            {
                ptr->next = temp;
                temp->prev = ptr;
                temp->next = s;
                s->prev = temp;
                cout << "Element successfully inserted!" << endl << endl;
                break;
            }
        }
    }
}

void double_clist::delete_pos()
{
    int pos, i;
    node *ptr, *s;
    if (start == last && start == 0)
    {
        cout << "Circular doubly linked list is empty!" << endl << endl;
        return;
    }
    cout << "Enter the position of the elements which you want to remove: ";
    cin >> pos;
    if (counter < pos)
    {
        cout << "Position is unavailable!" << endl << endl;
        return;
    }
    s = start;
    if(pos == 1)
    {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        delete(s);
        cout << "Element successfully removed!" << endl << endl;
        return;
    }
    for (i = 0;i < pos - 1;i++ )
    {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter)
    {
        last = ptr;
    }
    counter--;
    delete(s);
    cout << "Element successfully removed!" << endl << endl;
}

void double_clist::display()
{
    int i;
    struct node *s;
    if (start == last && start == 0)
    {
        cout << "Circular doubly linked list is empty!" << endl << endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        cout <<s->info << "<->";
        s = s->next;
    }
    cout <<s->info << endl << endl;
}

void double_clist::display_file()
{
    ofstream fout;
    fout.open("results.txt", ios::app);
    int i;
    struct node *s;
    if (start == last && start == 0)
    {
        fout << "Circular doubly linked list is empty!" << endl << endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        fout<<s->info<<"<->";
        s = s->next;
    }
    fout << s->info << endl << endl;
    fout.close();
}
Your delete method doesn't reset the pointers to null, which is your invariant state for empty. So the object doen't know when it's empty.

You can improve the usability of your list my making start and last members of the class. That way you can instantiate many different instances of lists.

The methods should just take the values or positions to be processed. They shouldn't prompt the user. That will allow more flexible use.
Topic archived. No new replies allowed.