The problem is in the Insert function. When referring to t->a , it indicates that t == nullptr
#include <iostream> #include <conio.h> using namespace std; struct List { int a; List* next; }; void Print(List* c) { List* print = c; while (print) { cout << print->a << "->"; print = print->next; } cout << "NULL" << endl; } void Add_begin(List** begin) { List* t = new List; t->a = NULL; t->next = NULL; cin >> t->a; t->next = *begin; *begin = t; return; } void Add_end(List* end) { List* t = new List; t->a = NULL; t->next = NULL; cin >> t->a; end->next = t; return; } void Insert(List** begin) { List* ins = new List; ins->a = NULL; ins->next = NULL; cin >> ins->a; if (*begin = NULL) { *begin = ins; return; } List* t = *begin; if (t->a > ins->a) { ins->next = t; *begin = ins; return; } List* t1 = t->next; while (t1) { if (ins->a >= t->a && ins->a <= t1->a) { t->next = ins; ins->next = t1; return; } t = t1; t1 = t1->next; } t->next = ins; ins->next = NULL; } int main() { List* begin = new List; begin->a = NULL; begin->next = NULL; cin >> begin->a; List* end = begin; for (int i = 0; i < 4; i++) { end->next = new List; end = end->next; cin >> end->a; end->next = NULL; } Print(begin); //Add_begin(&begin); //Print(begin); Add_end(end); Print(begin); Insert(&begin); Print(begin); _getch(); return 0; }
t->a = NULL;- it's crooked. - AnTList. - PinkTux