Help write on Pascal task.

Let L be a list of integers. Describe the procedure that includes the list 0 for the first negative number. If there are no negative numbers in L, the list does not change.

The difficulty, for me, is how to create a link with zero and include it in the list after the negative element ...

type list=^elem; elem=record data:integer; next:list; end; procedure otr(l:list); var f:boolean;p:list; begin f:=false;p:=l; while (p<>nil)and not f do if p^.data<0 then begin {Вот здесь не знаю как написать} end else p:=p^.next; end; 
  • You do not know how to create an instance of elem ? Of course, dynamically, with new . Can you continue? - VladD
  • I do not know how to create a new link and insert it into the list, after the first negative one, so that the link between the links does not break. Can you write? - mango44
  • @ mango44, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

The only trick in your task is to insert it in front of the found element. To do this, you need to have a pointer to the link instead of the link itself.

 procedure otr(var l: list); { var обязателен! } var curr: ^list; savednext: list; begin curr := @list ; while (curr^ <> nil) do if curr^^.data < 0 then begin savednext := curr^^.next; new(curr^); curr^^.data := 0; curr^^.next := savednext; break; end else curr := @(curr^^.next); end; 

I hope I was not mistaken with Pascal syntax, I have not practiced for a long time.