-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeque.java
More file actions
144 lines (125 loc) · 3.4 KB
/
Deque.java
File metadata and controls
144 lines (125 loc) · 3.4 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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private Node first = null;
private Node last = null;
private int n;
// construct an empty deque
public Deque() {
first = null;
last = null;
n = 0;
}
// unit testing (required)
public static void main(String[] args) {
// Something is here
}
// is the deque empty?
public boolean isEmpty() {
return n == 0;
}
// return the number of items on the deque
public int size() {
return n;
}
// add the item to the front
public void addFirst(Item item) {
if (item == null)
throw new IllegalArgumentException();
Node add = new Node();
add.item = item;
if (isEmpty()) {
add.prev = null;
add.next = null;
first = add;
last = add;
}
else {
first.next = add;
add.prev = first;
add.next = null;
first = add;
}
n++;
}
// add the item to the back
public void addLast(Item item) {
if (item == null)
throw new IllegalArgumentException();
Node add = new Node();
add.item = item;
if (isEmpty()) {
add.prev = null;
add.next = null;
first = add;
last = add;
}
else {
last.prev = add;
add.prev = null;
add.next = last;
last = add;
}
n++;
}
// remove and return the item from the front
public Item removeFirst() {
Item item = null;
if (n > 1) {
item = first.item;
first = first.prev;
first.next = null;
} else if (n == 1) {
item = first.item;
first.prev = null;
first.next = null;
} else if (n == 0) {
throw new NoSuchElementException();
}
n--;
return item;
}
// remove and return the item from the back
public Item removeLast() {
Item item = null;
if (n > 1) {
item = last.item;
last = last.next;
last.prev = null;
} else if (n == 1) {
item = last.item;
last.prev = null;
last.next = null;
} else if (n == 0) {
throw new NoSuchElementException("Queue Underflow");
}
n--;
return item;
}
// return an iterator over items in order from front to back
public Iterator<Item> iterator() {
return new DequeIterator();
}
private class Node {
Item item;
Node prev;
Node next;
}
private class DequeIterator implements Iterator<Item> {
private Node current = first;
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
public Item next()
{
if (!hasNext())
throw new NoSuchElementException();
Item item = current.item;
current = current.prev;
return item;
}
}
}