Algorithm - Deleting an item from a contiguous list(array)

We have two cases of deletion:

Delete at the beginning and middle:

In this case, we need to look for the position of the element to be deleted. The operation is carried out in two steps:
  • Crush" the element has the k index by shifting all the others that have an index  superior.
  • Decrement the size of the array with 1.

Delete at end:

This is a very simple case, just decrement the size of the array with 1.

The algorithm that combines the two cases:

Delete algorithm;
Var 
  i,k : integer;
  T[1..n]: integer;
Start
  for i from k to n-1
  faire
    T[i] := T[i+1];
  finfaire
  n := n-1;
End

Procedure:

Procedure delete(L:list,k:1..lmax)
var i,n:1..lmax;
Start
  n := L.length;
  for i from k to n-1
  faire
    L.T[i] := L.T[i+1];
  finfaire
  L.length:= n-1;
Fin




See also:

Algorithm |  Inserting and deleting an item in a linked list.
Algorithm |  Contiguous and chained lists.
Algorithm |  Inserting an element into a contiguous list(array).
C/C++ |  Deleting a value from an array.