{ Zeiger - Demo, Datenstrukturen SS03, Benn }
{ Autor: Dipl.-Ing. Thomas Speiser          }
{ Source from: http://Codes.TurboTools.de   }

program Zeigerdemo;
uses crt;

type zeiger = ^element;

     element = record
      data: integer;
      next: zeiger;
     end;

var p: zeiger;
    count: integer;
    c: char;

procedure Out_List(p: zeiger);
begin
while (p <> nil) do begin
write('p: ',p^.data);
write(' -> ');
p:=p^.next;
end;
if p = nil then write('nil');
writeLN;
end;

{ STANDARD-Proceduren }
function Is_Empty(p: zeiger): boolean;
begin
is_Empty:=(p = nil);
{ if (p = nil) then writeLN('LISTE LEER!'); }
end;

procedure Ins_First(var p: zeiger; i: integer);
var h: zeiger;
begin
h:=p;
new(p);
p^.data:=i;
p^.next:=h;
end;

procedure ins_Last(var p: zeiger; i: integer);
var h: zeiger;
begin
if is_Empty(p) then ins_First(p,i) else begin
h:=p;
while h^.next <> nil do h:=h^.next;
new(h^.next);
h^.next^.data:=i;
h^.next^.next:=nil;
end;
end;

procedure Make_List(var p: zeiger; var count: integer);
var i: integer;
begin
for i:=1 to count do ins_last(p,i);
writeLN('Out_List:');
Out_List(p);
end;

procedure Del_List(var p: zeiger);
var h: zeiger;
begin
if not(is_empty(p)) then begin
while (p <> nil) do begin
h:=p^.next;
dispose(p);
p:=h; end;
writeLN('Liste gel”scht!'); end;
end;

procedure Del_First(var p: zeiger);
var h: zeiger;
begin
if not(is_empty(p)) then begin
h:=p^.next;
dispose(p);
p:=h;
end;
end;

procedure Count_Set(p: zeiger);
var h: zeiger;
    i: integer;
begin
i:=0;
while (p <> nil) do begin p:=p^.next; inc(i); end;
write('Count_Set(',i,'): ');
readLN(count)
end;

procedure Such_Ele(p: zeiger);
var i: integer;
    h: zeiger;
begin
h:=p;
write('Such_Ele: ');
readLN(i);
while (h <> nil) do begin
if h^.data = i then writeLN('Element gefunden!');
h:=h^.next; end;
end;

procedure Reverse_List(var p: zeiger);
var h1, h2: zeiger;
begin
h1:=nil;
while (p <> nil) do begin
h2:=h1;
new(h1);
h1^.data:=p^.data;
h1^.next:=h2;
p:=p^.next; end;
p:=h1;
Out_List(p);
end;

procedure Menu_Set;
begin
ClrScr;
writeLN('Z E I G E R - D E M O ! ! !');
writeLN;
writeLN('m - Make_List');
writeLN('o - Out_List');
writeLN('d - Del_First');
writeLN('l - Del_List');
writeLN('g - Such_Ele');
writeLN('r - Reverse_List');
writeLN('_______________');
writeLN('c - Count_set');
writeLN('e - Menu_set');
writeLN('q - Ende_Gel„nde');
writeLN;
end;

begin
TextMode(CO80 xor Lo(LastMode)+Font8x8 xor LASTMODE);
count:=5;
Menu_Set;
repeat
c:=readkey;
case c of
'm': Make_List(p,count);
'o': Out_List(p);
'd': del_first(p);
'l': del_list(p);
'g': Such_Ele(p);
'r': Reverse_List(p);
{________________}
'e': Menu_Set;
'c': Count_Set(p);
'q': Halt(1);
end;
until count = 0;
end.