-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLinkedListDeque.java
148 lines (134 loc) · 3.58 KB
/
LinkedListDeque.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
/**
* Deque (usually pronounced like “deck”) is an irregular acronym of
* double-ended queue. Double-ended queues are sequence containers with dynamic
* sizes that can be expanded or contracted on both ends (either its front or
* its back).
*/
public class LinkedListDeque<T> implements Deque<T> {
private class Node {
private Node prev;
private T item;
private Node next;
Node(LinkedListDeque<T>.Node prev, T item, LinkedListDeque<T>.Node next) {
this.prev = prev;
this.item = item;
this.next = next;
}
}
private Node sentinel;
private int size;
public LinkedListDeque() {
sentinel = new Node(null, (T) new Object(), null);
sentinel.prev = sentinel;
sentinel.next = sentinel;
size = 0;
}
/**
* Adds an item of type T to the front of the deque.
*/
@Override
public void addFirst(T item) {
Node newNode = new Node(sentinel, item, sentinel.next);
sentinel.next.prev = newNode;
sentinel.next = newNode;
size++;
}
/**
* Adds an item of type T to the back of the deque.
*/
@Override
public void addLast(T item) {
Node newNode = new Node(sentinel.prev, item, sentinel);
sentinel.prev.next = newNode;
sentinel.prev = newNode;
size++;
}
/**
* Returns true if deque is empty, false otherwise.
*/
@Override
public boolean isEmpty() {
return size == 0;
}
/**
* Returns the number of items in the deque.
*/
@Override
public int size() {
return size;
}
/**
* Prints the items in the deque from first to last, separated by a space.
*/
@Override
public void printDeque() {
for (Node i = sentinel.next; i != sentinel; i = i.next) {
if (i.next == sentinel) {
System.out.println(i.item);
break;
}
System.out.print(i.item + " ");
}
}
/**
* Removes and returns the item at the front of the deque. If no such item
* exists, returns null.
*/
@Override
public T removeFirst() {
if (isEmpty()) {
return null;
}
T res = sentinel.next.item;
sentinel.next = sentinel.next.next;
sentinel.next.prev = sentinel;
size--;
return res;
}
/**
* Removes and returns the item at the back of the deque. If no such item
* exists, returns null.
*/
@Override
public T removeLast() {
if (isEmpty()) {
return null;
}
T res = sentinel.prev.item;
sentinel.prev = sentinel.prev.prev;
sentinel.prev.next = sentinel;
size--;
return res;
}
/**
* Gets the item at the given index, where 0 is the front, 1 is the next item,
* and so forth. If no such item exists, returns null. Must not alter the deque!
*/
@Override
public T get(int index) {
if (size < index) {
return null;
}
Node p = sentinel.next;
while (index > 0) {
p = p.next;
index--;
}
return p.item;
}
/**
* Same as get, but uses recursion.
*/
public T getRecursive(int index) {
if (size < index) {
return null;
}
return getRecursive(sentinel.next, index);
}
private T getRecursive(LinkedListDeque<T>.Node node, int i) {
if (i == 0) {
return node.item;
}
return getRecursive(node.next, i - 1);
}
}