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