Ошибка в выводе списка
1) Добавляю верхний элемент 2) Удаляю его 3) Вывожу список, но выходи ошибка, не могу разобраться
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node
{
int data;
Node* next;
};
struct List
{
Node* head;
};
inline void init(List& list)
{
list.head = NULL;
}
Node* create_node(int data = 0)
{
Node* temp;
temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
void push_back(List& list, int data = 0)
{
if (list.head == NULL)
list.head = create_node(data);
else
{
Node* temp = create_node(data);
Node* it = list.head;
while (it->next != NULL)
it = it->next;
it->next = temp;
}
}
void push_up(List& list, int data = 0)
{
if (list.head == NULL)
{
list.head = create_node(data);
}
else
{
Node* temp = create_node(data);
temp->next = list.head;
list.head = temp;
}
}
void print(List& list)
{
if (list.head == NULL)
{
cout << "Empty list " << endl;
}
else
{
Node* it = list.head;
while (it!= NULL)
{
cout << it->data << " ";
it = it->next;
}
cout << endl;
}
}
void delete_list(List& list)
{
if (list.head != NULL)
{
Node* it = list.head, * temp;
while (it->next != NULL)
{
temp = it;
it = it->next;
delete temp;
}
delete it;
list.head = NULL;
}
}
void delete_one(List& list)
{
if (list.head != NULL)
{
delete list.head;
}
}
int search(List&list, int data)
{
int i;
i=0;
Node* temp = list.head;
while ((temp->data != data) || (temp->next = NULL))
{
++i;
temp = temp ->next;
}
if (data != NULL) cout << i+1;
return i;
}
int main()
{
setlocale(LC_ALL, "russian");
List list;
init(list);
int n, X,Y;
do {
cout << "При вводе единицы(1) вы вставите число в начало списка: " << endl;
cout << "При вводе двойки(2) вы удалите число из начала списка: " << endl;
cout << "При вводе тройки(3) вы вывидете список: " << endl;
cout << "При вводе четверки(4) вычислим расположение введенного вами элемента относительно первого элемента и его наличие в списке: " << endl;
cout << "При вводе тройки(5) вы удалите список: " << endl;
cout << "При нуле программа(0) завершается: " << endl;
cin >> n;
switch (n) {
case 1:
cout << "Введите число в начало: " << endl;
cin >> X;
push_up(list, X);
break;
case 2:
delete_one(list);
break;
case 3:
print(list);
break;
case 4:
cout << "Введите число для поиска: " << endl;
cin >> Y;
search(list, Y );
break;
case 5:
delete_list(list);
break;
case 0:
break;
default:
cout << "Ошибка. Введите другое число: " << endl;
}
} while (n != 0);
system("pause");
return 0;
}