forked from dancollins/comp317_regex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDequeue.java
118 lines (107 loc) · 1.88 KB
/
Dequeue.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
/*
* A dequeue
* Does as a dequeue do
*
* Dan Collins 1183446
* Severin Mahoney-Marsh 1181754
*/
public class Dequeue<T>{
// Node for double linked deque
private class Node<T>{
T data;
Node<T> left;
Node<T> right;
public Node(T data){
this.data = data;
}
public T getData(){
return data;
}
public void setLeft(Node<T> left){
this.left = left;
}
public Node<T> getLeft(){
return left;
}
public void setRight(Node<T> right){
this.right = right;
}
public Node<T> getRight(){
return right;
}
}
Node<T> head;
Node<T> tail;
/*
* Appends data to tail
*/
public void push(T data){
// Handle edge case with no data;
if (tail == null){
tail = new Node<T>(data);
head = tail;
} else {
tail.setRight(new Node<T>(data));
tail.getRight().setLeft(tail);
tail = tail.getRight();
}
}
/*
* Appends data to head
*/
public void unshift(T data){
// Handle edge case with no data
if (head == null){
head = new Node<T>(data);
tail = head;
} else {
head.setLeft(new Node<T>(data));
head.getLeft().setRight(head);
head = head.getLeft();
}
}
/*
* Removes from tail
*/
public T pop(){
// Is it empty?
if (tail == null){
throw new RuntimeException("The dequeue is empty.");
} else {
T value = tail.getData();
tail = tail.getLeft();
// Any data remaining?
if (tail == null){
head = null;
} else {
tail.setRight(null);
}
return value;
}
}
/*
* Removes from head
*/
public T shift(){
// Is it empty?
if (head == null){
throw new RuntimeException("The dequeue is empty.");
} else {
T value = head.getData();
head = head.getRight();
// Any data remaining?
if (head == null){
tail = null;
} else {
head.setLeft(null);
}
return value;
}
}
/*
* Tells you if the deque is empty
*/
public boolean isEmpty(){
return head == null;
}
}