-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.java
192 lines (161 loc) · 4.3 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
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
import java.util.Iterator;
public class Deque<Item> implements Iterable<Item> {
private Node front;
private Node end;
private int size;
public Deque(){ // construct an empty deque
front = new Node();
end = new Node();
}
public boolean isEmpty(){ // is the deque empty?
return size == 0;
}
public int size(){ // return the number of items on the deque
return size;
}
public void addFirst(Item item){ // insert the item at the front
if(item == null){
throw new java.lang.NullPointerException();
}else{
if(isEmpty()){
front = new Node(item);
end = front;
size++;
} else{
front.prev = new Node(item);
front.prev.next = front;
front = front.prev;
size++;
}
}
}
public void addLast(Item item){ // insert the item at the end
if(item == null){
throw new java.lang.NullPointerException();
} else {
if(isEmpty()){
end = new Node(item);
front = end;
size++;
}else{
end.next = new Node(item);
end.next.prev = end;
end = end.next;
size++;
}
}
}
public Item removeFirst(){ // delete and return the item at the front
if(isEmpty()){
throw new java.util.NoSuchElementException();
}
Item item = front.data;
front = front.next;
if(front == null){
end = null;
} else {
front.prev = null;
}
size--;
return item;
}
public Item removeLast(){ // delete and return the item at the end
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}
Item item = end.data;
end = end.prev;
if(end == null){
front = null;
} else {
end.next = null;
}
size--;
return item;
}
public Iterator<Item> iterator(){ // return an iterator over items in order from front to end
return new DequeIterator<Item>();
}
private class DequeIterator<E> implements Iterator<E>{
private Node current;
public DequeIterator() {
this.current = front;
}
@Override
public boolean hasNext() {
if(front.next == null && front.prev == null && front.data == null) {
return false;
}
return current != null;
}
@SuppressWarnings("unchecked")
@Override
public E next() {
if(!hasNext()){
throw new java.util.NoSuchElementException();
}
Item item = current.data;
current = current.next;
return (E)item;
}
@Override
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
}
private class Node {
private Node prev;
private Node next;
private Item data;
public Node(Item data) {
this.data = data;
this.prev = null;
this.next = null;
}
public Node() {
this.data = null;
this.prev = null;
this.next = null;
}
}
public static void main(String[] args){ // unit testing
Deque<Integer> deq2 = new Deque<Integer>();
Iterator<Integer> itr3 = deq2.iterator();
System.out.println(itr3.hasNext());
System.out.println("deq2: " + deq2.toString());
System.out.println("size: " + deq2.size());
deq2.addFirst(1);
deq2.addFirst(2);
deq2.addFirst(3);
deq2.addFirst(4);
deq2.addFirst(5);
System.out.println("deq2: " + deq2.toString());
deq2.removeLast();
System.out.println("deq2: " + deq2.toString());
deq2.removeFirst();
deq2.removeFirst();
System.out.println("deq2: " + deq2.toString());
System.out.println("size: " + deq2.size());
deq2.removeLast();
deq2.removeLast();
System.out.println("deq2: " + deq2.toString());
deq2.addFirst(1);
deq2.addLast(2);
System.out.println("deq2: " + deq2.toString());
deq2.addFirst(3);
deq2.addLast(4);
System.out.println("deq2: " + deq2.toString());
System.out.println("size: " + deq2.size());
Iterator<Integer> itr = deq2.iterator();
Iterator<Integer> itr2 = deq2.iterator();
//System.out.println(itr.);
System.out.println(itr.next());
System.out.println(itr.next());
System.out.println(itr.next());
System.out.println(itr.next());
System.out.println(itr2.next());
System.out.println(itr2.next());
System.out.println(itr2.next());
System.out.println(itr2.next());
}
}