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

  • Once you have done del->next = New; , the expression del->next->next is the same as New->next . That is, the subsequent New->next = del->next->next; - it's just pointless New->next = New->next; . Assigning garbage to yourself. - AnT
  • Usually, recursion is introduced in the listlist, for faster searching, something like Node* 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. - NewView
  • In your code, you need: New->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
  • I wanted to write an example, but I was too lazy, Google knows everything about leaflets and double leaflets 1 and double leaflets 2 :) - NewView
  • @ AnT, but I'm just trying to establish a connection between the elements available and the one being inserted, how else can I turn it on? - Elvin

1 answer 1

Got it

 Node* add1(Node* head,Node* del) { Node *New = (Node*)malloc(sizeof(Node)); New->Data = 777; New->next = del->next; del->next = New; return head; }