views:

163

answers:

2

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.

+1  A: 

Good article: http://www.learn-programming.za.net/programming_pascal_learn14.html

StefanE
Yeah I know that article. Its a good one, Id like to have all that code in one file so I can tinker with it but unfortunately the link down on that page appears to be broken.
NumberFour
This is also the first Google hit for "pascal linked list".
asveikau
+1 @NumberFour, that code is all you need. You can't find better ready-made code for two reasons: (1) This is a very basic data structure, not very useful in everyday life. (2) Any *real* implementation is going to have too much "decorative" code around everything. Learning the Linked List is all about dealing with pointers, allocating memory, freeing memory, getting Access Violations when you get it wrong. It's not actually about the data structure.
Cosmin Prund
+4  A: 

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.

Aif