#include "stdafx.h" using namespace std; struct list { int val; list *next; }; void print(list *a) { list *p = a; cout << "Spisok: "; while (p != NULL) { cout << p->val; p = p->next; } } void add(list *&a, list *&head, int data) { list *p = new list; p->next = NULL; p->val = data; list *q = new list; q = head; if (a == NULL) a = p; else { while (q->next != NULL) q = q->next; q->next = new list; q = q->next; q->val = data; q->next = NULL; } a = q; } int main() { setlocale(LC_ALL, "Russian"); list *p1 = new list; list *head = NULL; add(p1, head, 5); add(p1, head, 6); print(p1); _getch(); return 0; } I can not understand what is wrong with the function of adding a new item to the list
Thanks for the advice. Here's a fix.
list *init(int a) { list *head = new list; head->val = a; head->next = NULL; return (head); } list *add(list *head, int data) { list *temp, *p; p = head->next; temp = new list; head->next = temp; temp->val = data; temp->next = p; return (temp); }