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;
- public FifoQueue() {
- last = null;
- }
- /**
- * Returns an iterator over the elements in this queue
- * @return an iterator over the elements in this queue
- */
- @Override
- public Iterator<E> iterator() {
- return new QueueIterator();
- }
- private class QueueIterator implements Iterator<E> {
- private QueueNode<E> pos;
- private int index = 0;
- /* Konstruktor */
- private QueueIterator() {
- if (last == null)
- pos = null;
- else
- pos = last.next;
- }
- public boolean hasNext() {
- if (size > 0)
- return index < size;
- return false;
- }
- public E next() {
- if (pos != null){
- index++;
- QueueNode<E> newNode = pos;
- pos = newNode.next;
- return newNode.element;
- }
- else
- throw new NoSuchElementException();
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- }
- /**
- * 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) {
- QueueNode<E> newNode = new QueueNode<E>(x);
- if (!isEmpty()){
- newNode.next = last.next;
- last.next = newNode;
- last = newNode;
- }
- else {
- last = newNode;
- last.next = last;
- }
- size++;
- return true;
- }
- /**
- * 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 (!isEmpty()){
- QueueNode<E> head = last.next;
- last.next = head.next;
- size--;
- return head.element;
- }
- return null;
- }
- /**
- * 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() {
- if(!isEmpty())
- return last.next.element;
- return null;
- }
- private static class QueueNode<E> {
- E element;
- QueueNode<E> next;
- private QueueNode(E x) {
- element = x;
- next = null;
- }
- }
- /**
- * Appends the specified queue to this queue
- * post: all elements from the specified queue are appended
- * to this queue. The specified queue (q) is empty
- * @param q the queue to append
- */
- public void append(FifoQueue<E> q){
- if (q.size == 0){
- }
- else if (size == 0){
- size = q.size;
- last = q.last;
- last.next = q.last.next;
- q.last = null;
- q.size = 0;
- }
- else{
- QueueNode<E> temp = last.next;
- last.next = q.last.next;
- q.last.next = temp;
- last = q.last;
- size += q.size;
- q.last = null;
- q.size = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement