menu prev next

POINTERS
Lets modify the previous program which introduced a separate pointer for tranversing the links of records. This time, rather than statically creating three records, we will allow the use to enter the details as the list is created.

The modified program appears below.


program PointerRecordExample3( input, output );

type  rptr = ^recdata;
	recdata = record
		number : integer;
		code : string;
		nextrecord : rptr
	end;

var	startrecord, listrecord, insertptr : rptr;
	digitcode : integer;
	textstring : string;
	exitflag, first : boolean;

begin
	exitflag := false;
	first := true;
	while exitflag = false do
	begin
		writeln('Enter in a digit [-1 to end]');
		readln( digitcode );
		if digitcode = -1 then
			exitflag := true
		else
		begin
			writeln('Enter in a small text string');
			readln( textstring );
			new( insertptr );
			if insertptr = nil then
			begin
				writeln('1: unable to allocate storage space');
				exit
			end;
			if first = true then begin
				startrecord := insertptr;
				listrecord := insertptr;
				first := false
			end
			else begin
				listrecord^.nextrecord := insertptr;
				listrecord := insertptr
			end;
			insertptr^.number := digitcode;
			insertptr^.code := textstring;
			insertptr^.nextrecord := nil
		end
        end;
        while startrecord <> nil do
        begin
	     listrecord := startrecord;
             writeln( startrecord^.number );
	     writeln( startrecord^.code );
	     startrecord := startrecord^.nextrecord;
	     dispose( listrecord )
	end
end.


The program uses three pointers. startrecord remembers the start or head of the list, listrecord is used to link between the previous record and the next/current one, and insertptr is used to create a new record which is then linked into the chain.


Copyright B Brown/P Henry/CIT, 1988-1997. All rights reserved.
menu prev next