(* yhteen suuntaan linkitetty lista taulukossa *)

const 	maxlength = 100;							(* talletusalueen koko *)
type	LIST = ^LISTRECORD;
		LISTRECORD = record
				elem : array[1..maxlength] of ELEMENT;
				next : array[1..maxlength] of LIST_POSITION;
				firstused,
				firstfree : LIST_POSITION
			end;

const		maxposition = maxlength +1;
type		LIST_POSITION = 1..maxposition;


function LIST_EOL(L : LIST) : LIST_POSITION;
begin
	LIST_EOL := maxposition
end;

procedure LIST_CREATE(var L : LIST);
	var i : integer;
begin
	new(L);
	for i := 1 to maxlength do
		L^.next[i] := i + 1;
	L^.firstused := maxposition;				(* lista aluksi tyhjä *)
	L^.firstfree := 1							{ ja kaikki paikat vapaita }
end;

procedure LIST_INSERT(L : LIST; p : LIST_POSITION; x : ELEMENT);
	var i : integer;
begin
	if L^.firstfree <> maxposition then			{on vielä tilaa}
		begin
		i := L^.firstfree;						{tilanvaraus}
		L^.firstfree := L^.next[L^.firstfree];
		if p <> maxposition then				{lisäysasema <> EOL}
			begin
			L^.elem[i] := L^.elem[p];			{entisen kopionti}
			L^.next[i] := L^.next[p];
			L^.elem[p] := x;					{uusi paikalleen}
			L^.next[p] := i
			end
		else									{lisäysasema EOL}
			begin
			L^.elem[i] := x; 					{uusi paikalleen}
		 	L^.next[i] := maxposition;
			if L^.firstused = maxposition then	{lista oli tyhjä}
				L^.firstused := i
			else								{lista ei tyhjä}
				L^.next[L^.lastused] := i;
			L^.lastused := i
			end
		end
end;

