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 edu.caltech.cs2.datastructures;
import edu.caltech.cs2.interfaces.IDeque;
import edu.caltech.cs2.interfaces.IQueue;
import edu.caltech.cs2.interfaces.IStack;
import java.util.Iterator;
public class LinkedDeque<E> implements IDeque<E>, IQueue<E>, IStack<E> {
private Node<E> head;
private int size;
private Node<E> tail;
public LinkedDeque(){
this.head = null;
this.tail = null;
this.size = 0;
}
private static class Node<E> {
public final E data;
public Node<E> next;
public Node<E> prev;
public Node(E data) {
this(data, null, null);
}
public Node(E data, Node<E> next, Node<E> prev) {
this.data = data;
this.next = next;
this.prev = prev;
}
}
@Override
public void addFront(E e) {
if (this.size == 0){
Node<E> tempNode = new Node<>(e);
this.tail = tempNode;
this.head = tempNode;
} else {
Node<E> tempHead = new Node<>(e, this.head, null);
// previous is this head
this.head.prev = tempHead;
//switch node
this.head = tempHead;
}
this.size++;
}
@Override
public void addBack(E e) {
if (this.size == 0){
this.head = new Node<>(e);
this.tail = this.head;
} else {
Node<E> tempTail = new Node<>(e, null, this.tail);
this.tail.next = tempTail;
this.tail = tempTail;
}
this.size++;
}
@Override
public E removeFront() {
if (this.size == 0) {
return null;
}
E tempHead = this.head.data;
this.head = this.head.next;
if (this.head != null) {
this.head.prev = null;
}
this.size--;
if (this.size == 0) {
this.tail = this.head;
}
return tempHead;
}
@Override
public E removeBack() {
if (this.size == 0) {
return null;
}
E tempHead = this.tail.data;
this.tail = this.tail.prev;
if (this.tail != null) {
this.tail.next = null;
}
this.size--;
if (this.size == 0) {
this.head = this.tail;
}
return tempHead;
}
@Override
public boolean enqueue(E e) {
addFront(e);
return true;
}
@Override
public E dequeue() {
if (this.head == null || this.tail == null){
return null;
} else {
return removeBack();
}
}
@Override
public boolean push(E e) {
addBack(e);
return true;
}
@Override
public E pop() {
if (this.head == null || this.tail == null){
return null;
} else {
return removeBack();
}
}
@Override
public E peekFront() {
if (this.size == 0){
return null;
}
return this.head.data;
}
@Override
public E peekBack() {
if (this.size == 0) {
return null;
}
return this.tail.data;
}
@Override
public E peek() {
return peekBack();
}
@Override
public Iterator<E> iterator() {
return new LinkedDequeIterator();
}
@Override
public int size() {
return this.size;
}
@Override
public String toString(){
String returnStatement = "";
if (this.size == 0) {
return "[]";
}
else {
returnStatement += "[";
Node<E> tempNode = this.head;
for (int i = 0; i < this.size; i++){
returnStatement += tempNode.data.toString();
returnStatement += ", ";
tempNode = tempNode.next;
}
returnStatement = returnStatement.substring(0, returnStatement.length() - 2);
returnStatement += "]";
return returnStatement;
}
}
private class LinkedDequeIterator implements Iterator<E> {
private Node<E> currNode = null;
public LinkedDequeIterator(){
this.currNode = LinkedDeque.this.head;
}
@Override
public boolean hasNext() {
return this.currNode != null;
}
@Override
public E next() {
E element = currNode.data;
this.currNode = currNode.next;
return element;
}
}
}