Repository navigation
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
67 lines (58 loc) · 1.5 KB
/
Copy pathDoublyLinkedList.java
File metadata and controls
67 lines (58 loc) · 1.5 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
import java.util.*;
class DNode {
String data;
DNode next;
DNode prev;
DNode(String data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
public class DoublyLinkedList {
DNode head;
DNode tail;
DoublyLinkedList(String data) {
this.head = new DNode(data);
this.tail = this.head;
}
void append(String data) {
DNode newNode = new DNode(data);
this.tail.next = newNode;
newNode.prev = this.tail;
this.tail = newNode;
}
void remove(String data) {
DNode node = new DNode(data);
if (node.prev != null) {
node.prev.next = node.next;
} else {
this.head = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
} else {
this.tail = node.prev;
}
}
List<String> printList() {
DNode current = this.head;
List<String> list = new ArrayList<String>();
while (current != null) {
list.add(current.data);
current = current.next;
}
return list;
}
// @Override
// public String toString() {
// StringBuilder sb = new StringBuilder();
// DNode current = this.head;
// while (current != null) {
// sb.append(current.data);
// if (current.next != null) sb.append(" -> ");
// current = current.next;
// }
// return sb.toString();
// }
}