Exception thrown: read access violation. Doubly linked list

I'm getting this error. I don't understand why temp->next is nullptr if temp=head.

Exception thrown: read access violation.
temp->**next** was nullptr. occurred

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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "pch.h"
#include <iostream>

using namespace std;

struct node {
	int data;
	struct node *next;
	struct node *prev;
};

void create_list(node *&head, node *&tail, int num);

void display_dlist(node *head);
void reverse_display_dlist(node *head);

void insert_position_dlist(node *current, node *tail, int value, int position);
void insert_before_dlist(node *&head, int key);

void delete_element_dlist(node *&head, int key);
void delete_position_dlist(node *&head, int pos);
void delete_whole_dlist(node *&head);

void search_dlist(node *head, int key);



int Size = 0;


int main()
{
	int choice, value, position;

	node *head = NULL;
	node *tail = NULL;

	cout << "1.Sukuria dvikrypti sarasa (INSERTS A NODE)" << endl;
	cout << "2.Spausdinimas nuo pradziu" << endl;
	cout << "3.Spausdinimas nuo galo" << endl;
	cout << "4.Iterpia nauja elementa i nurodyta saraso pozicija" << endl;
	cout << "5.Iterpia elementa pries tam tikra elementa" << endl;
	cout << "6.Istrina pasirinkta reiksme (temp->next nullptr" << endl;
	cout << "7.Istrina pasirinkta pozicija" << endl;
	cout << "8.Istrina visa sarasa" << endl;
	cout << "9.Elemento paieskat" << endl;


	while (1) {

		cout << "Iveskite pasirinkima: ";
		cin >> choice;
		cout << endl;

		switch (choice) {
		case 1:
			cout << "Iveskite elementa: ";
			cin >> value;
			create_list(head, tail, value);
			cout << endl;
			break;
		case 2:
			cout << "Sarasas is priekio- ";
			display_dlist(head);
			cout << endl;
			break;
		case 3:
			cout << "Sarasas is galo- ";
			reverse_display_dlist(head);
			cout << endl;
			break;
		case 4:
			cout << "Iveskite norima pozicija ";
			cin >> position;
			cout << endl;
			cout << "Iveskite elementa ";
			cin >> value;
			insert_position_dlist(head, tail, value, position);
			cout << endl;
			break;
		case 5:
			cout << "Iveskite elementa pries kuri norite iterpti ";
			int key;
			cin >> key;
			cout << endl;
			insert_before_dlist(head, key);
			break;
		case 6:
			cout << "Kuria reiksme norite istrinti ";
			cin >> key;
			cout << endl;
			delete_element_dlist(head, key);
			break;
		case 7:
			cout << "Kuria pozicija norite istrinti ";
			cin >> position;
			delete_position_dlist(head, position);
			cout << endl;
			break;
		case 8:
			cout << "Sarasas istrintas";
			delete_whole_dlist(head);
			cout << endl;
			break;
		case 9:
			cout << "Kokio elemento ieskote? ";
			cin >> key;
			search_dlist(head, key);
			cout << endl;
			break;
		default:
			cout << "Tokio pasirinkimo nera" << endl;
			break;
		}
	}

	return 0;
}
void create_list(node *&head, node *&tail, int num) {
	struct node *temp, *temp1;
	temp = new node;
	temp->data = num;
	temp->next = NULL;
	if (head == NULL) {
		temp->prev = NULL;
		head = temp;
	}
	else {
		temp1 = head;
		while (temp1->next != NULL)
			temp1 = temp1->next;
		temp1->next = temp;
		temp->prev = temp1;
		tail = temp;
	}
	Size++;
}


