-
Notifications
You must be signed in to change notification settings - Fork 24
/
Deque.java
163 lines (147 loc) · 3.76 KB
/
Deque.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package two;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @author sunyue
* @version 1.0 2017/1/9 16:23
*/
public class Deque<Item> implements Iterable<Item> {
private Node first;
private Node last;
// null node
// 一开始使用null而不是null Node,各种错误。
private Node nul;
private int size;
/**
* construct an empty deque
*/
public Deque() {
nul = new Node(null, null, null);
first = nul;
last = nul;
size = 0;
}
/**
* is the deque empty?
*
* @return true or false
*/
public boolean isEmpty() {
return first.item == null && last.item == null;
}
/**
* return the number of items on the deque
*
* @return size
*/
public int size() {
return size;
}
/**
* add the item to the front
*
* @param item which to be added to the front
*/
public void addFirst(Item item) {
if (null == item) throw new NullPointerException("Can't add null to the deque!");
Node newFirst = new Node(nul, first, item);
if (isEmpty()) {
last = newFirst;
first = newFirst;
} else {
first.prev = newFirst;
first = newFirst;
}
size++;
}
/**
* add the item to the end
*
* @param item which to be added to the end
*/
public void addLast(Item item) {
if (item == null) throw new NullPointerException("Can't add null to the deque!");
Node newLast = new Node(last, nul, item);
if (isEmpty()) {
first = newLast;
last = newLast;
} else {
last.next = newLast;
last = newLast;
}
size++;
}
/**
* remove and return the item from the front
*
* @return item which to be removed from the front
*/
public Item removeFirst() {
if (isEmpty()) throw new NoSuchElementException("The deque is empty!");
Item item = first.item;
first = first.next;
// is only one item
if (first.item == null) {
last = nul;
}else {
first.prev = nul;
}
size--;
return item;
}
/**
* remove and return the item from the end
*
* @return item which to be removed from the end
*/
public Item removeLast() {
if (isEmpty()) throw new NoSuchElementException("The deque is empty!");
Item item = last.item;
last = last.prev;
// is only one item
if (last.item == null) {
first = nul;
}else {
last.next = nul;
}
size--;
return item;
}
/**
* return an iterator over items in order from front to end
*
* @return Iterator
*/
@Override
public Iterator<Item> iterator() {
return new DequeIterator();
}
private class DequeIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
return current.next != null;
}
@Override
public Item next() {
if (!hasNext()) throw new NoSuchElementException("There is no more items!");
Item item = current.item;
current = current.next;
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove operator is unsupported!");
}
}
private class Node {
private Node prev;
private Node next;
private Item item;
Node(Node prev, Node next, Item item) {
this.prev = prev;
this.next = next;
this.item = item;
}
}
}