home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
insidetp
/
1990_06
/
normdemo.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1990-05-10
|
2KB
|
98 lines
PROGRAM Normal_List_Demo; { NORMDEMO.PAS }
USES Crt;
CONST
FileName = 'SAMPLE.DAT';
TYPE
Ptr = ^ItemRec;
ItemRec = RECORD Data: String[40];
Next: Ptr;
Last: Ptr; END;
VAR
Index : Ptr; Mem1, Mem2 : Longint;
PROCEDURE FreeList;
BEGIN
WHILE Index^.Last <> NIL DO
Index := Index^.Last;
WHILE Index <> NIL DO
BEGIN
Dispose( Index ); Index := Index^.Next;
END;
END;
PROCEDURE AddToList( Str: String );
VAR
Local : Ptr;
BEGIN
WriteLn( 'adding: ', Str );
New( Local ); Local^.Data := Str;
IF( Index = NIL ) THEN Local^.Last := NIL
ELSE Local^.Last := Index;
Local^.Next := NIL; Index^.Next := Local;
Index := Local;
END;
PROCEDURE ReadFile( Fn: String );
VAR
F : Text; Str : String;
BEGIN
Assign( F, Fn );
{$I-} Reset( F ); {$I+}
IF IOResult <> 0 THEN
BEGIN
WriteLn( Fn, ' not found -- exiting!' );
Halt;
END;
REPEAT
ReadLn( F, Str ); AddToList( Str );
UNTIL Eof( F );
END;
PROCEDURE ShowList;
VAR
P1 : Ptr;
BEGIN
P1 := Index;
WHILE P1^.Last <> NIL DO P1 := P1^.Last;
REPEAT
WriteLn( P1^.Data ); P1 := P1^.Next;
UNTIL P1 = NIL;
END;
PROCEDURE SortList;
VAR
P1, P2, P3 : Ptr; { local pointers }
BEGIN
P1 := Index;
WHILE P1^.Last <> NIL DO P1 := P1^.Last;
REPEAT
P2 := P1^.Next;
REPEAT
IF P1^.Data > P2^.Data THEN
BEGIN
Write('*');
P3^.Data := P1^.Data;
P1^.Data := P2^.Data;
P2^.Data := P3^.Data;
END;
P2 := P2^.Next;
UNTIL P2 = NIL;
P1 := P1^.Next;
UNTIL P1^.Next = NIL;
WriteLn( ' sorted' );
END;
BEGIN
ClrScr; Mem1 := MemAvail;
Index := NIL; { initialize Index }
ReadFile( FileName ); { read data }
SortList; { sort data }
Mem2 := MemAvail;
ShowList; { display data }
FreeList;
WriteLn( 'Memory -- initial = ', Mem1:7 );
WriteLn( 'Memory -- w/ list = ', Mem2:7 );
WriteLn( 'Memory -- dispose = ', MemAvail:7 );
ReadLn;
END.