home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1986 / 02 / walker.feb < prev   
Text File  |  1986-02-27  |  5KB  |  246 lines

  1. Listing One
  2.  
  3.  
  4. MODULE Sorter;
  5.  
  6. (* Sorter utilizes modules RandomNumbers and Queues to demonstrate *)
  7. (* the utility of data abstraction.                                *)
  8.  
  9. FROM InOut IMPORT
  10.                 ClearScreen,
  11.                 WriteInt,
  12.                 WriteLn,
  13.                 WriteString;
  14.         
  15. FROM RandomNumbers IMPORT
  16.         (* PROC *)     randu;
  17.         
  18. FROM Queues IMPORT
  19.         (* PROC *)     InitPriorityQueue,
  20.                        QueueEmpty,
  21.                 Add,
  22.                 Fetch,
  23.         (* TYPE *)     PriorityQueue;
  24.         
  25. VAR Count : CARDINAL;
  26.     x     : INTEGER;
  27.     Q     : PriorityQueue;
  28.         
  29. BEGIN (* MAIN *)
  30.     
  31.     ClearScreen;
  32.     
  33.     InitPriorityQueue(Q); (* Initialize the Priority Queue Q *)
  34.     
  35.     (* Add 100 pseudo-random numbers to the Priority Queue Q *)
  36.     FOR Count := 1 TO 100 DO
  37.         Add(Q, randu());
  38.     END;
  39.     
  40.     (* Empty the Priority Queue Q and display each removed element *)
  41.     WriteLn;
  42.     WriteLn;
  43.     WriteString("Sorted Pseudo-Random Numbers.....");
  44.     WriteLn;
  45.     WHILE NOT QueueEmpty(Q) DO
  46.         x := Fetch(Q);
  47.         WriteInt(x, 10);
  48.         WriteLn
  49.     END;
  50.  
  51. END Sorter.
  52.  
  53.  
  54.  
  55. Listing Two
  56.  
  57. The DEFINITION MODULEs Queues and RandomNumbers
  58.  
  59. DEFINITION MODULE Queues;
  60.  
  61. EXPORT QUALIFIED
  62.         (* TYPE *)    PriorityQueue,
  63.         (* PROC *)     InitPriorityQueue,
  64.                 Add,
  65.                 Fetch,
  66.                 QueueEmpty;
  67.  
  68. TYPE PriorityQueue;  (* Opaque Type *)
  69.  
  70. PROCEDURE InitPriorityQueue(VAR Q : PriorityQueue);
  71. (* Initializes the Priority Queue Q *)
  72.  
  73. PROCEDURE Add(VAR Q : PriorityQueue; datum : INTEGER);
  74. (* Adds the data item to the Priority Queue Q *)
  75.  
  76. PROCEDURE Fetch(VAR Q : PriorityQueue ) : INTEGER;
  77. (* Fetches the smallest element from the Priority Queue Q *)
  78.  
  79. PROCEDURE QueueEmpty( Q : PriorityQueue) : BOOLEAN;
  80. (* QueueEmpty RETURNs TRUE if PriorityQueue Q is empty; FALSE otherwise *)
  81.  
  82. END Queues.
  83.  
  84. DEFINITION MODULE RandomNumbers;
  85.  
  86. EXPORT QUALIFIED
  87.     (* PROC *) randu;
  88.  
  89. PROCEDURE randu() : INTEGER;
  90. (* randu RETURNs a pseudo-random INTEGER *)
  91.  
  92. END RandomNumbers. 
  93.  
  94.  
  95. Listing Three
  96.  
  97. IMPLEMENTATION MODULE of Queues incorporating an Add procedure
  98. that uses a recursively defined algorithm; IMPLEMENTATION 
  99. MODULE of RandomNumbers that uses poorly chosen coefficients
  100. for the recurrence relation.
  101.  
  102.  
  103. IMPLEMENTATION MODULE Queues;
  104.  
  105. FROM Storage IMPORT ALLOCATE, DEALLOCATE;
  106.  
  107. TYPE PriorityQueue = POINTER TO PriNode;
  108.      PriNode       = RECORD
  109.              data : INTEGER;
  110.             link : PriorityQueue;
  111.          END;
  112.  
  113. PROCEDURE InitPriorityQueue(VAR Q : PriorityQueue);
  114. BEGIN
  115.     Q := NIL
  116. END InitPriorityQueue;
  117.  
  118. PROCEDURE Add(VAR Q : PriorityQueue; datum : INTEGER);
  119. VAR T : PriorityQueue;
  120. BEGIN
  121.     IF QueueEmpty(Q) THEN
  122.         NEW(Q);
  123.         Q^.link := NIL;
  124.         Q^.data := datum;
  125.     ELSIF datum < Q^.data THEN
  126.         NEW(T);
  127.         T^.link := Q;
  128.         T^.data := datum;
  129.         Q := T
  130.     ELSE
  131.         Add(Q^.link, datum)
  132.     END;
  133. END Add;
  134.  
  135. PROCEDURE Fetch(VAR Q : PriorityQueue) : INTEGER;
  136. VAR tempInt : INTEGER;
  137.     tempQ   : PriorityQueue;
  138. BEGIN
  139.     tempQ := Q;
  140.     tempInt := Q^.data;
  141.     Q := Q^.link;
  142.     DISPOSE(tempQ);
  143.     RETURN tempInt
  144. END Fetch;
  145.  
  146. PROCEDURE QueueEmpty(Q : PriorityQueue) : BOOLEAN;
  147. BEGIN
  148.     RETURN Q = NIL
  149. END QueueEmpty;
  150.  
  151. END Queues.
  152.  
  153.  
  154.  
  155.  
  156. Listing Four
  157.  
  158. IMPLEMENTATION MODULE of Queues incorporating an Add procedure
  159. that uses a non-recursive algorithm; IMPLEMENTATION MODULE of 
  160. RandomNumbers with better coefficients for the recurrence
  161. relation. These improved versions of the implementation modules
  162. can be compiled and substituted for the original Queues and 
  163. RandomNumbers without the definition module or any client 
  164. modules having to be recompiled.
  165.  
  166. IMPLEMENTATION MODULE Queues;
  167.  
  168. FROM Storage IMPORT ALLOCATE, DEALLOCATE;
  169.  
  170. TYPE PriorityQueue = POINTER TO PriNode;
  171.      PriNode       = RECORD
  172.              data : INTEGER;
  173.             link : PriorityQueue;
  174.          END;
  175.  
  176. PROCEDURE InitPriorityQueue(VAR Q : PriorityQueue);
  177. BEGIN
  178.     Q := NIL
  179. END InitPriorityQueue;
  180.  
  181. PROCEDURE Add(VAR Q : PriorityQueue; datum : INTEGER);
  182. VAR T, T1, NewNode : PriorityQueue;
  183. BEGIN
  184.     IF QueueEmpty(Q) THEN
  185.         NEW(Q);
  186.         Q^.link := NIL;
  187.         Q^.data := datum;
  188.     ELSIF datum < Q^.data THEN
  189.         NEW(T);
  190.         T^.link := Q;
  191.         T^.data := datum;
  192.         Q := T
  193.     ELSE
  194.         T := Q;
  195.         WHILE (T # NIL) & (datum >= T^.data) DO
  196.             T1 := T;
  197.             T := T^.link;
  198.         END;
  199.         NEW(NewNode);
  200.         NewNode^.link := T;
  201.         NewNode^.data := datum;
  202.         T1^.link := NewNode;
  203.     END;
  204.     
  205. END Add;
  206.  
  207. PROCEDURE Fetch(VAR Q : PriorityQueue) : INTEGER;
  208. VAR tempInt : INTEGER;
  209.     tempQ   : PriorityQueue;
  210. BEGIN
  211.     tempQ := Q;
  212.     tempInt := Q^.data;
  213.     Q := Q^.link;
  214.     DISPOSE(tempQ);
  215.     RETURN tempInt
  216. END Fetch;
  217.  
  218. PROCEDURE QueueEmpty(Q : PriorityQueue) : BOOLEAN;
  219. BEGIN
  220.     RETURN Q = NIL
  221. END QueueEmpty;
  222.  
  223. END Queues.
  224.  
  225.  
  226. IMPLEMENTATION MODULE RandomNumbers;
  227.  
  228. VAR x : INTEGER;
  229.  
  230. PROCEDURE randu() : INTEGER;
  231. BEGIN
  232.     x := (5 * x + 31) MOD 4096;
  233.     RETURN x
  234. END randu;
  235.  
  236. BEGIN
  237.     x := 7;
  238.  
  239. END RandomNumbers.;
  240.     RETURN x
  241. END randu;
  242.  
  243. BEGIN
  244.     x := 7;
  245.  
  246. END RandomNumbers.