Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. public class Queue_ArrayBased implements IQueue, IArrayBased {
  2. int size;
  3. Object []array;
  4. public Queue_ArrayBased(int size) {
  5. this.size = size;
  6. array = new Object[size];
  7. }
  8. int f = 0, r = 0;
  9.  
  10. public void enqueue(Object item) {
  11. if (size() == size)
  12. throw new RuntimeException();
  13. array[r] = item;
  14. r = (r + 1) % size;
  15.  
  16. }
  17.  
  18. public Object dequeue() {
  19. if (isEmpty())
  20. throw new RuntimeException();
  21. Object getter = array[f];
  22. array[f] = null;
  23. f = (f + 1) % size;
  24. return getter;
  25. }
  26.  
  27. public boolean isEmpty() {
  28.  
  29. return (f == 0 && r == 0);
  30. }
  31.  
  32. public int size() {
  33.  
  34. return (size - f + r) % size;
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement