После очистки списка не могу выполнить не одну программу
Пишу программу работы со списками. После очистки списка, если я хочу посмотреть список или начать снова добавлять, то выходят ошибки примерно как на фото или похожие на них. Подскажите как исправить и почему выходят такие ошибки. Это моя программа:
#include <iostream>
#include <Windows.h>
using namespace std;
struct node
{
int elem;
node* next;
node* prev;
};
struct lists
{
private:
node* head;
node* end;
int quanity;
public:
lists()
{
head = end = new node();
quanity = 0;
}
int count()
{
return quanity;
}
node* find(int el)
{
node* curr = head->next;
while (curr != nullptr)
{
if (curr->elem == el)
{
return curr;
}
curr = curr->next;
}
return nullptr;
}
node* find(int el, node*& aft)
{
node* curr = head->next;
aft = head;
while (curr != nullptr)
{
if (curr->elem == el)
{
return curr;
}
aft = curr;
curr = curr->next;
}
return nullptr;
}
bool index(int el, int& ind)
{
node* curr = head->next;
ind = 0;
while (curr != nullptr)
{
if (curr->elem == el)
{
return true;
}
curr = curr->next;
ind++;
}
return false;
}
void push_back(int el)
{
node* curr = new node();
curr->elem = el;
end->next = curr;
end = curr;
quanity++;
}
void push_forward(int el)
{
node* curr = new node();
curr->elem = el;
curr->next = head->next;
head->next = curr;
if (end == head)
{
end = curr;
}
quanity++;
}
void change(int el1, int el2)
{
node* aft;
node* curr = find(el1, aft);
if (curr != nullptr)
{
curr->elem = el2;
}
}
void remove(int el)
{
node* aft;
node* curr = find(el, aft);
if (curr == nullptr)
{
return;
}
if (aft == nullptr)
{
head = curr->next;
}
else {
aft->next = curr->next;
}
if (curr->next == nullptr)
{
end = aft;
}
delete curr;
quanity--;
}
void print() const
{
if (head->next == nullptr)
{
cout << "Список пуст" << endl;
return;
}
node* current = head->next;
while (current != nullptr)
{
cout << current->elem << " ";
current = current->next;
}
cout << endl;
}
void clear()
{
node* current = head;
while (current != nullptr)
{
node* next = current->next;
delete current;
current = next;
}
head = nullptr;
end = nullptr;
quanity = 0;
cout << "Список успешно очищен." << endl;
}
};
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
lists myList;
int el, el2;
int c;
int choice;
int ch;
while (true)
{
cout << "Введите команду" << endl;
cout << "1 - работа с двусвязным списком " << endl;
cout << "2 - выйти" << endl;
cin >> ch;
if (ch == 2)
{
exit(0);
break;
}
if (ch == 1)
{
while (true)
{
cout << "Введите команду" << endl;
cout << "1 - добавить элемент в конец" << endl;//
cout << "2 - добавить элемент в начало" << endl;
cout << "3 - удалить элемент" << endl;
cout << "4 - изменить элемент" << endl;
cout << "5 - показать список" << endl;
cout << "6 - очистить список" << endl;
cout << "7 - назад" << endl;
cin >> c;
if (c == 15)
{
break;
}
switch (c)
{
case 1:
cout << "Введите элемент: ";
cin >> el;
myList.push_back(el);
cout << "Элемент добавлен в конец списка" << endl;
break;
case 2:
cout << "Введите элемент: ";
cin >> el;
myList.push_forward(el);
cout << "Элемент добавлен в начало списка" << endl;
break;
case 3:
cout << "Введите элемент: ";
cin >> el;
myList.remove(el);
cout << "Элемент удален из списка." << endl;
break;
case 4:
cout << "Введите элемент для замены: ";
cin >> el;
cout << "Введите новое значение элемента: ";
cin >> el2;
myList.change(el, el2);
cout << "Элемент заменен" << endl;
break;
case 5:
myList.print();
break;
case 6:
{
myList.clear();
break;
}
case 7:
break;
default:
cout << "Некорректный ввод. Попробуйте еще раз." << endl;
break;
}
}
}
}
return 0;
}
