-1

I have made a piece of code that works in simple linked list node in pascal. Now I need add a procedure that can delete or remove an element in my simple linked list. I have made many procedures that do test and adding but I can't figure out how to add a procedure that can remove a item. Here is my code:

type
  node = ^MyRec;
  MyRec = record
    value: Integer;
    index: integer;
    next: node
  end;

var
  head, tail: node;
  i, ix: Integer;
const
  el = 14;

procedure Insert;
var
  tmp: node;
  ix: Integer;
begin
  Randomize;
  for i := 1 to el do
  begin
    new (tmp);
    inc(ix);
    tmp^.value := Random(88);
    tmp^.index := ix;
    tmp^.next:= nil;
    if head = nil
      then head := tmp
    else
      tail^.next := tmp;
    tail:= tmp;
  end;
end;

//displays the list
procedure Display;
var
  tmp: node;
begin
  tmp := head;
  while tmp <> nil do
  begin
    WriteLn('Val=', tmp^.value, ' ID=', tmp^.index);
    tmp := tmp^.next
  end;
end;

begin
  head := nil;
  ReadLn;
end.
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
kilis
  • 151
  • 2
  • 8
  • Please fix indentation and try to remove all unnecessary code, like "IS_FULL" and the rest that is irrelevant to this question. – Lasse V. Karlsen Mar 05 '14 at 20:52
  • @LasseV.Karlsen Edited my code and posted it. – kilis Mar 05 '14 at 21:15
  • 1
    Is this supposed to be a single or double linked list? You hae a `prev` field but you do not seem to be setting it anywhere. – 500 - Internal Server Error Mar 05 '14 at 21:55
  • @500-InternalServerError it supposte to be single. It is my error that i added prev. I was trying to understand how a linked list works. Now i edited that out. Also i have a task to add my list a end pointer. How that works? – kilis Mar 06 '14 at 05:31

1 Answers1

2

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).

  1. Current start at the first item (Head) and previous start at nil.
  2. 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.
  3. If current is nil, then the item is not found and there is nothing to delete.
  4. If current points to the correct item, and previous is nil, you need to change the head pointer.
  5. If current points to the correct item and previous is not nil, set the next pointer of previous to the next pointer of current.
  6. If the next pointer of current is nil (you are at the tail), you also set the tail pointer to previous.
  7. 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;
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202