Advertisement
Guest User

Untitled

a guest
Jul 13th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. package queue;
  2. import java.util.*;
  3.  
  4. public class FifoQueue<E> extends AbstractQueue<E> implements Queue<E> {
  5. private QueueNode<E> last;
  6. private int size=0;
  7. private FifoQueue<E> queue;
  8.  
  9. public FifoQueue() {
  10. queue = new FifoQueue<E>();
  11. }
  12.  
  13. /**
  14. * Returns an iterator over the elements in this queue
  15. * @return an iterator over the elements in this queue
  16. */
  17. public Iterator<E> iterator() {
  18. return queue.iterator();
  19. }
  20.  
  21. /**
  22. * Returns the number of elements in this queue
  23. * @return the number of elements in this queue
  24. */
  25. public int size() {
  26. return size;
  27. }
  28.  
  29. /**
  30. * Inserts the specified element into this queue, if possible
  31. * post: The specified element is added to the rear of this queue
  32. * @param x the element to insert
  33. * @return true if it was possible to add the element
  34. * to this queue, else false
  35. */
  36. public boolean offer(E x) {
  37. if(queue.add(x)){
  38. size++;
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44.  
  45. /**
  46. * Retrieves and removes the head of this queue,
  47. * or null if this queue is empty.
  48. * post: the head of the queue is removed if it was not empty
  49. * @return the head of this queue, or null if the queue is empty
  50. */
  51. public E poll() {
  52. if(queue.size() > 0) {
  53. size=size-1;
  54. }
  55. return queue.poll();
  56. }
  57.  
  58. /**
  59. * Retrieves, but does not remove, the head of this queue,
  60. * returning null if this queue is empty
  61. * @return the head element of this queue, or null
  62. * if this queue is empty
  63. */
  64. public E peek() {
  65. return queue.peek();
  66. }
  67.  
  68.  
  69. private static class QueueNode<E> {
  70. E element;
  71. QueueNode<E> next;
  72.  
  73. private QueueNode(E x) {
  74. element = x;
  75. next = null;
  76. }
  77.  
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement