-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.java
130 lines (112 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
/*----------------------------------------------------------------
* Author: Kevin Zhou
* Written: 12/18/2016
* Last updated: 12/18/2016
*
* Compilation: javac Deque.java
* Execution: java
*
*
*
*
*----------------------------------------------------------------*/
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Deque<Item> implements Iterable<Item> {
private int n; // nodes in linkedlist
private Node current;
// container for object
private class Node {
private Item item;
private Node next;
private Node previous;
}
// construct an empty deque
public Deque() {
current = null;
n = 0;
}
// is the deque empty?
public boolean isEmpty() {
return current == null;
}
// 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 java.lang.NullPointerException();
Node oldfirst = current;
current = new Node();
current.item = item;
current.next = oldfirst;
if (current.next != null) {
oldfirst.previous = current;
}
n++;
}
// add the item to the end
public void addLast(Item item) {
if(item==null) throw new java.lang.NullPointerException();
if (current == null) {
current = new Node();
current.item = item;
} else {
while (current.next != null) current = current.next;
Node oldfirst = current;
current = new Node();
current.item = item;
current.previous = oldfirst;
oldfirst.next = current;
while (current.previous != null) current = current.previous;
}
n++;
}
// remove and return the item from the front
public Item removeFirst() {
if (current == null) throw new NoSuchElementException();
Item temp = current.item;
if (current.next != null) {
current = current.next;
current.previous = null;
}
else current = null;
n--;
return temp;
}
// remove and return the item from the end
public Item removeLast() {
if (current == null) throw new NoSuchElementException();
else if (current.next == null){
Item temp = current.item;
current = null;
n--;
return temp;
}
else {
while (current.next != null) current = current.next;
Item temp = current.item;
current = current.previous; // the last element
current.next = null;
while (current.previous != null) current = current.previous;
n--;
return temp;
}
}
// 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 now = current;
public boolean hasNext() { return now != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = now.item;
now = now.next;
return item;
}
}
}