forked from srafi1-stuycs/deque
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLDeque.java
More file actions
128 lines (113 loc) · 3.17 KB
/
LLDeque.java
File metadata and controls
128 lines (113 loc) · 3.17 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
public class LLDeque<T> implements Deque<T> {
//instance variables
private int _size;
private DLLNode<T> _front, _end;
//constructor
public LLDeque() {
_size = 0;
_front = null;
_end = null;
}
//returns true if the deque is empty
public boolean isEmpty() {
return _size == 0;
}
//returns the size
public int size() {
return _size;
}
//adds a new DLLNode to the front of the Deque
public void addFront(T obj) {
//if the queue is empty, set the DLLNode as the front and back
if (isEmpty()) {
DLLNode<T> l = new DLLNode<T>(obj, null, null);
_front = l;
_end = l;
}
//otherwise, set _front as the DLLNode, and point it to the old _front
else {
_front = new DLLNode<T>(obj, null, _front);
}
_size++;
}
//removes an DLLNode from the front of the Deque
//returns the value in that DLLNode
public T removeFront() {
//a temp set to return later
T ret = _front.getValue();
//sets the new _front as the second in line
_front = _front.getNext();
_size--;
//if the deque only has one DLLNode to begin with, the deque must now be empty
if (isEmpty())
_end = null;
//otherwise, if the deque ends up not empty, have _front point ahead to null
else
_front.setPrev(null);
return ret;
}
//returns the value in the DLLNode in the front of the Deque
public T peekFront() {
return _front.getValue();
}
//addes a new DLLNode to the back of the Deque
public void addLast(T obj) {
//if the deque is empty, set the DLLNode as _front and _end
if (isEmpty()) {
DLLNode<T> l = new DLLNode<T>(obj, null, null);
_front = l;
_end = l;
}
//if deque isn't empty
else {
//have _end point to the new DLLNode
_end.setNext(new DLLNode<T>(obj, _end, null));
//set _end to that DLLNode
_end = _end.getNext();
}
_size++;
}
//remove the DLLNode from the end of the Deque
//returns the value of that DLLNode
public T removeLast() {
//temp storage for later return
T ret = _end.getValue();
//set _end to the next to last, since the last will be removed
_end = _end.getPrev();
_size--;
//if deque originally has only one DLLNode, this makes sure both _front and _end are null
if (isEmpty())
_front = null;
//otherwise, set _end pointing to null instead of the removed DLLNode
else
_end.setNext(null);
return ret;
}
//returns the value of the last DLLNode in the Deque
public T peekLast() {
return _end.getValue();
}
//prints the Deque with the front on the left and end on the right
public String toString() {
String ret = "FRONT-->";
DLLNode<T> curr = _front;
while (curr != null) {
ret += curr.getValue() + "-->";
curr = curr.getNext();
}
ret += "END";
return ret;
}
public static void main(String args[]) {
LLDeque<String> deque = new LLDeque<String>();
System.out.println("Starts as: " + deque);
deque.addFront("bip");
deque.addLast("bop");
deque.addFront("bam");
System.out.println("Now it's: " + deque);
System.out.println(deque.removeLast());
System.out.println("Removed one: " + deque);
System.out.println(deque.removeFront());
System.out.println("Removed one: " + deque);
}
}