void display_dlist(node *head) {
	node *temp = head;

	if (temp == NULL) {
		cout << "Sarasas tuscias" << endl;
		return;
	}

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

void reverse_display_dlist(node *head) {
	node *temp = head;

	if (temp == NULL) {
		cout << "Sarasas tuscias" << endl;
		return;
	}

	while (temp->next != NULL) {
		temp = temp->next;
	}
	while (temp != head) {
		cout << temp->data << "  ";
		temp = temp->prev;
	}
	cout << temp->data << endl;
}


void insert_position_dlist(node *head, node *tail, int value, int position) { //kazkas su head insertion negerai
	node *temp = new node;
	node *temp1;
	node *temp2;

	temp->data = value;
	if (position == 1 || head == NULL)
	{
		if (head == NULL && position == 1)
		{
			temp->prev = temp->next = NULL;
			head = temp;
			return;
		}
		head->prev = temp;
		temp->prev = NULL;
		temp->next = head;
		head = temp;
		return;
	}
	temp1 = head;
	for (int i = 0; i < position - 2; i++)
	{
		temp1 = temp1->next;

	}
	temp->next = temp1->next;
	temp->prev = temp1;
	temp1->next = temp;
	if (temp->next != NULL)
	{
		temp2 = temp->next;
		temp2->prev = temp;
	}
	Size++;
}

void insert_before_dlist(node *&head, int key) {
	node *temp = new node;
	node *temp1 = head;
	int num;
	cout << "Iveskite elementa ";
	cin >> num;
	cout << endl;
	temp->data = num;

	while (temp1 != NULL) {
		if (temp1->data == key) {
			break;
		}
		temp1 = temp1->next;
		if (temp1->data == key) {
			break;
		}
	}
	temp->prev = temp1->prev;
	temp->next = temp1;
	temp1->prev = temp;

	if (temp->prev != NULL)
		temp->prev->next = temp;
	else {
		head = temp;
	}
	Size++;
}


void delete_element_dlist(node *&head, int key) { //kazkodel pradejo neveikt EYOOOOO
	node *temp;
	temp = head;
	if (temp == NULL)
	{
		cout << "Sarasas tuscias" << endl;
		return;
	}
	else if (temp->data == key) //+++++++++++
	{
		head = temp->next;
		delete temp;
	}
	else
	{
		while (temp->data != key)
			temp = temp->next;
		node* temp2 = NULL;
		temp2 = temp->next;
		while (temp->next->data != key)
		{
			temp = temp->next;
			temp2 = temp->next;
		}
		temp->next = temp2->next;
		temp2->prev = temp;

		delete temp;
	}
	Size--;
}

void delete_position_dlist(node *&head, int pos) { // 1 element-crash
	node *temp;
	temp = head;
	if (pos > Size + 1) {
		cout << "Blogai ivesta pozicija" << endl;
		return;
	}
	if (temp == NULL) {
		cout << "Sarasas tuscias" << endl;
		return;
	}
	if (temp->next == NULL) {
		delete temp;
		return;
	}

	for (int i = 1; i < pos&&temp != NULL; i++)
		temp = temp->next;

	if (pos == 1) {
		temp = head;
		head = head->next;
		head->prev = NULL;
		delete temp;
	}

	else if (temp->next == NULL) {
		node *temp;
		temp = head;
		while (temp->next->next != NULL)
			temp = temp->next;
		delete temp->next;
		temp->next = NULL;
	}

	else if (temp != NULL) {
		temp->prev->next = temp->next;
		temp->next->prev = temp->prev;
		delete temp;
	}
	Size--;
}

void delete_whole_dlist(node *&head) {
	if (head == NULL) {
		cout << "Sarasas jau yra tuscias" << endl;
		return;
	}

	node *current = head;
	node *temp;

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

	Size = 0;
}


void search_dlist(node *current, int key) {
	int pos = 0;

	if (current == NULL) {
		cout << "Sarasas tuscias" << endl;
		return;
	}
	cout << "Elemento pozicijos- ";
	while (current != NULL) {
		pos++;
		if (current->data == key) {
			cout << pos << "  ";
		}
		if (current->next != NULL)
			current = current->next;
		else
			break;
	}
	cout << endl;
}
what input did you type in to crash it? Or is it dying before it gets that far?

you don't need struct in front of node when making variables of type node in c++, that is a C thing.

I suspect it is blowing up in create list, is that right?
Last edited on
Input: 1 1 1 2 1 3 (inserts 3 nodes).
press 2 to print the list.
press 6 (this is a function to delete given key).
then press 2.

It will crash. It says that temp->next is NULL. But I don't know why becauseI did write "temp = head".

This only happens in this function.
Last edited on
I will look later but look again at the logic in the else block at line 255.
it has to be in that chunk...
Line 258: At the end temp becomes null. You need to break the loop when that happens otherwise line 257 will crash.
In create_list (which should probably be called append()), you don't set tail when appending to an empty list.

Also, when adding to the end, or printing in reverse, use the tail pointer.

Some more comments:
When coding a linked list with a tail pointer, you're pretty much guaranteed that anywhere you can change the head pointer, you can change the tail pointer too. That means all of your insert and delete functions require a node * &tail argument.

You'll find the much easier if you start by writing these two routines:

1
2
3
4
5
6
// Insert "val" before "where". If "where" is null then insert at end.
void insert_before(node * &head, node * &tail, node *where, int val);

// Delete "where" from the list.  "Where" must be a node in
// the list
void delete_node(node * &head, node * &tail, node *where);


Then all the routines to modify the list simply find the place to insert/delete and call insert_before() or delete_node().

You will also save a lot of heartache if you create a temporary function to validate the list. Call this at the top of the while loop so you validate the list after each function. That way you'll immediately know when the list gets messed up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void validate_list(node *head, node *tail)
{
    int sz=0;
    node *prev, *next;

    for (prev = NULL, next = head;
         next;
         prev = next, next = next->next) {
        if (( (sz==0 && prev == NULL) || prev->next == next)
            && next->prev == prev) {
            // okay
        } else {
            cout << "prev/next pointers aren't right\n";
        }
        ++sz;
    }
    if (prev != tail) {
        cout << "tail is bad\n";
    }
    if (sz != Size) {
        cout << "Size is " << Size << " instead of " << sz << '\n';
    }
}

Topic archived. No new replies allowed.