Repository navigation
Expand file tree
/
Copy pathDeque.java
More file actions
202 lines (163 loc) · 4.57 KB
/
Copy pathDeque.java
File metadata and controls
202 lines (163 loc) · 4.57 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package queues;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private Node head = null;
private Node tail = null;
private int size = 0;
// construct an empty deque
public Deque() {
}
// is the deque empty?
public boolean isEmpty() {
return size == 0;
}
// return the number of items on the deque
public int size() {
return this.size;
}
// add the item to the front
public void addFirst(Item item) {
if (null == item) {
throw new NullPointerException();
} else if (size == 0) {
Node current = new Node(item, null, null);
head = current;
tail = current;
incSize();
} else {
Node current = new Node(item, head, null);
head.setAhead(current);
head = current;
incSize();
}
}
// add the item to the end
public void addLast(Item item) {
if (null == item) {
throw new NullPointerException();
} else if (size == 0) {
Node current = new Node(item, null, null);
head = current;
tail = current;
incSize();
} else {
Node current = new Node(item, null, tail);
tail.setNext(current);
tail = current;
incSize();
}
}
private void incSize() {
size++;
}
private void decSize() {
size--;
}
// remove and return the item from the front
public Item removeFirst() {
if (size == 0) {
throw new NoSuchElementException();
}
Node oldHead = head;
Item item = head.getItem();
head = head.getNext();
// reset the pointer of the head to let GC the oldHead memory
if (size != 1) {
head.setAhead(null);
}
oldHead.setNext(null);
decSize();
return item;
}
// remove and return the item from the end
public Item removeLast() {
if ( size == 0) {
throw new NoSuchElementException();
}
Item item = tail.getItem();
Node oldTail = tail;
tail = tail.getAhead();
// reset the pointer of the head to let GC the oldHead memory
if (size != 1) {
tail.setNext(null);
}
oldTail.setAhead(null);
decSize();
return item;
}
// unit testing
public static void main(String[] args) {
Deque<String> deque = new Deque<String>();
deque.addFirst("hello");
deque.addFirst("world");
deque.addLast("fucj");
for (String string : deque) {
System.out.println(string);
}
System.out.println(deque.size);
String First1 = deque.removeLast();
String First2 = deque.removeLast();
String First3 = deque.removeLast();
System.out.println(First1);
System.out.println(First2);
System.out.println(First3);
}
// return an iterator over items in order from front to end
public Iterator<Item> iterator() {
return new DequeIterator();
}
private class DequeIterator implements Iterator<Item> {
private Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
if (null == current) {
throw new NoSuchElementException();
}
Item item = current.getItem();
current = current.getNext();
return item;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
private class Node {
private Item item;
private Node next;
private Node ahead;
public Node getAhead() {
return ahead;
}
public void setAhead(Node ahead) {
this.ahead = ahead;
}
public Node() {
}
public Node(Item item) {
this.item = item;
}
public Node(Item item, Node next, Node ahead) {
this.item = item;
this.next = next;
this.ahead = ahead;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}