算法第四版 练习1.3.33

Deque.java

import java.util.Iterator;

public class Deque<Item> implements Iterable<Item>{
    private class DoubleNode<Item>
    {
        private DoubleNode<Item> next = null;
        private DoubleNode<Item> last = null;
        private Item data;
    }
    private DoubleNode<Item> head = null;
    public Deque()
    {

    }
    public boolean isEmpty()
    {
        return head == null;
    }
    public int size()
    {
        if (head == null)
            return 0;
        DoubleNode<Item> current = head;
        int count = 1;
        while(current.next != null)
        {
            ++count;
            current = current.next;
        }
        return count;
    }
    public void pushLeft(Item item)
    {
        if (head == null)
        {
            head = new DoubleNode<Item>();
            head.data = item;
            return;
        }
        DoubleNode<Item> current = new DoubleNode<Item>();
        current.data = item;
        current.next = head;
        head.last = current;
        head = current;
    }
    public void pushRight(Item item)
    {
        if (head == null)
        {
            head = new DoubleNode<Item>();
            head.data = item;
            return;
        }
        DoubleNode<Item> current = head;
        while (current.next != null)
            current = current.next;
        current.next = new DoubleNode<Item>();
        current.next.data = item;
        current.next.last = current;
    }
    public Item popLeft()
    {
        if (head == null)
            throw new RuntimeException();
        Item ret = head.data;
        head = head.next;
        head.last = null;
        return ret;
    }
    public Item popRight()
    {
        if (head == null)
            throw new RuntimeException();
        DoubleNode<Item> current = head;
        while (current.next != null)
            current = current.next;
        Item ret = current.data;
        current.last.next = null;
        return ret;
    }
    public Iterator<Item> iterator()
    {
        return new DequeIterator();
    }
    private class DequeIterator implements Iterator<Item>
    {
        private DoubleNode<Item> current = head;
        public boolean hasNext()
        {
            return current != null;
        }
        public void remove()
        {

        }
        public Item next()
        {
            Item item = current.data;
            current = current.next;
            return item;
        }
    }
}

ResizingArrayDeque.java

import java.util.Iterator;

public class ResizingArrayDeque<Item> implements Iterable<Item>{
    private Item[] item;
    private int head;
    private int tail;
    @SuppressWarnings("unchecked")
    public ResizingArrayDeque(int cap)
    {
        item = (Item[])(new Object[cap]);
        if (cap <= 1)
            throw new RuntimeException();
        head = cap / 2;
        tail = cap / 2;
    }
    public boolean isEmpty()
    {
        return head == tail;
    }
    public int size()
    {
        return tail - head;
    }
    public void resize(int size)
    {
        @SuppressWarnings("unchecked")
        Item[] temp = (Item[])(new Object[size]);
        int start = size / 2 - size() / 2;
        int end = start + size();
        for (int i = start,j = head; i < end; ++i,++j)
                temp[i] = item[j];
        item = temp;
        head = start;
        tail = end;
    }
    public void pushLeft(Item data)
    {
        if (head == 0)
            resize(size() * 3);
        item[--head] = data;
    }
    public void pushRight(Item data)
    {
        if (tail == item.length)
            resize(3 * size());
        item[tail++] = data;
    }
    public Item popLeft()
    {
        if(isEmpty())
            return null;
        Item ret = item[head];
        item[head++] = null;
        return ret;
    }
    public Item popRight()
    {
        if (isEmpty())
            return null;
        Item ret = item[--tail];
        item[tail] = null;
        return ret;
    }
    public Iterator<Item> iterator()
    {
        return new ResizingArrayDequeIterator();
    }
    public class ResizingArrayDequeIterator implements Iterator<Item>
    {
        private int current = head;
        public boolean hasNext()
        {
            return current < tail;
        }
        public void remove()
        {

        }
        public Item next()
        {
            Item ret = item[current++];
            return ret;
        }
    }
}

测试:main.java

import edu.princeton.cs.algs4.StdOut;

public class Main {
    public static void main(String[] args)
    {
         Deque<Integer> dq = new Deque<Integer>();
         for (int i = 5 ; i >= 0; --i)
             dq.pushLeft(i);
         for (int i = 6; i <= 10; ++i)
             dq.pushRight(i);
         for (Integer it : dq)
             StdOut.print(it + " ");
         StdOut.println();
         for (int i = 0; i < 5; ++i)
         {
             StdOut.print(dq.popRight() + " ");
             StdOut.print(dq.popLeft() + " ");
         }
         StdOut.println();
        ResizingArrayDeque<Integer> rd = new ResizingArrayDeque<Integer>(11);
         for (int i = 5 ; i >= 0; --i)
             rd.pushLeft(i);
         for (int i = 6; i <= 10; ++i)
             rd.pushRight(i);
         for (Integer it : rd)
             StdOut.print(it + " ");
         StdOut.println();
         for (int i = 0; i < 5; ++i)
         {
             StdOut.print(rd.popRight() + " ");
             StdOut.print(rd.popLeft() + " ");
         }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值