Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.util.Iterator;
  2.  
  3. // ARRAY QUEUE. A fixed length queue implemented as a circular array.
  4. class ArrayQueue<Base>
  5. {
  6. private class ArrayQueueIterator implements Iterator<Base>
  7. {
  8. // constructor
  9. private ArrayQueueIterator(...)
  10. {
  11.  
  12. }
  13. // return true if ArrayQueue has more elements to visit
  14. // use ArrayQueueIterator's private vars only, use ideas from ArrayQueue's isEmpty.
  15. public boolean hasNext()
  16. {
  17.  
  18. }
  19. // return next Base from ArrayQueue. If hasNext is false, throw IllegalStateException.
  20. // Use private vars only. Use ideas from dequeue.
  21. public Base next()
  22. {
  23.  
  24. }
  25. public void remove()
  26. { }
  27. }
  28.  
  29. private int front;
  30. private int rear;
  31. private Base [] bases;
  32.  
  33. public ArrayQueue(int size)
  34. {
  35. if (size >= 1)
  36. {
  37. front = 0;
  38. rear = 0;
  39. bases = (Base []) new Object[size];
  40. }
  41. else
  42. {
  43. throw new IllegalArgumentException("Size must be at least 1.");
  44. }
  45. }
  46.  
  47. public Base dequeue()
  48. {
  49. if (isEmpty())
  50. {
  51. throw new IllegalStateException("Queue is empty.");
  52. }
  53. else
  54. {
  55. front = (front + 1) % bases.length;
  56. Base temp = bases[front];
  57. bases[front] = null;
  58. return temp;
  59. }
  60. }
  61.  
  62. public void enqueue(Base base)
  63. {
  64. int nextRear = (rear + 1) % bases.length;
  65. if (front == nextRear)
  66. {
  67. throw new IllegalStateException("Queue is full.");
  68. }
  69. else
  70. {
  71. rear = nextRear;
  72. bases[rear] = base;
  73. }
  74. }
  75.  
  76. public boolean isEmpty()
  77. {
  78. return front == rear;
  79. }
  80.  
  81. public boolean isFull()
  82. {
  83. return front == (rear + 1) % bases.length;
  84. }
  85.  
  86. // Make a new instance of ArrayQueueIterator and return it.
  87. public Iterator<Base> iterator()
  88. {
  89.  
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement