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<Item> implements Iterable<Item> {
    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<Item> iterator() {
        // use the inner class
        return new DequeIterator();
    }

    private class DequeIterator implements Iterator<Item> {
        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<Integer> l = new Deque<Integer>();
        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());
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,774评论 0 33
  • 新的开始
    阿斯顿马阅读 165评论 0 0
  • 君子处事,不拘泥于行迹,惟义所在,也不怕别人说。为什么呢,路遥知马力,日久见人心,你若是正人君子,时间长了,人人都...
    华杉2009阅读 889评论 0 3
  • 今宵再過故園扉 一樹桃花去不知 忽憶去年霜燭下 蛾眉素手作新詩
    瓶蓋阅读 172评论 0 1
  • 如果不是天生或者从小就会那么受母语以及英语影响,一般人的舌头是比较僵硬的一个状态。那我们第一步要做的就是让你的舌头...
    SafariTime阅读 430评论 0 0