Advertisement
jules0707

Deque.java

Feb 19th, 2018 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package sources;
  2.  
  3. import java.util.Iterator;
  4.  
  5. /* http://coursera.cs.princeton.edu/algs4/assignments/queues.html
  6.  * choice to use Linked list as requirement is "constant worst case time" for non iterator operations
  7.  * and arrays are "linear at worst case" for push and pop (resizing arrays via doubling and one quarter full shrinking)
  8.  */
  9.  
  10. public class Deque<Item> implements Iterable<Item> {
  11.  
  12.     private Node first, last;
  13.     private int n;
  14.  
  15.     private class Node {
  16.         private Item item;
  17.         private Node next; // Useful to remove and add first
  18.         private Node prev; // Useful to remove and add last
  19.     }
  20.  
  21.     // construct an empty deque
  22.     public Deque() {
  23.         n = 0;
  24.         first = null;
  25.         last = null;
  26.     }
  27.  
  28.     public boolean isEmpty() {
  29.         return first == null;
  30.     }
  31.  
  32.     public int size() {
  33.         return n;
  34.     }
  35.  
  36.     // add the item to the front
  37.     public void addFirst(Item item) {
  38.         if (item == null)
  39.             throw new java.lang.IllegalArgumentException("item is null");
  40.  
  41.         Node oldFirst = first; // can be null!
  42.         first = new Node(); // construct the new node that will be added
  43.         first.item = item;
  44.         first.prev = null; // always null as it is the first
  45.  
  46.         if (n == 0) {
  47.             first.next = last; // last is null here
  48.         } else if (n == 1) {
  49.             last = oldFirst;
  50.             first.next = last;
  51.             last.prev = first;
  52.         } else {
  53.             first.next = oldFirst;
  54.             oldFirst.prev = first;
  55.         }
  56.         n++;
  57.     }
  58.  
  59.     // add the item to the end
  60.     public void addLast(Item item) {
  61.         if (item == null)
  62.             throw new java.lang.IllegalArgumentException("item is null");
  63.         if (n == 0) {
  64.             addFirst(item);
  65.         } else {
  66.             Node oldLast = last; // can be null!
  67.             last = new Node();
  68.             last.item = item;
  69.             last.next = null; // by principle
  70.  
  71.             if (n == 1) {
  72.                 last.prev = first; // the only possibility
  73.                 first.next = last;
  74.             } else {
  75.                 last.prev = oldLast;
  76.                 oldLast.next = last;
  77.             }
  78.             n++;
  79.         }
  80.     }
  81.  
  82.     // remove and return the item from the front
  83.     public Item removeFirst() {
  84.         if (isEmpty())
  85.             throw new java.util.NoSuchElementException("deque is empty");
  86.         Node oldFirst = first;
  87.         Item item = first.item;
  88.  
  89.         if (n == 1) {
  90.             first = null; // then the deck gets emptied
  91.             last = null;
  92.         } else {
  93.             first = oldFirst.next;
  94.             if (n == 2) { // last becomes first
  95.                 last = null;
  96.                 first.next = null;
  97.                 first.prev = null;
  98.             }
  99.             first.prev = null;
  100.             first.next = oldFirst.next.next;
  101.         }
  102.  
  103.         n--;
  104.         return item;
  105.     }
  106.  
  107.     // remove and return the item from the end
  108.     public Item removeLast() {
  109.         if (isEmpty())
  110.             throw new java.util.NoSuchElementException("deque is empty");
  111.         Item item;
  112.         Node oldLast = last; // can be null !
  113.  
  114.         if (n == 1) {
  115.             item = first.item;
  116.             first = null; // we empty the deck
  117.             last = null; // to be sure
  118.         } else {
  119.             item = oldLast.item;
  120.             if (n == 2) { // only two items in the deck last becomes null
  121.                 last = null;
  122.                 first.next = null;
  123.             } else { // otherwise
  124.                 last = oldLast.prev;
  125.                 last.next = null;
  126.             }
  127.         }
  128.         n--;
  129.         return item;
  130.     }
  131.  
  132.     // return an iterator over items in order from front to end
  133.     public Iterator<Item> iterator() {
  134.         return new DequeIterator();
  135.     }
  136.  
  137.     private class DequeIterator implements Iterator<Item> {
  138.         private Node current = first;
  139.  
  140.         public boolean hasNext() {
  141.             return current != null;
  142.         }
  143.  
  144.         public void remove() {
  145.             throw new java.lang.UnsupportedOperationException("not supported");
  146.         }
  147.  
  148.         public Item next() {
  149.             if (!hasNext())
  150.                 throw new java.util.NoSuchElementException("No more items");
  151.             Item item = current.item;
  152.             current = current.next;
  153.             return item;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement