I'm going to give you some information and pseudocode so you will be able to fix this problem yourself.
You can see a linked list as a chain of items:
Head -> Item1 -> Item2 -> Item3 -> Item4 <- Tail
If you need to delete Item3, you need to change the next pointer of Item2 so it points to Item4 (The item following Item3). Then you can release Item3.
But there are some special cases:
- If you want to delete Item1 (the first item), you need to change the Head pointer, so it points to Item2.
- If you want to delete Item4 (the last item), you need to change the Tail pointer, so it points to Item3.
- If the list contains only one item and you want to delete that, you need to change both the Head and the Tail to nil.
- If the list is empty, you don't need to delete anything, but your code should not crash because of that.
So in order to fix all these, you can use 2 pointers to walk the list, (current and previous).
- Current start at the first item (Head) and previous start at nil.
- As long as current is not nil and it does not point to the item to be deleted, set previous to the value of current and set current to the value of the next item.
- If current is nil, then the item is not found and there is nothing to delete.
- If current points to the correct item, and previous is nil, you need to change the head pointer.
- If current points to the correct item and previous is not nil, set the next pointer of previous to the next pointer of current.
- If the next pointer of current is nil (you are at the tail), you also set the tail pointer to previous.
- If all pointers are changed, you can dispose of the current item.
A side note on your code. You have an insert procedure that inserts several random elements. A better idea is to have a separate procedure to insert a single element and a separate procedure to add several items like:
procedure Insert(index, value: Integer);
var
tmp: node;
begin
new (tmp);
tmp^.value := value;
tmp^.index := index;
tmp^.next:= nil;
if head = nil
then head := tmp
else
tail^.next := tmp;
tail:= tmp;
end;
procedure FillList;
var
ix: Integer;
begin
Randomize;
for i := 1 to el do
begin
inc(ix);
Insert(ix, Random(88));
end;
end;