LINKECHO.ATR Disk Image
LINKECHO.P is a simple Linked List example program. A Linked List is an abstract data structure that can grow or shrink as needed.
Here, the Linked List data structure is created as well as few pointers to it.
Next, the user can enter some numbers with a new Linked List node or element added to the list to hold each entered number.
Once the user is done entering numbers, the list is traversed and the numbers are printed out.
In this example, the Linked List holds a integer value as its payload, but it can be easily modified to carry data for a monster, a spacecraft, dungeon room data, etc.
Source Code
(* LINKECHO.P *)
program LinkEcho(intput,output);
type
ElementPointer = ^Element;
Element = record
Number : Integer;
Next : ElementPointer;
end;
var
FirstElement : ElementPointer;
CurrentElement : ElementPointer;
Number : Integer;
(* Main Program *)
begin
write(chr(125));
(* Initialize the list and its pointers. *)
new(FirstElement);
FirstElement^.Next := Nil;
CurrentElement := FirstElement;
(* Fill the linked list *)
repeat
write('Enter number or 0 to exit:');
read(Number);
writeln;
if Number > 0 then
begin
(* Add each number to the list, then add an element. *)
CurrentElement^.Number := Number;
new(CurrentElement^.Next);
CurrentElement := CurrentElement^.Next;
CurrentElement^.Next := Nil;
end;
until Number <= 0;
(* Write the linked list back out *)
if CurrentElement<>FirstElement then
begin
CurrentElement:=FirstElement;
while CurrentElement^.Next <> Nil do
begin
writeln(CurrentElement^.Number);
CurrentElement:= CurrentElement^.Next
end (* while loop *)
end; (* if *)
end. (* LinkEcho *)
No comments:
Post a Comment