Advertisement
Guest User

FifoQueue

a guest
Oct 9th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 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;
  7.  
  8.     public FifoQueue() {
  9.         last = null;
  10.     }
  11.  
  12.     /**
  13.      * Returns an iterator over the elements in this queue
  14.      * @return an iterator over the elements in this queue
  15.      */
  16.     @Override
  17.     public Iterator<E> iterator() {
  18.         return new QueueIterator();
  19.     }
  20.    
  21.     private class QueueIterator implements Iterator<E> {
  22.         private QueueNode<E> pos;
  23.         private int index = 0;
  24.        
  25.         /* Konstruktor */      
  26.         private QueueIterator() {
  27.        
  28.             if (last == null)
  29.                 pos = null;
  30.             else                   
  31.                 pos = last.next;
  32.            
  33.         }
  34.        
  35.         public boolean hasNext() {
  36.            
  37.             if (size > 0)
  38.                 return index < size;
  39.            
  40.             return false;
  41.                
  42.         }
  43.        
  44.         public E next() {
  45.                
  46.             if (pos != null){
  47.                 index++;
  48.                 QueueNode<E> newNode = pos;
  49.                 pos = newNode.next;
  50.                 return newNode.element;
  51.             }
  52.             else
  53.                 throw new NoSuchElementException();
  54.            
  55.         }
  56.        
  57.         public void remove() {
  58.        
  59.             throw new UnsupportedOperationException();
  60.            
  61.         }
  62.        
  63.     }
  64.  
  65.     /**
  66.      * Returns the number of elements in this queue
  67.      * @return the number of elements in this queue
  68.      */
  69.     public int size() {    
  70.         return size;
  71.     }
  72.  
  73.     /**
  74.      * Inserts the specified element into this queue, if possible
  75.      * post:    The specified element is added to the rear of this queue
  76.      * @param   x the element to insert
  77.      * @return  true if it was possible to add the element
  78.      *          to this queue, else false
  79.      */
  80.     public boolean offer(E x) {
  81.         QueueNode<E> newNode =  new QueueNode<E>(x);
  82.        
  83.         if (!isEmpty()){
  84.             newNode.next = last.next;
  85.             last.next = newNode;
  86.             last = newNode;                
  87.         }
  88.         else {
  89.             last = newNode;
  90.             last.next = last;
  91.         }
  92.        
  93.         size++;
  94.         return true;
  95.            
  96.     }
  97.  
  98.     /**
  99.      * Retrieves and removes the head of this queue,
  100.      * or null if this queue is empty.
  101.      * post:    the head of the queue is removed if it was not empty
  102.      * @return  the head of this queue, or null if the queue is empty
  103.      */
  104.     public E poll() {
  105.                
  106.         if (!isEmpty()){
  107.             QueueNode<E> head = last.next;
  108.             last.next = head.next;
  109.             size--;
  110.             return head.element;
  111.         }
  112.        
  113.         return null;
  114.     }
  115.  
  116.     /**
  117.      * Retrieves, but does not remove, the head of this queue,
  118.      * returning null if this queue is empty
  119.      * @return  the head element of this queue, or null
  120.      *          if this queue is empty
  121.      */
  122.     public E peek() {
  123.        
  124.         if(!isEmpty())
  125.             return last.next.element;
  126.        
  127.         return null;
  128.     }
  129.  
  130.  
  131.     private static class QueueNode<E> {
  132.         E element;
  133.         QueueNode<E> next;
  134.  
  135.         private QueueNode(E x) {
  136.             element = x;
  137.             next = null;
  138.         }
  139.  
  140.     }
  141.  
  142.     /**
  143.     * Appends the specified queue to this queue
  144.     * post: all elements from the specified queue are appended
  145.     * to this queue. The specified queue (q) is empty
  146.     * @param q the queue to append
  147.     */
  148.     public void append(FifoQueue<E> q){
  149.        
  150.         if (q.size == 0){
  151.         }
  152.        
  153.         else if (size == 0){
  154.             size = q.size;
  155.             last = q.last;
  156.             last.next = q.last.next;
  157.             q.last = null;
  158.             q.size = 0;
  159.            
  160.         }
  161.            
  162.         else{
  163.         QueueNode<E> temp = last.next;
  164.         last.next = q.last.next;
  165.         q.last.next = temp;
  166.         last = q.last;
  167.         size += q.size;
  168.         q.last = null;
  169.         q.size = 0;
  170.         }  
  171.    
  172.     }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement