Guest User

Untitled

a guest
Jul 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. package Labs.Lab6;
  2.  
  3. //--------------------------------------------------------------------
  4. //
  5. // Laboratory 6 AQueue.jshl
  6. //
  7. // Class definition for the array implementation of the Queue ADT
  8. //
  9. // The student is to complete all missing or incomplete method
  10. // implementations for this class
  11. //
  12. //--------------------------------------------------------------------
  13.  
  14. class AQueue implements Queue // Array-based queue class
  15. {
  16. // Data members
  17. private int maxSize; // Maximum number of elements in the queue
  18. private int front; // Index of the front element
  19. private int rear; // Index of the rear element
  20. private Object [] element; // Array containing the queue elements
  21.  
  22. // Constructors
  23. public AQueue ( ) // Constructor: default size
  24. {
  25. setup(defMaxQueueSize);
  26. }
  27.  
  28. public AQueue ( int size ) // Constructor: sets size
  29. {
  30. setup(size);
  31. }
  32.  
  33. // Class methods
  34. private void setup(int size) // Called by Constructors only
  35. {
  36. if (size > 0){
  37. element = new Object[size];
  38. rear = size - 1;
  39. front = -1;
  40. maxSize = size;
  41. }
  42. }
  43.  
  44. // Queue manipulation operations
  45.  
  46. public void showStructure ( )
  47. // Array implementation. Outputs the elements in a queue. If the
  48. // queue is empty, outputs "Empty queue". This operation is intended
  49. // for testing and debugging purposes only.
  50. {
  51. int j; // Loop counter
  52.  
  53. if ( front == -1 )
  54. System.out.println("Empty queue");
  55. else
  56. {
  57. System.out.println("front = " + front + " rear = " + rear);
  58. for ( j = 0 ; j < maxSize ; j++ )
  59. System.out.print(j + "\t");
  60. System.out.println( );
  61. if ( rear >= front )
  62. for ( j = 0 ; j < maxSize ; j++ )
  63. if ( ( j >= front ) && ( j <= rear ) )
  64. System.out.print(element[j] + "\t");
  65. else
  66. System.out.print(" \t");
  67. else
  68. for ( j = 0 ; j < maxSize ; j++ )
  69. if ( ( j >= front ) || ( j <= rear ) )
  70. System.out.print(element[j] + "\t");
  71. else
  72. System.out.print(" \t");
  73. System.out.println( );
  74. }
  75. } // showStructure for AQueue
  76.  
  77.  
  78. public void clear() {
  79. front = -1; //resets the queue by setting beginning and end to -1
  80. rear = -1;
  81. }
  82.  
  83. public Object dequeue() {
  84. if (isEmpty()){
  85. System.out.println("Empty queue, cannot dequeue from.");
  86. return null;
  87. } else if (front == rear){ //dequeueing last element in queue only
  88. Object toReturn = element[front];
  89. clear();
  90. return toReturn;
  91. }
  92. else {
  93. Object toReturn = element[front];
  94. element[front] = null;
  95. front = (front + 1) % element.length;
  96. return toReturn;
  97. }
  98. }
  99.  
  100. public void enqueue(Object newElement) {
  101. //check for if queue is not full
  102. if (isFull()){
  103. System.out.println("Full queue, cannot add more.");
  104. }
  105. else
  106. {
  107. rear = (rear + 1) % element.length;
  108. element[rear] = newElement;
  109. if (front == -1){ //Increment first time enqueueing an element
  110. front = 0;
  111. }
  112. }
  113. }
  114.  
  115. public boolean isEmpty() {
  116. if (front == -1){ //checks that both the front and rear are the same
  117. //which means therefore there is nothing in the array
  118. return true;
  119. }
  120. return false;
  121. }
  122.  
  123. public boolean isFull() {
  124. if (((rear+1)%element.length) == front)
  125. {
  126. return true;
  127. }
  128. return false;
  129. }
  130.  
  131. // In-lab operations
  132. // These methods are NOT included in the interface Queue.
  133. // Since compiler errors will occur before some of these methods
  134. // are implemented, they have been temporarily commented out.
  135. // Remove the comment delimeters '/*' and '*/' once these methods
  136. // are implemented.
  137.  
  138. public void putFront ( Object newElement ) // Insert at front
  139. {
  140. if (!isFull() && newElement != null){
  141. for (int i = 0; i < maxSize-1; i++) {
  142. element[i] = element[i + 1];
  143. }
  144. element[front] = newElement;
  145. }
  146. }
  147.  
  148. public Object getRear ( ) // Get from rear
  149. {
  150. if(!isEmpty()){
  151. Object toReturn = element[rear];
  152. element[rear] = null;
  153. rear = (rear - 1) % element.length;
  154. return toReturn;
  155. } else {
  156. String fail = new String("Array empty.");
  157. return fail;
  158. }
  159. }
  160. }
  161.  
  162. /* public int length ( ) // Number of elements
  163. { }
  164. */ // class AQueue
Add Comment
Please, Sign In to add comment