-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeque.java
157 lines (127 loc) · 3.5 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
import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.algs4.StdOut;
public class Deque<Item> implements Iterable<Item> {
private int count;
private class Node {
Item value;
Node prev;
Node next;
Node(final Item value) {
this.value = value;
}
}
private Node front;
private Node rear;
// construct an empty deque
public Deque() {
count = 0;
front = null;
rear = null;
}
// is the deque empty?
public boolean isEmpty() {
return count == 0;
}
// return the number of items on the deque
public int size() {
return count;
}
// add the item to the front
public void addFirst(final Item item) {
if (item == null)
throw new IllegalArgumentException();
final Node newnode = new Node(item);
if (front == null) {
front = newnode;
rear = front;
} else {
newnode.next = front;
front.prev = newnode;
front = newnode;
}
count++;
}
// add the item to the back
public void addLast(final Item item) {
if (item == null)
throw new IllegalArgumentException();
final Node newnode = new Node(item);
if (rear == null) {
rear = newnode;
front = rear;
} else {
newnode.prev = rear;
rear.next = newnode;
rear = newnode;
}
count++;
}
// remove and return the item from the front
public Item removeFirst() {
if (count == 0)
throw new NoSuchElementException();
Item temp = front.value;
front = front.next;
if (front == null)
rear = null;
else
front.prev = null;
count--;
return temp;
}
// remove and return the item from the back
public Item removeLast() {
if (count == 0)
throw new NoSuchElementException();
Item temp = rear.value;
rear = rear.prev;
if (rear == null)
front = null;
else
rear.next = null;
count--;
return temp;
}
// return an iterator over items in order from front to back
public Iterator<Item> iterator() {
return new LinkedListIterator(front);
}
private class LinkedListIterator implements Iterator<Item> {
Node currNode;
public LinkedListIterator(final Node firsNode) {
currNode = firsNode;
}
@Override
public boolean hasNext() {
return currNode != null;
}
@Override
public Item next() {
if (currNode == null)
throw new NoSuchElementException();
final Item currValue = currNode.value;
currNode = currNode.next;
return currValue;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
// unit testing (required)
public static void main(final String[] args) {
Deque<Integer> dq = new Deque<>();
dq.addFirst(12);
dq.addLast(13);
dq.addFirst(11);
dq.addLast(14);
dq.addFirst(10);
dq.addLast(15);
dq.removeFirst();
dq.removeLast();
Iterator<Integer> it = dq.iterator();
while (it.hasNext())
StdOut.println(it.next());
}
}