算法第四版 练习1.3.33
来源:互联网 发布:orbslam2 kitti数据集 编辑:程序博客网 时间:2024/06/05 22:32
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
- 算法第四版 练习1.3.33
- 算法-第四版-练习1.3.33解答
- 算法第四版 练习1.3.31
- 算法第四版 练习 1.3.34
- 算法第四版 练习1.3.35
- 算法第四版 练习 1.3.39
- 算法第四版 第一章练习
- 算法-第四版-练习1.3.1解答
- 算法-第四版-练习1.3.2解答
- 算法-第四版-练习1.3.3解答
- 算法-第四版-练习1.3.4解答
- 算法-第四版-练习1.3.5解答
- 算法-第四版-练习1.3.6解答
- 算法-第四版-练习1.3.7解答
- 算法-第四版-练习1.3.8解答
- 算法-第四版-练习1.3.9解答
- 算法-第四版-练习1.3.10解答
- 算法-第四版-练习1.3.11解答
- 浅谈RBSP trailing bits syntax
- zepto.js中swipe失效问题
- PAT B1030. 完美数列
- Appium 小白从零安装 ,Appium连接真机测试。
- 不互质的中国剩余定理-HDU1573
- 算法第四版 练习1.3.33
- Day05 高可用集群2
- 红帽开源专家解读容器与OpenStack发展趋势
- 关于Android加载状态视图切换
- Git-实战篇-创建本地仓库和关联远程仓库-命令行
- js需要删除页面中某个元素
- Java 从设计者的角度理解Java IO流
- 暑期学习记录05
- [转]各种Mysql技巧总结