Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Deque.java
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class Deque<Item> implements Iterable<Item> {
- private Node<Item> sentinel = new Node<>(null);
- private int size;
- public Deque() {
- sentinel.left = sentinel;
- sentinel.right = sentinel;
- }
- public Iterator<Item> iterator() {
- return new DequeIterator();
- }
- public int size() {
- return size;
- }
- public boolean isEmpty() {
- return size() == 0;
- }
- private void checkInvariants() {
- assert size >= 0;
- assert (size > 0) == (sentinel.left != sentinel && sentinel.right != sentinel);
- assert sentinel.left != null;
- assert sentinel.right != null;
- }
- public void addFirst(Item item) {
- if (item == null) {
- throw new NullPointerException();
- }
- Node<Item> toAdd = new Node<Item>(item);
- Node<Item> previous = sentinel.right;
- sentinel.right = toAdd;
- toAdd.left = sentinel;
- previous.left = toAdd;
- toAdd.right = previous;
- size++;
- checkInvariants();
- }
- public void addLast(Item item) {
- if (item == null) {
- throw new NullPointerException();
- }
- Node<Item> toAdd = new Node<Item>(item);
- Node<Item> previous = sentinel.left;
- sentinel.left = toAdd;
- toAdd.right = sentinel;
- previous.right = toAdd;
- toAdd.left = previous;
- size++;
- checkInvariants();
- }
- public Item removeLast() {
- if (isEmpty()) {
- throw new java.util.NoSuchElementException();
- }
- size--;
- Node<Item> toGo = sentinel.right;
- sentinel.right = toGo.right;
- sentinel.right.left = sentinel;
- toGo.left = toGo.right = null;
- checkInvariants();
- return toGo.item;
- }
- public Item removeFirst() {
- if (isEmpty()) {
- throw new java.util.NoSuchElementException();
- }
- size--;
- Node<Item> toGo = sentinel.left;
- sentinel.left = toGo.left;
- sentinel.left.right = sentinel;
- toGo.left = toGo.right = null;
- checkInvariants();
- return toGo.item;
- }
- private class Node<Item> {
- public Node<Item> left, right;
- private final Item item;
- public Node(Item item) {
- // FIXME: maybe it's a bad practice to throw exception in constructor
- //if (item == null) { throw new NullPointerException(); }
- this.item = item;
- }
- }
- private class DequeIterator implements Iterator<Item> {
- private Node<Item> curr;
- public DequeIterator() {
- curr = sentinel.right;
- }
- public boolean hasNext() {
- return curr != sentinel;
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- public Item next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- Item item = curr.item;
- curr = curr.right;
- return item;
- }
- }
- }
- //DequeTest.java
- import org.junit.Test;
- import static org.junit.Assert.*;
- import java.util.Iterator;
- public class DequeTest {
- @Test
- public void new_deque__isEmpty__true() {
- Deque<Double> d = new Deque<Double>();
- assertTrue(d.isEmpty());
- }
- @Test
- public void deque_with_element__isEmpty__false() {
- Deque<Double> d = new Deque<Double>();
- d.addFirst(2.0);
- assertFalse(d.isEmpty());
- assertEquals(1, d.size());
- }
- @Test
- public void addLast_removeFirst__correct_value() {
- Deque<Double> d = new Deque<Double>();
- Double actual = 4.0;
- d.addLast(actual);
- assertEquals(actual, d.removeFirst());
- }
- @Test
- public void addFirst_removeLast__correctValue() {
- Deque<Double> d = new Deque<Double>();
- Double actual = 4.0;
- d.addFirst(actual);
- assertEquals(actual, d.removeLast());
- }
- @Test
- public void addFirst_removeFirst__correctValue() {
- Deque<Double> d = new Deque<Double>();
- Double actual = 4.0;
- d.addFirst(actual);
- assertEquals(actual, d.removeFirst());
- }
- @Test
- public void addLast_removeLast__correctValue() {
- Deque<Double> d = new Deque<Double>();
- Double actual = 4.0;
- d.addLast(actual);
- assertEquals(actual, d.removeLast());
- }
- @Test
- public void iterator_returns() {
- Deque<Integer> d = new Deque<Integer>();
- Integer first = 1;
- Integer second = 2;
- Integer third= 3;
- d.addLast(first);
- d.addLast(second);
- d.addLast(third);
- Iterator<Integer> iter = d.iterator();
- assertEquals(first, iter.next());
- assertEquals(second, iter.next());
- assertEquals(third, iter.next());
- }
- @Test
- public void deque_works_as_queue() {
- Deque<Integer> d = new Deque<Integer>();
- Integer first = 1;
- Integer second = 2;
- Integer third= 3;
- d.addFirst(first);
- d.addFirst(second);
- d.addFirst(third);
- assertEquals(first, d.removeFirst());
- assertEquals(second, d.removeFirst());
- assertEquals(third, d.removeFirst());
- }
- @Test
- public void deque_works_as_stack() {
- Deque<Integer> d = new Deque<Integer>();
- Integer first = 1;
- Integer second = 2;
- Integer third= 3;
- d.addFirst(first);
- d.addFirst(second);
- d.addFirst(third);
- assertEquals(third, d.removeLast());
- assertEquals(second, d.removeLast());
- assertEquals(first, d.removeLast());
- }
- @Test(expected=java.util.NoSuchElementException.class)
- public void empty_deque__removeFirst_throws() {
- Deque<Double> q = new Deque<Double>();
- q.removeFirst();
- }
- @Test(expected=java.util.NoSuchElementException.class)
- public void empty_deque__removeLast_throws() {
- Deque<Double> d = new Deque<Double>();
- d.removeLast();
- }
- @Test(expected=java.util.NoSuchElementException.class)
- public void iterator__next_deque__throws() {
- Deque<Double> d = new Deque<Double>();
- Iterator<Double> i = d.iterator();
- assertFalse(i.hasNext());
- i.next();
- }
- @Test(expected=NullPointerException.class)
- public void addFirst__null__throws() {
- Deque<Double> d = new Deque<Double>();
- d.addFirst(null);
- }
- @Test(expected=NullPointerException.class)
- public void addLast__null__throws() {
- Deque<Double> d = new Deque<Double>();
- d.addLast(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement