Exception thrown at 0x00A12849 in DLL.exe: 0xC0000005: Access violation reading ocation 0xDDDDDDDD.
Ошибка возникает при синтаксическом анализе, и я не знаю, почему. Я написал тот же самый код, что и в книге, они только написали функцию del, а не функцию синтаксического анализа. PLS скажите, почему эта ошибка возникает в этом коде и как этого избежать в будущем. также помогают написать правильную функцию синтаксического анализа. спасибо всем заранееОшибка при анализе через связанный список
#include<iostream>
using namespace std;
struct list
{
int data;
list *next;
list *prev;
};
list* createNode()
{
list *node = new list;
node->next = NULL;
node->prev = NULL;
return node;
}
void insert(list**head,int data,int position)
{
int k = 1;
list *current, *temp, *node = createNode();
current = NULL;
temp = *head;
node->data = data;
if (position==1)
{
node->next = *head;
*head = node;
if (*head)
{
(*head)->prev = node;
}
}
else
{
while (temp->next!= NULL&&k < (position-1))
{
temp = temp->next;
k++;
}
node->next = temp->next;
node->prev = temp->prev;
if (temp->next)
temp->next->prev = node;
temp->next= node;
}
}
void del(list**head, int position)
{
int k = 1;
list *current, *temp;
current = NULL;
temp = *head;
if (position == 1)
{
*head = (*head)->next;
if (*head != NULL)
(*head)->prev = NULL;
delete temp;
return;
}
while (k < position&&temp->next != NULL)
{
temp = temp->next;
k++;
}
current = temp->prev;
current->next = temp->next;
if (temp->next)
temp->next->prev = current;
delete temp;
return;
}
void parse(list * temp)
{
while (temp != NULL)
{
cout << "->" << temp->data;
temp = temp->next;
}
}
void main()
{
struct list *head = NULL;
start:
int choice;
cin >> choice;
switch (choice)
{
case 1: int data, position;
cout << "\ndata:"; cin >> data; cout << "\nposition:";
cin>>position;
insert(&head, data, position);
goto start;
case 2:parse(head);
goto start;
case 3: cout << "\nposition:"; cin >> position;
del(&head, position);
goto start;
default: break;
}
exit(0);
}
Почему вы используете 'goto' вместо цикла? –
@JoachimPileborg Этот код имел бы общую проблему, требующую «разбить» цикл изнутри «switch», поэтому требуется некоторая форма нечистого управления потоком. Выбор 'goto start' является более уродливым, чем каждый из нескольких других способов. Но это не так просто, как просто использовать цикл. – JSF