Hi,
Im looking for a good and simple implementation of a linked list in Pascal. As Im a Pascal beginner it would help me alot as a study material.
Thanks for any tips.
Hi,
Im looking for a good and simple implementation of a linked list in Pascal. As Im a Pascal beginner it would help me alot as a study material.
Thanks for any tips.
I have found back in my old mails an implementation we had done. The code is "in french" so I won't touch it in order not to forget a function somewhere which would break the compilation :-)
First of all, we have an "element" type, to let us easily change what we want to store in the list:
unit U_ELEMENT;
interface
{ We store integers }
type
T_ELEMENT = Integer;
{ Reads an element }
procedure lireElement(out res:INTEGER; out code:WORD; out ch:STRING);
{ Write and element }
procedure ecrireElement(const t:T_ELEMENT);
implementation
procedure lireElement(out res:T_ELEMENT; out code:WORD; out ch:STRING);
begin
write('> ');readln(ch);
val(ch,res,code);
end;
procedure ecrireElement(const t:T_ELEMENT);
begin
write(t);
end;
end.
Then, the actual list module, which is designed to add elements at the beginning of the list:
unit U_LISTE;
interface
uses U_ELEMENT;
const LISTEVIDE = NIL;
type
T_LISTE = ^T_CELLULE;
T_CELLULE = record
info: T_ELEMENT; { The stored information }
suivant: T_LISTE; { Pointer to the next element }
end;
{ Add an heading element }
function ajouteEnTete(e:T_ELEMENT;l:T_LISTE):T_LISTE;
{ returns the head of the list }
function tete(l:T_LISTE):T_ELEMENT;
{ returns the list, without the head }
function reste(l:T_LISTE):T_LISTE;
{ List empty? }
function estListeVide(l:T_LISTE):BOOLEAN;
{ Modify the header element }
procedure modifierTete(var l:T_LISTE;const e:T_ELEMENT);
{ Modify the list after the head }
procedure modifierReste(var l1:T_LISTE; const l2:T_LISTE);
implementation
function ajouteEnTete(e:T_ELEMENT;l:T_LISTE):T_LISTE;
var l1:T_LISTE;
begin
new(l1);
l1^.info := e;
l1^.suivant := l;
ajouteEnTete := l1;
end;
function tete(l:T_LISTE):T_ELEMENT;
begin
tete := l^.info;
end;
function reste(l:T_LISTE):T_LISTE;
begin
reste := l^.suivant;
end;
function estListeVide(l:T_LISTE):BOOLEAN;
begin
estListeVide := l=NIL;
end;
procedure modifierTete(var l:T_LISTE;const e:T_ELEMENT);
begin
l^.info := e;
end;
procedure modifierReste(var l1:T_LISTE; const l2:T_LISTE);
begin
l1^.suivant := l2;
end;
end.
And a small test program:
program testeliste;
uses U_ELEMENT,U_LISTE;
procedure afficherListe(const l:T_LISTE);
var l1: T_LISTE;
vide: BOOLEAN;
begin
write('(');
l1 := l;
vide := estListeVide(l1);
while not(vide) do
begin
ecrireElement(tete(l1));
l1 := reste(l1);
vide := estListeVide(l1);
if not(vide) then
write(',');
end;
write(')');
writeln;
end;
var res:T_ELEMENT;
code:WORD;
ch:STRING;
i:INTEGER;
l:T_LISTE;
Begin
l:=LISTEVIDE;
for i:=0 to 9 do
begin
lireElement(res,code,ch);
l := ajouteEnTete(res,l);
end;
afficherListe(l);
afficherListe(reste(l));
afficherListe(reste(reste(reste(l))));
afficherListe(ajouteEnTete(tete(l),l));
End.
As I said, this is an old (very old) program made when I started learning CS, so it may not fit :-), but I think it helps with the syntax and the global idea.