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();
}
Hello itfreakas,

When I tried to compile the program I received an error on line 260 the "ptr" is an uninitialized variable. It also means that "ptr" never receives a value before it is used.
It comes down to:

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

When I actually ran the program it started and ended just as quickly. Then I realized there is an input I do not have to work with. Having no idea what is in the file it will take awhile to come up with something to use. Or you could post the input file so everyone knows they have to work with.

Having a problem with the program closing to soon I added line two to give the user a chance to see the error message. Doing a "return" for line three may not be the best idea I generally use exit(1); Where any number greater than zero denotes an error. The "-1" may work , but is not the expected number.

1
2
3
4
5
6
if (!fin)
{
	cout << "Error! Unable to read a text file " << "1.txt" << endl << endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
	exit(1);
}


That is what I have found so far.

Hope that helps,

Andy
Hello itfreakas,

What I have found so far is if you only have one node, "start" this portion of the delete function is not working

1
2
3
4
5
6
7
8
9
10
if (pos == 1)
{
	counter--;
	last->next = s->next;
	s->next->prev = last;
	start = s->next;
	delete(s);
	cout << "Element successfully removed!" << endl << endl;
	return;
}

Lines 3, 8 and 9 are OK, but the rest is a problem. Line 5 causes a run time error because "s" has an address to use , but "s->next" is a nullprt having no address to use, so "s->next->prev" has no address to store the value of "last".

I believe what would work is to delete "start" then set "start"and "last" to "nullptr"s leaving you with an empty list. I have not changed this yet to test, but will let you know when I have.

Hope that helps,

Andy
Hello itfreakas,

I changed the above code to:
1
2
3
4
5
6
7
8
9
if (pos == 1)
{
	counter--;
	delete(start);
	start = last = nullptr;

	cout << "\nElement successfully removed!" << endl << endl;
	return;
}


And it works.

Reading other parts of your code I noticed s = start;. Although this is OK it is not necessary. That is what "start " is for to give you a starting place to begin. Now something like s = start + 3; to give you a new place to start from makes sense, but still not really necessary because even that could make use of "start + 3" where needed. Just a thought for the future I am not saying that you have to go change the whole program.

Hope that helps,

Andy
Thank You So Much!
Well actually this gives rise to another problem. For example if I have a list: 0, 1, 2, 3, 4 and I choose to delete the first node of the list which is 0, then the whole list then becomes empty.
Look at lines 117 and 118. By setting the prev and next pointers to NULL, the list isn't circular. The same bug exists in several places.
Hello itfreakas,

Maybe I misunderstood. I had the idea that you wanted to delete one node from a one node linked list. What I said earlier would still hold true.

To delete the first node of a linked lint you would need to set "start" to the second node for a new starting point and "start->prev" to last to make it circular.

I will have to go back to the code to see where to fix this.

Hope that helps,

Andy
Handy Andy, you understood correct. I needed to be able to delete one node from a one node linked list, but at the same time I need to be able to delete the first node when there are a lot of nodes in the list. For example my list is consists of 1 2 3 4 5, and in the current code, if I try to delete the first node, which is 1 - the whole list becomes empty. I should be able to delete 1, leaving 2 3 4 5 in the list. At first I was able to do that, but I could not delete the last remaining node in the one node list without printing garbage or crashing, but now when you fixed my code, I can delete one node from a one node linked list, but I can't delete specifically only the first element, without deleting the remaining ones.
Hello itfreakas,

It has been awhile since I did much work with linked lists. We have satisfied dealing with a one node list. When you delete that one node both "start" and "last" have to point to "nullptr" so they can not be used.

When you want to delete zero from your list "1" has to become "start" "start->prev" has to become last "start->next" needs to point to "2". When you want to delete the "last" node it is much like deleting "start". Now what you used as "s = start" is useful as "s = node to delete".

I am thinking that when you delete the "start" node and set "start" equal to "1" you will also have to delete "1" or you will have a duplicate.

I need to work this out on a piece of paper first, but I think I have it right.

Hope that helps,

Andy
Topic archived. No new replies allowed.