program amoeba(input,output); const AMOEBANAMESIZE = 10; { maximum number of characters in amoeba name } type AmoebaName = packed array [1..AMOEBANAMESIZE] of char; AmoebaPtr = ^ Amoeba; Amoeba = record name : AmoebaName; { this one's name } parent : AmoebaPtr; { good old mom (or is it dad?) } olderSibling : AmoebaPtr; { the next older brother/sister } youngestChild : AmoebaPtr; { the youngest kid } oldestChild : AmoebaPtr; { the oldest kid } end; var me : AmoebaPtr; { me } grandparent : AmoebaPtr; { my grandparent } parent : AmoebaPtr; { my parent } brother, sister : AmoebaPtr; { my brother and sister } child1, child2, child3 : AmoebaPtr; { my children } grandchild11, grandchild12 : AmoebaPtr; { grandchildren } grandchild31, grandchild32 : AmoebaPtr; { NewAmoeba: create a new amoeba with given name } function NewAmoeba(name : AmoebaName) : AmoebaPtr; var newOne : AmoebaPtr; begin { create the new amoeba } new(newOne); { set name as specified and pointers to nil } newOne^.name := name; newOne^.parent := nil; newOne^.olderSibling := nil; newOne^.youngestChild := nil; newOne^.oldestChild := nil; NewAmoeba := newOne; end; { PrintName: print the name of an amoeba } procedure PrintName(which : AmoebaPtr); begin { watch out for nil amoeba } if which = nil then begin writeln(output,'PrintName: Sorry. That one has no name.'); end else begin writeln(output,which^.name); end; end; { AddChild: add a child to a parent. Make it the youngest child } { connect all pointers as appropriate } procedure AddChild(parent, newChild : AmoebaPtr); var otherSibling : AmoebaPtr; begin { make child point to parent } newChild^.parent := parent; { check for parent having other children } otherSibling := parent^.youngestChild; if otherSibling = nil then begin { only child. Make parents youngest and oldest point to this one } parent^.youngestChild := newChild; parent^.oldestChild := newChild; { there is no older sibling } newChild^.olderSibling := nil; end else begin { other children. Make this one youngest } parent^.youngestChild := newChild; { no younger siblings, but this one points to others as older siblings } newChild^.olderSibling := otherSibling; end; end; begin { create everybody } me := NewAmoeba('me'); parent := NewAmoeba('mom/dad'); grandparent := NewAmoeba('Amos McCoy'); brother := NewAmoeba('Fred'); sister := NewAmoeba('Wilma'); child1 := NewAmoeba('Larry'); child2 := NewAmoeba('Curly'); child3 := NewAmoeba('Moe'); grandchild11 := NewAmoeba('Laurel'); grandchild12 := NewAmoeba('Hardy'); grandchild31 := NewAmoeba('Rowan'); grandchild32 := NewAmoeba('Martin'); { This will seem a little backwards, because we have an "add child" } { instead of an "add parent". We have to put the grandchildren on } { the children and then add the children to me, then add me to my parent. } { first do Larry's kids } AddChild(child1,grandchild11); AddChild(child1,grandchild12); { next do Moe's kids } AddChild(child3,grandchild31); AddChild(child3,grandchild32); { now add Larry, Curly, and Moe to me } AddChild(me,child1); AddChild(me,child2); AddChild(me,child3); { now add me to my parent } AddChild(parent,me); { now add my brother and sister } AddChild(parent,brother); AddChild(parent,sister); { we don't need these any more, do we? } parent := nil; grandparent := nil; brother := nil; sister := nil; child1 := nil; child2 := nil; child3 := nil; grandchild11 := nil; grandchild12 := nil; grandchild31 := nil; grandchild32 := nil; { print out my and my parent's name } write(output,'Hi. My name is '); PrintName(me); write(output,'Meet my parent '); PrintName(me^.parent); end.