Lusien_Lashans

Ochered

Dec 25th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. class QueueY<Item> implements Iterable<Item> {
  2.     private QueObj<Item> head;
  3.     private QueObj<Item> tail;
  4.     private int size;
  5.  
  6.     private static class QueObj<Item> {
  7.         Item value;
  8.         QueObj next;
  9.  
  10.         QueObj(Item value, QueObj next) {
  11.             this.value = value;
  12.             this.next = next;
  13.         }
  14.     }
  15.  
  16.     private class QueueIterator implements Iterator<Item> {
  17.         QueObj current = head;
  18.  
  19.         @Override
  20.         public boolean hasNext() {
  21.             return (current != null);
  22.         }
  23.  
  24.         @Override
  25.         public Item next() {
  26.             if (hasNext()) {
  27.                 QueObj<Item> item = current;
  28.                 current = item.next;
  29.                 return item.value;
  30.             }
  31.             return null;
  32.         }
  33.     }
  34.  
  35.     @Override
  36.     public Iterator<Item> iterator() {
  37.         return new QueueIterator();
  38.     }
  39.  
  40.     public boolean isEmpty() {
  41.         return (head == null);
  42.     }
  43.  
  44.     public int size() {
  45.         return size;
  46.     }
  47.  
  48.     public void Enqueue(Item value)
  49.     {
  50.         if (isEmpty()) {
  51.             tail = head = new QueObj<Item>(value, null);
  52.             size++;
  53.         }
  54.         else
  55.         {
  56.             QueObj item = new QueObj (value, null );
  57.             tail.next = item;
  58.             tail = item;
  59.             size++;
  60.         }
  61.     }
  62.  
  63.     public Item Dequeue()
  64.     {
  65.         Item result = head.value;
  66.         head = head.next;
  67.         if (head == null)
  68.             tail = null;
  69.         size--;
  70.         return result;
  71.     }
  72. }
Add Comment
Please, Sign In to add comment