Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package queue;
- import java.util.*;
- public class FifoQueue<E> extends AbstractQueue<E> implements Queue<E> {
- private QueueNode<E> last;
- private int size=0;
- private FifoQueue<E> queue;
- public FifoQueue() {
- queue = new FifoQueue<E>();
- }
- /**
- * Returns an iterator over the elements in this queue
- * @return an iterator over the elements in this queue
- */
- public Iterator<E> iterator() {
- return queue.iterator();
- }
- /**
- * Returns the number of elements in this queue
- * @return the number of elements in this queue
- */
- public int size() {
- return size;
- }
- /**
- * Inserts the specified element into this queue, if possible
- * post: The specified element is added to the rear of this queue
- * @param x the element to insert
- * @return true if it was possible to add the element
- * to this queue, else false
- */
- public boolean offer(E x) {
- if(queue.add(x)){
- size++;
- return true;
- } else {
- return false;
- }
- }
- /**
- * Retrieves and removes the head of this queue,
- * or null if this queue is empty.
- * post: the head of the queue is removed if it was not empty
- * @return the head of this queue, or null if the queue is empty
- */
- public E poll() {
- if(queue.size() > 0) {
- size=size-1;
- }
- return queue.poll();
- }
- /**
- * Retrieves, but does not remove, the head of this queue,
- * returning null if this queue is empty
- * @return the head element of this queue, or null
- * if this queue is empty
- */
- public E peek() {
- return queue.peek();
- }
- private static class QueueNode<E> {
- E element;
- QueueNode<E> next;
- private QueueNode(E x) {
- element = x;
- next = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement