struct Node { int Data; Node* next; }; This is the structure
Thus made an addition and by this call inserted
for (int i = 0; i < 5; i++) a = add(a, i); Node* add(Node *head, int Value) { Node *New = (Node*)malloc(sizeof(Node)); New->Data = Value; New->next = head; return New; } But now I try to insert the following element from del new, but so that it spreads the linear list and stands between the second and the first. And something went wrong. So I find the desired item:
Node* Find(Node *head, int Value) { while (head) { if (head->Data == Value) return head; head = head->next; } return NULL; } And the export itself looks like this: Node * del = Find (a, 2);
Node* add1(Node* head,Node* del) { Node *New = (Node*)malloc(sizeof(Node)); New->Data = 777; del->next = New; New->next = del->next->next; return head; } I'm trying to insert the number 777 between the second and the first. The problem in the last function, I do not understand how to fix
del->next = New;, the expressiondel->next->nextis the same asNew->next. That is, the subsequentNew->next = del->next->next;- it's just pointlessNew->next = New->next;. Assigning garbage to yourself. - AnTNode* prev;and it is more convenient (more understandable) to insert and you can search in 2 directions at once. In this case, prev = structure before, next = after. For the previous structure, next = inserted, for the next prev = inserted structure. - NewViewNew->next = del; return New;New->next = del; return New;like so, there is nothing to try now :) the truth is unclear in what form the head gets there, in what place .. - NewView