home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / m / mod2tutr.zip / EXAMPLES.ZIP / CH12E2.MOD < prev    next >
Text File  |  1989-01-18  |  3KB  |  99 lines

  1.                            (* Chapter 12 - Programming exercise 2 *)
  2.                                         (* Chapter 12 - Program 3 *)
  3. MODULE CH12E2;
  4.  
  5. FROM InOut   IMPORT WriteString, Write, WriteLn;
  6. FROM Storage IMPORT ALLOCATE, DEALLOCATE, Available;
  7. FROM SYSTEM  IMPORT TSIZE;
  8.  
  9. TYPE  NextPointer = POINTER TO FullName;
  10.       FullName = RECORD
  11.         FirstName : ARRAY[0..12] OF CHAR;
  12.         Initial   : CHAR;
  13.         LastName  : ARRAY[0..15] OF CHAR;
  14.         Next      : NextPointer;
  15.       END;
  16.  
  17. VAR   StartOfList : NextPointer;
  18.       PlaceInList : NextPointer;
  19.       TempPlace   : NextPointer;
  20.       Index       : CARDINAL;
  21.       Enough      : BOOLEAN;
  22.  
  23. BEGIN   (* Main Program *)
  24.  
  25.                   (* Generate the first name in the list *)
  26.  
  27.    ALLOCATE(PlaceInList,TSIZE(FullName));
  28.    StartOfList := PlaceInList;
  29.    PlaceInList^.FirstName := "John ";
  30.    PlaceInList^.Initial := 'Q';
  31.    PlaceInList^.LastName := " Doe";
  32.    PlaceInList^.Next := NIL;
  33.  
  34.                   (* Generate another name in the list *)
  35.  
  36.    TempPlace := PlaceInList;
  37.    ALLOCATE(PlaceInList,TSIZE(FullName));
  38.    TempPlace^.Next := PlaceInList;
  39.    PlaceInList^.FirstName := "Mary ";
  40.    PlaceInList^.Initial := 'R';
  41.    PlaceInList^.LastName := " Johnson";
  42.    PlaceInList^.Next := NIL;
  43.  
  44.                  (* Add 10 more names to complete the list *)
  45.  
  46.    FOR Index := 1 TO 10 DO
  47.       TempPlace := PlaceInList;
  48.       Enough := Available(TSIZE(FullName));  (* Ignore result *)
  49.       ALLOCATE(PlaceInList,TSIZE(FullName));
  50.       TempPlace^.Next := PlaceInList;
  51.       PlaceInList^.FirstName := "Billy ";
  52.       PlaceInList^.Initial := 'R';
  53.       PlaceInList^.LastName := " Franklin";
  54.       PlaceInList^.Next := NIL;
  55.    END;
  56.  
  57.                    (* Display the list on the video monitor *)
  58.  
  59.    PlaceInList := StartOfList;
  60.    REPEAT
  61.       WriteString(PlaceInList^.FirstName);
  62.       Write(PlaceInList^.Initial);
  63.       WriteString(PlaceInList^.LastName);
  64.       WriteLn;
  65.       TempPlace := PlaceInList;
  66.       PlaceInList := PlaceInList^.Next;
  67.    UNTIL TempPlace^.Next = NIL;
  68.  
  69.                               (* Deallocate the entire list *)
  70.  
  71.    WHILE StartOfList # NIL DO
  72.       TempPlace := StartOfList^.Next;
  73.       DEALLOCATE(StartOfList,TSIZE(FullName));
  74.       StartOfList := TempPlace;
  75.    END;
  76.  
  77. END CH12E2.
  78.  
  79.  
  80.  
  81.  
  82. (* Result of execution
  83.  
  84. John Q Doe
  85. Mary R Johnson
  86. Billy R Franklin
  87. Billy R Franklin
  88. Billy R Franklin
  89. Billy R Franklin
  90. Billy R Franklin
  91. Billy R Franklin
  92. Billy R Franklin
  93. Billy R Franklin
  94. Billy R Franklin
  95. Billy R Franklin
  96.  
  97. *)
  98.  
  99.