Advertisement
Guest User

Generic Deque datastructure

a guest
Jul 8th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. // Deque.java
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4.  
  5. public class Deque<Item> implements Iterable<Item> {
  6.  
  7.   private Node<Item> sentinel = new Node<>(null);
  8.   private int size;
  9.  
  10.   public Deque() {
  11.     sentinel.left  = sentinel;
  12.     sentinel.right = sentinel;
  13.   }
  14.   public Iterator<Item> iterator() {
  15.     return new DequeIterator();
  16.   }
  17.  
  18.   public int size() {
  19.     return size;
  20.   }
  21.  
  22.   public boolean isEmpty() {
  23.     return size() == 0;
  24.   }
  25.  
  26.   private void checkInvariants() {
  27.     assert size >= 0;
  28.     assert (size > 0) == (sentinel.left != sentinel && sentinel.right != sentinel);
  29.     assert sentinel.left != null;
  30.     assert sentinel.right != null;
  31.   }
  32.  
  33.   public void addFirst(Item item) {
  34.     if (item == null) {
  35.       throw new NullPointerException();
  36.     }
  37.     Node<Item> toAdd = new Node<Item>(item);
  38.     Node<Item> previous = sentinel.right;
  39.     sentinel.right = toAdd;
  40.     toAdd.left = sentinel;
  41.     previous.left = toAdd;
  42.     toAdd.right = previous;
  43.     size++;
  44.     checkInvariants();
  45.   }
  46.  
  47.   public void addLast(Item item) {
  48.     if (item == null) {
  49.       throw new NullPointerException();
  50.     }
  51.     Node<Item> toAdd = new Node<Item>(item);
  52.     Node<Item> previous = sentinel.left;
  53.     sentinel.left = toAdd;
  54.     toAdd.right = sentinel;
  55.     previous.right = toAdd;
  56.     toAdd.left = previous;
  57.     size++;
  58.     checkInvariants();
  59.   }
  60.  
  61.   public Item removeLast() {
  62.     if (isEmpty()) {
  63.       throw new java.util.NoSuchElementException();
  64.     }
  65.     size--;
  66.     Node<Item> toGo = sentinel.right;
  67.     sentinel.right = toGo.right;
  68.     sentinel.right.left = sentinel;
  69.     toGo.left = toGo.right = null;
  70.     checkInvariants();
  71.     return toGo.item;
  72.   }
  73.  
  74.   public Item removeFirst() {
  75.     if (isEmpty()) {
  76.       throw new java.util.NoSuchElementException();
  77.     }
  78.     size--;
  79.     Node<Item> toGo = sentinel.left;
  80.     sentinel.left = toGo.left;
  81.     sentinel.left.right = sentinel;
  82.     toGo.left = toGo.right = null;
  83.     checkInvariants();
  84.     return toGo.item;
  85.   }
  86.  
  87.  
  88.   private class Node<Item> {
  89.     public Node<Item> left, right;
  90.     private final Item item;
  91.  
  92.     public Node(Item item) {
  93.       // FIXME: maybe it's a bad practice to throw exception in constructor
  94.       //if (item == null) { throw new NullPointerException(); }
  95.       this.item = item;
  96.     }
  97.   }
  98.  
  99.   private class DequeIterator implements Iterator<Item> {
  100.     private Node<Item> curr;
  101.     public DequeIterator() {
  102.       curr = sentinel.right;
  103.     }
  104.  
  105.     public boolean hasNext() {
  106.       return curr != sentinel;
  107.     }
  108.     public void remove() {
  109.       throw new UnsupportedOperationException();
  110.     }
  111.     public Item next() {
  112.       if (!hasNext()) {
  113.         throw new NoSuchElementException();
  114.       }
  115.       Item item = curr.item;
  116.       curr = curr.right;
  117.       return item;
  118.     }
  119.   }
  120. }
  121.  
  122. //DequeTest.java
  123. import org.junit.Test;
  124. import static org.junit.Assert.*;
  125. import java.util.Iterator;
  126.  
  127. public class DequeTest {
  128.  
  129.   @Test
  130.   public void new_deque__isEmpty__true() {
  131.     Deque<Double> d = new Deque<Double>();
  132.     assertTrue(d.isEmpty());
  133.   }
  134.   @Test
  135.   public void deque_with_element__isEmpty__false() {
  136.     Deque<Double> d = new Deque<Double>();
  137.     d.addFirst(2.0);
  138.     assertFalse(d.isEmpty());
  139.     assertEquals(1, d.size());
  140.   }
  141.  
  142.   @Test
  143.   public void addLast_removeFirst__correct_value() {
  144.     Deque<Double> d = new Deque<Double>();
  145.     Double actual = 4.0;
  146.     d.addLast(actual);
  147.     assertEquals(actual, d.removeFirst());
  148.   }
  149.  
  150.   @Test
  151.   public void addFirst_removeLast__correctValue() {
  152.     Deque<Double> d = new Deque<Double>();
  153.     Double actual = 4.0;
  154.     d.addFirst(actual);
  155.     assertEquals(actual, d.removeLast());
  156.   }
  157.  
  158.   @Test
  159.   public void addFirst_removeFirst__correctValue() {
  160.     Deque<Double> d = new Deque<Double>();
  161.     Double actual = 4.0;
  162.     d.addFirst(actual);
  163.     assertEquals(actual, d.removeFirst());
  164.   }
  165.  
  166.   @Test
  167.   public void addLast_removeLast__correctValue() {
  168.     Deque<Double> d = new Deque<Double>();
  169.     Double actual = 4.0;
  170.     d.addLast(actual);
  171.     assertEquals(actual, d.removeLast());
  172.   }
  173.  
  174.   @Test
  175.   public void iterator_returns() {
  176.     Deque<Integer> d = new Deque<Integer>();
  177.     Integer first = 1;
  178.     Integer second = 2;
  179.     Integer third= 3;
  180.     d.addLast(first);
  181.     d.addLast(second);
  182.     d.addLast(third);
  183.     Iterator<Integer> iter = d.iterator();
  184.     assertEquals(first, iter.next());
  185.     assertEquals(second, iter.next());
  186.     assertEquals(third, iter.next());
  187.   }
  188.  
  189.   @Test
  190.   public void deque_works_as_queue() {
  191.     Deque<Integer> d = new Deque<Integer>();
  192.     Integer first = 1;
  193.     Integer second = 2;
  194.     Integer third= 3;
  195.     d.addFirst(first);
  196.     d.addFirst(second);
  197.     d.addFirst(third);
  198.     assertEquals(first, d.removeFirst());
  199.     assertEquals(second, d.removeFirst());
  200.     assertEquals(third, d.removeFirst());
  201.   }
  202.  
  203.   @Test
  204.   public void deque_works_as_stack() {
  205.     Deque<Integer> d = new Deque<Integer>();
  206.     Integer first = 1;
  207.     Integer second = 2;
  208.     Integer third= 3;
  209.     d.addFirst(first);
  210.     d.addFirst(second);
  211.     d.addFirst(third);
  212.     assertEquals(third, d.removeLast());
  213.     assertEquals(second, d.removeLast());
  214.     assertEquals(first, d.removeLast());
  215.   }
  216.  
  217.  
  218.   @Test(expected=java.util.NoSuchElementException.class)
  219.   public void empty_deque__removeFirst_throws() {
  220.     Deque<Double> q = new Deque<Double>();
  221.     q.removeFirst();
  222.   }
  223.  
  224.   @Test(expected=java.util.NoSuchElementException.class)
  225.   public void empty_deque__removeLast_throws() {
  226.     Deque<Double> d = new Deque<Double>();
  227.     d.removeLast();
  228.   }
  229.  
  230.   @Test(expected=java.util.NoSuchElementException.class)
  231.   public void iterator__next_deque__throws() {
  232.     Deque<Double> d = new Deque<Double>();
  233.     Iterator<Double> i = d.iterator();
  234.     assertFalse(i.hasNext());
  235.     i.next();
  236.    
  237.   }
  238.  
  239.   @Test(expected=NullPointerException.class)
  240.   public void addFirst__null__throws() {
  241.     Deque<Double> d = new Deque<Double>();
  242.     d.addFirst(null);
  243.   }
  244.  
  245.   @Test(expected=NullPointerException.class)
  246.   public void addLast__null__throws() {
  247.     Deque<Double> d = new Deque<Double>();
  248.     d.addLast(null);
  249.   }
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement