Princeton Algorithm Part 1 Week 2 - Deque

自己做了个双向链表,加上头尾两个node。 可能环状结构(可能叫这个名字?)更适合这道题。我在之后的RandomizedQueue用上了。还未提交。

A doubly linked list with header and trailer. A loop- like data structure can be a good option as well. (Which I did on RandomizedQueue). This solution has not yet been submitted.

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Deque implements Iterable {
    private Node front = new Node(); // the front Node;
    private Node end   = new Node(); // the end Node;
    private int  count = 0;          // Deque size;

    public Deque() {
        front.Next = end;
        end.Prev = front;
    }

    private class Node {
        Item item;
        Node Next;
        Node Prev;
    }

    public boolean isEmpty() {
        // return true when the deque is empty, vice versa;
        return count == 0;
    }

    public int size() {
        // return the size of the deque;
        return count;
    }

    public void addFirst(Item item) {
        // add the item to the front
        if (item.equals(null))
            throw new IllegalArgumentException();
        Node node = new Node();
        node.item = item;
        node.Next = front.Next;
        node.Next.Prev = node;
        node.Prev = front;
        front.Next = node;
        count++;
    }

    public void addLast(Item item) {
        // add the item to the end
        if (item.equals(null))
            throw new IllegalArgumentException();
        Node node = new Node();
        node.item = item;
        node.Prev = end.Prev;
        node.Prev.Next = node;
        node.Next = end;
        end.Prev = node;
        count++;
    }

    public Item removeFirst() {
        // remove and return the item fromt the front;
        if (isEmpty())
            throw new NoSuchElementException();
        Item result = front.Next.item;
        front.Next = front.Next.Next;
        front.Next.Prev = front;
        count--;
        return result;
    }

    public Item removeLast() {
        // remove and return the item from the end;
        if (isEmpty())
            throw new NoSuchElementException();
        Item result = end.Prev.item;
        end.Prev = end.Prev.Prev;
        end.Prev.Next = end;
        count--;
        return result;
    }

    public Iterator iterator() {
        // use the inner class
        return new DequeIterator();
    }

    private class DequeIterator implements Iterator {
        private Node current = front.Next;

        public void remove() {
            throw new UnsupportedOperationException();
        }

        public boolean hasNext() {
            return current != end;
        }

        public Item next() {
            if (current == end)
                throw new NoSuchElementException();
            Item item = current.item;
            current = current.Next;
            return item;
        }
    }

    public static void main(String[] args) {
        // unit testing;
        Deque l = new Deque();
        for (int i = 0; i < 10; i++)
            l.addFirst(i);
        for (Integer i : l) {
            System.out.println(i);
        }
        l.addFirst(3);
        System.out.println(l.removeLast());
    }
}

你可能感兴趣的:(Princeton Algorithm Part 1 Week 2 - Deque)