How do I delete a current node in a linked list in c++? I have studied many sites, but my compiler refuses to accept anything

this is my first question on StackOverflow, so please forgive me if I did not fully indicate something somewhere, I will try to correct it

I have been trying to solve this problem for quite a long time, but it doesn't work out. The compiler shows that the problem is in void Show, but all functions except node deletion work with void Show correctly. Here's my code:

#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iterator>
using namespace std;

struct element {
int x, y;
element* Next, * next;
element* Prev, * prev;
};
creating class

class List {
element* Head, * head;
element* Tail, * tail;
public:
List() {
Head == NULL;
head == NULL;
Tail == NULL;
tail == NULL;
}
~List();
void Del();
void Add(int x);
void Show();
};

List::~List() {
while (Head != NULL) {
element* temp = Head->Next;
delete Head;
Head = temp;
}
}
here is my function

void List::Del() {
element* temp = new element; //node to delete
element* temp1 = new element; //prev node
int y; //y - node to delete
temp = Head;
cout << "which node do you want to delete? " << endl;
cin >> y;

if (temp != NULL) {
for (int i = 1; i < y; i++) {
temp1->x = temp->x;
cout << "temp1 = " << temp1->x << endl;
temp = temp->Next;
cout << "temp = " << temp->x << endl;
}
if (temp != NULL) {
temp1->Next = temp->Next;
free(temp);
}
}
}
adding a node

void List::Add(int x) {
element* temp = new element;
temp->x = x;
temp->Next = https://www.myaccountaccess.us/;

if (!Head) {
temp->Prev = NULL;
Head = Tail = temp;
}
else {
temp->Prev = Tail;
Tail->Next = temp;
Tail = temp;
}
}
displaying the list

void List::Show() {
element* temp = new element;
temp = Head;
while (temp != NULL) {
cout << temp->x << " ";
temp = temp->Next;
}
}
Start

int main()
{
int x;
List L1;

int N;
cout << "Enter N: \nN = ";
cin >> N;
filling in the list

for (int i = 0; i < N; i++) {
cout << i + 1 << ".x = ";
cin >> x;
L1.Add(x);
}

cout << "\nL1: ";
L1.Show();
removal

cout << "\nTry to delete: " << endl;
L1.Del();
cout << "\nResult: " << endl;
what the problem is with

L1.Show();

return 0;
}
when I run the code, all the functions work correctly, but there is some problem with the deletion: when I try to display the list after using this function, Visual Studio throws an error: Exception raised: read access violation. temp was 0xDDDDDDDD

more details: The problem with my code is this: Let's say we have a list of 7 items that the user enters himself. And from this list, you need to remove the node specified by the user. Let's say the third one. Input data: N = 7, list: 2 12 12 43 54 56 67, number of the node being deleted: 3. Here is what the compiler outputs: 2 12 -842150451 and the process is interrupted
Last edited on
This is not StackOverflow.

Neither in Show() nor in Del() there should be a new. Do not use free(...) for memory allocated with new.

1
2
void List::Show() {
element* temp = Head; // No new here that causes just memory leads 


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
void List::Del() {
element* temp = Head; // No new here that causes just memory leads
int y; //y - node to delete
cout << "which node do you want to delete? " << endl;
cin >> y;

if (temp != NULL) {
for (int i = 1; i < y; i++) {
temp = temp->Next;
if(temp)
  cout << "temp = " << temp->x << endl;
else
  break;
}
if (temp != NULL) {
  if(temp->Prev) // Note: temp->Prev is nullptr it is Head
  {
    if(temp->Next) // Note: temp->Prev is nullptr it is Tail
    {
      element* temp_prev = temp->Prev;
      temp->Next = temp->Next->Next;
      temp->Prev = temp_prev
    }
    else if(Tail->Prev)
      Tail->Prev = Tail->Prev->Prev;
  }
  else if(Head->Next)
    Head->Next = Head->Next->Next;
  delete temp;
}
}
Not tested!
Last edited on
Try this:

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
#include <iostream>
using namespace std;

struct element {
	int x {}, y {};
	element *Next {};
	element *Prev {};
};

class List {
	element *Head {};
	element *Tail {};

public:
	List() {}
	~List();
	void Del();
	void Add(int x);
	void Show();
};

List::~List() {
	while (Head != nullptr) {
		const auto temp {Head->Next};

		delete Head;
		Head = temp;
	}
}

void List::Del() {
	auto temp {Head};

	if (temp != nullptr) {
		if (temp == Head && temp == Tail) {
			cout << "Only 1 element. Deleting\n";
			Head = Tail = nullptr;
		} else {
			int y {};

			cout << "which node do you want to delete? ";
			cin >> y;

			for (int i = 1; temp && i < y; ++i) {
				temp = temp->Next;
				if (temp)
					cout << "temp = " << temp->x << '\n';
			}

			if (temp != nullptr) {
				if (temp->Prev) {
					if (temp->Next) {
						temp->Prev->Next = temp->Next;
						temp->Next->Prev = temp->Prev;
					} else
						if (Tail->Prev) {
							Tail->Prev->Next = nullptr;
							Tail = Tail->Prev;
						}
				} else
					if (Head->Next) {
						Head = Head->Next;
						Head->Prev = nullptr;
					}
				delete temp;
			} else
				cout << "Not enough nodes!\n";
		}
	} else
		cout << "Empty list\n";
}

void List::Add(int x) {
	auto temp {new element};

	temp->x = x;
	temp->Next = nullptr;

	if (!Head) {
		temp->Prev = nullptr;
		Head = Tail = temp;
	} else {
		temp->Prev = Tail;
		Tail->Next = temp;
		Tail = temp;
	}
}

void List::Show() {
	auto temp {Head};

	while (temp != nullptr) {
		cout << temp->x << " ";
		temp = temp->Next;
	}
}

int main()
{
	int x {};
	List L1;
	int N {};

	cout << "Enter N: \nN = ";
	cin >> N;

	for (int i = 0; i < N; ++i) {
		cout << i + 1 << ".x = ";
		cin >> x;
		L1.Add(x);
	}

	cout << "\nL1: ";
	L1.Show();

	cout << "\nTry to delete:\n";
	L1.Del();

	cout << "\nResult:\n";

	L1.Show();
}

Topic archived. No new replies allowed.