home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / Pascal / BFTGPH.ZIP / QUEUE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-08-21  |  4.5 KB  |  121 lines

  1. Unit Q;
  2.  
  3. (***************************************************************************)
  4. (*   Unit Name: Queue                                                      *)
  5. (*   Author: William Hobday                                                *)
  6. (*   Last Modified: March 2, 1989                                          *)
  7. (*                                                                         *)
  8. (*   Description: This unit implements a queue in an array implementation  *)
  9. (*   using the following documented procedures:                            *)
  10. (*                          NewQueue : queue                               *)
  11. (*                          EnQueue( queue,value )                         *)
  12. (*                          DeQueue( queue ) : value                       *)
  13. (*                          Empty( queue ) : boolean                       *)
  14. (***************************************************************************)
  15.  
  16. interface
  17.  
  18. Const Max_Q = 50;
  19.  
  20. type Queue = record
  21.            Data: array[1..max_Q] of char;
  22.            Head,
  23.            Tail,
  24.            Length: integer;
  25.         end;
  26.  
  27. Procedure NewQueue( var Q: Queue );
  28. Procedure EnQueue( var Q: Queue; Name : char );
  29. Function DeQueue( var Q: Queue ) : char;
  30. Function Empty( Q: Queue ) : boolean;
  31.  
  32. implementation
  33.  
  34. (***************************************************************************)
  35. (*   Name: NewQueue                                                        *)
  36. (*                                                                         *)
  37. (*   Purpose: Creates a new empty Queue                                    *)
  38. (*   Input: Q - the Q to be created                                        *)
  39. (*   Output: Q - a new empty Q                                             *)
  40. (***************************************************************************)
  41.  
  42. Procedure NewQueue(var Q: Queue);
  43.  
  44. begin
  45.    With Q do
  46.       begin
  47.          Head := 1;
  48.          Tail := 0;
  49.          Length := 0
  50.       end
  51. end;
  52.  
  53.  
  54.  
  55. (***************************************************************************)
  56. (*   Name: EnQueue                                                         *)
  57. (*                                                                         *)
  58. (*   Purpose: Creates a new node containing the given info and places it   *)
  59. (*            on the end of the Q                                          *)
  60. (*   Input: Q - Q to be appended on                                        *)
  61. (*          Name - data to be added to Q                                   *)
  62. (*   Output: Q - the modified Q                                            *)
  63. (***************************************************************************)
  64.  
  65. Procedure EnQueue(var Q: Queue; Name: char);
  66.  
  67. begin
  68.    with Q do
  69.       if length < Max_Q
  70.          then
  71.             begin
  72.                inc( Length );
  73.                Tail := ( Tail+1 ) mod Max_Q;
  74.                Data[ Tail ] := Name
  75.             end
  76.          else writeln('Error --- Queue Overflow ---')
  77. end;
  78.  
  79.  
  80.  
  81. (***************************************************************************)
  82. (*   Name: DeQueue                                                         *)
  83. (*                                                                         *)
  84. (*   Purpose: Removes node at the head of the queue and returns data       *)
  85. (*   Input: Q - Queue to be modified                                       *)
  86. (*   Output: DeQueue - Name deleted from list                              *)
  87. (***************************************************************************)
  88.  
  89. Function DeQueue(var Q: Queue) : char;
  90.  
  91. begin
  92.    with Q do
  93.       if length > 0
  94.          then begin
  95.                dec( length );
  96.                DeQueue := Data[head];
  97.                head := ( head+1 ) mod Max_Q
  98.             end
  99.          else writeln('Error --- Queue underflow ---')
  100. end;
  101.  
  102.  
  103.  
  104. (***************************************************************************)
  105. (*   Name: Empty                                                           *)
  106. (*                                                                         *)
  107. (*   Purpose: Returns TRUE if Q is empty                                   *)
  108. (*   Input: Q - Queue to be evaluated                                      *)
  109. (*   Output: Empty - Result of Function                                    *)
  110. (***************************************************************************)
  111.  
  112. Function Empty( Q: Queue ) : boolean;
  113.  
  114. begin
  115.    if Q.length > 0
  116.       then Empty := false
  117.       else Empty := true
  118. end;
  119.  
  120. end.
  121.