home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / modula2 / tutorial / programs / linklist.mod < prev    next >
Text File  |  1993-03-14  |  2KB  |  66 lines

  1.                                         (* Chapter 12 - Program 3 *)
  2. MODULE LinkList;
  3.  
  4. FROM InOut   IMPORT WriteString, Write, WriteLn;
  5. FROM Storage IMPORT ALLOCATE, DEALLOCATE;
  6. FROM SYSTEM  IMPORT TSIZE;
  7.  
  8. TYPE  NextPointer = POINTER TO FullName;
  9.       FullName = RECORD
  10.         FirstName : ARRAY[0..12] OF CHAR;
  11.         Initial   : CHAR;
  12.         LastName  : ARRAY[0..15] OF CHAR;
  13.         Next      : NextPointer;
  14.       END;
  15.  
  16. VAR   StartOfList : NextPointer;
  17.       PlaceInList : NextPointer;
  18.       TempPlace   : NextPointer;
  19.       Index       : CARDINAL;
  20.  
  21. BEGIN   (* Main Program *)
  22.  
  23.                   (* Generate the first name in the list *)
  24.  
  25.    ALLOCATE(PlaceInList,TSIZE(FullName));
  26.    StartOfList := PlaceInList;
  27.    PlaceInList^.FirstName := "John ";
  28.    PlaceInList^.Initial := 'Q';
  29.    PlaceInList^.LastName := " Doe";
  30.    PlaceInList^.Next := NIL;
  31.  
  32.                   (* Generate another name in the list *)
  33.  
  34.    TempPlace := PlaceInList;
  35.    ALLOCATE(PlaceInList,TSIZE(FullName));
  36.    TempPlace^.Next := PlaceInList;
  37.    PlaceInList^.FirstName := "Mary ";
  38.    PlaceInList^.Initial := 'R';
  39.    PlaceInList^.LastName := " Johnson";
  40.    PlaceInList^.Next := NIL;
  41.  
  42.                  (* Add 10 more names to complete the list *)
  43.  
  44.    FOR Index := 1 TO 10 DO
  45.       TempPlace := PlaceInList;
  46.       ALLOCATE(PlaceInList,TSIZE(FullName));
  47.       TempPlace^.Next := PlaceInList;
  48.       PlaceInList^.FirstName := "Billy ";
  49.       PlaceInList^.Initial := 'R';
  50.       PlaceInList^.LastName := " Franklin";
  51.       PlaceInList^.Next := NIL;
  52.    END;
  53.  
  54.                    (* Display the list on the video monitor *)
  55.  
  56.    PlaceInList := StartOfList;
  57.    REPEAT
  58.       WriteString(PlaceInList^.FirstName);
  59.       Write(PlaceInList^.Initial);
  60.       WriteString(PlaceInList^.LastName);
  61.       WriteLn;
  62.       TempPlace := PlaceInList;
  63.       PlaceInList := PlaceInList^.Next;
  64.    UNTIL TempPlace^.Next = NIL;
  65. END LinkList.
  66.