Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
DYNAMIC.PAS
◄Example► ◄Contents► ◄Index► ◄Back►
PROGRAM dynamic;
{ DYNAMIC.PAS illustrates the use of dynamic variables with the
following procedures:
Dispose New
It also uses two procedures to generate random numbers:
Random Randomize
}
USES
Crt;
CONST
program_name = 'DYNAMIC ';
program_desc = 'builds a simple linked list of records.';
TYPE
record_ptr_t = ^record_t;
record_t = RECORD
name : STRING[40];
id : Integer;
amount : Real;
next_rec : record_ptr_t;
END;
VAR
record_ptr : record_ptr_t;
first_record : record_ptr_t;
answer : Char;
BEGIN
Writeln( program_name, program_desc );
Writeln;
Writeln( program_name,
'will prompt you for a string for each record,' );
Writeln( 'and will randomly generate two additional fields.' );
Write( 'Type a string up to 40 characters long in response to ' );
Writeln( 'the Name: prompt.' );
Randomize;
{ Build simple linked list. }
first_record := NIL;
REPEAT
New( record_ptr );
WITH record_ptr^ DO
BEGIN
Write( 'Name: ' );
Readln( name );
id := Random( 255 );
amount := Random * 10;
Writeln( 'Random id:', id:4, ' Random real: ', amount:3:2 );
next_rec := first_record;
END;
first_record := record_ptr;
REPEAT
Write( 'More? (''Y'' or ENTER to continue; ''N'' to quit)' );
answer := ReadKey;
Writeln( answer );
answer := UpCase( answer );
UNTIL (answer = 'Y') OR (answer = 'N') OR (answer = Chr( 13 ));
UNTIL (answer = 'N');
{ Traverse the list, print its contents, and release pointers. }
WHILE (record_ptr <> NIL) DO
BEGIN
Writeln;
WITH record_ptr^ DO
BEGIN
Writeln( name );
Writeln( id:4 );
Writeln( amount:3:2 );
END; { WITH statement }
Dispose( record_ptr );
record_ptr := record_ptr^.next_rec;
END;
END.