As a matter of fact, I create a list, fill it with some numbers, and then I want to remove the element with the key = 6 , but it does not work, even after dispose(b) it manages me to display the result 6 without problems, how is it? The brain cannot enter, but I deleted the cell with the number b from the memory, well, or something like that

 type stack = ^st; st = record data:Integer; next:stack; end; var i,k:Integer; b,a,c:stack; begin new(b); b^.data:=0; a:=b; for i := 1 to 9 do begin new(b^.next); b:=b^.next; b^.data:=i; end; k:=6; c:=a; while c<>nil do begin if c^.data = k then begin b:=c; c:=c^.next; break; end; c:=c^.next; end; dispose(b); WriteLn(b^.data); { while a<>nil do begin WriteLn(a^.data); a:=a^.next; end; } ReadLn; end. 

    2 answers 2

    Incorrectly deleting an item from the list. To delete, you need to change the next field of the element that precedes the deleted element on the next field. And here, just a pointer to the current element (not connected in any way with the previous element) is changed to a pointer to the next element.

    PS I see a comparison of c with nil, but I do not see any next element of nil being recorded.

    • Thanks really, I didn’t make the right corrections while c <> nil do begin if c ^. data = k then break; b: = c; {previous element} c: = c ^ .next; {element with value k} end; b ^ .next: = c ^ .next; {translate the next pointer next to the next pointer found} dispose (c); {free memory} - Eugene536
     while c<>nil do begin if c^.data = k then break; b:=c;{предыдущий элемент} c:=c^.next;{элемент со значением k} end; b^.next:=c^.next;{перевожу указатель next предыдущего на указатель next найденного} dispose(c);{освобождаю память} 
    • one
      Yes, it seems right. As for nil, after the initialization loop, you need to insert b^.next:=nil; - insolor