-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoubleDoubleLL.java
162 lines (135 loc) · 4.3 KB
/
DoubleDoubleLL.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Name: Benjamin Bush
public class DoubleDoubleLL {
// Represents each node in the doubly linked list
private class Node {
double data;
Node next;
Node prev;
public Node(double data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Initalizes the head, tail, and current
private Node head;
private Node tail;
private Node curr;
public DoubleDoubleLL() {
head = null;
tail = null;
curr = null;
}
// Moves current reference foward by one node
public void gotoNext() {
if (curr != null) curr = curr.next;
}
// Moves current reference backwards by one node
public void gotoPrev() {
if (curr != null) curr = curr.prev;
}
// Resets current reference to the head
public void reset() {
curr = head;
}
// Moves current reference to the tail
public void gotoEnd() {
curr = tail;
}
// Checks if there are more nodes to traverse
public boolean hasMore() {
return (curr != null);
}
// Returns the data from the current reference
public double getCurrent() {
return (curr == null) ? null : curr.data;
}
// Changes the data at the current reference
public void setCurrent(double data) {
if (curr != null) curr.data = data;
}
// Adds new node to the end and updates applicable references
public void add(double data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
}
// Adds new node next to current and updates applicable references
public void addAfterCurrent(double data) {
if (curr == null) return;
Node newNode = new Node(data);
newNode.next = curr.next;
newNode.prev = curr;
if (curr.next != null) curr.next.prev = newNode;
else tail = newNode;
curr.next = newNode;
}
// Removes a specified node given data and updates applicable references
public void remove(double data) {
Node temporary = head;
while (temporary != null) {
if (temporary.data == data) {
// Case 1: Node is the head
if (temporary == head) {
head = head.next;
if (head != null) head.prev = null;
}
// Case 2: Node is the tail
else if (temporary == tail) {
tail = tail.prev;
if (tail != null) tail.next = null;
}
// Case 3: Node is somewhere in the middle
else {
temporary.prev.next = temporary.next;
temporary.next.prev = temporary.prev;
}
}
temporary = temporary.next;
}
}
// Removes the current node and updates applicable references
public void removeCurrent() {
if (curr == null) return;
// Case 1: Node is the head
if (curr == head) {
head = head.next;
if (head != null) head.prev = null;
}
// Case 1: Node is the tail
else if (curr == tail) {
tail = tail.prev;
if (tail != null) tail.next = null;
}
// Case 1: Node is somewhere in the middle
else {
curr.prev.next = curr.next;
curr.next.prev = curr.prev;
}
curr = curr.next;
}
// Prints out the whole list
public void print() {
Node temporary = head;
while (temporary != null) {
System.out.println(temporary.data);
temporary = temporary.next;
}
}
// Checks if the list contains a specific value
public boolean contains(double data) {
Node temporary = head;
while (temporary != null) {
if (temporary.data == data) return true;
temporary = temporary.next;
}
return false;
}
}