-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list.java
More file actions
131 lines (100 loc) · 3.01 KB
/
linked-list.java
File metadata and controls
131 lines (100 loc) · 3.01 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
129
130
131
class Entry {
public static void main(String[] args) throws Exception {
LinkedList linkedList = new LinkedList();
linkedList.pushNode(new Node(5));
linkedList.pushNode(new Node(3));
linkedList.pushNode(new Node(9));
linkedList.pushNode(new Node(7));
Node node = linkedList.getNodeByValue(3);
linkedList.deleteNode(7);
linkedList.getNodeByValue(7);
}
}
class LinkedList {
private Node headNode;
private Node tailNode;
LinkedList() {
this.headNode = null;
}
public Node getHeadNode() {
return headNode;
}
public Node getTailNode() {
return tailNode;
}
public void pushNode(Node nextNode) {
if (tailNode == null) {
tailNode = nextNode;
}
if (this.headNode != null) {
this.headNode.setPreviousNode(nextNode);
}
nextNode.setNextNode(this.headNode);
headNode = nextNode;
}
public void deleteNode(int nodeValue) throws Exception {
Node node = getNodeByValue(nodeValue);
resetNodeRelations(node);
if(node == getTailNode()) {
tailNode = node.getPreviousNode();
} else if (node == getHeadNode()) {
headNode = node.getNextNode();
}
}
private void resetNodeRelations(Node node) {
if (node.hasNextNode() && node.hasPreviousNode()) {
Node nextNode = node.getNextNode();
Node previousNode = node.getPreviousNode();
nextNode.setPreviousNode(previousNode);
previousNode.setNextNode(nextNode);
} else if (node.hasPreviousNode() && !node.hasNextNode()) {
node.getPreviousNode().setNextNode(null);
} else {
node.getNextNode().setPreviousNode(null);
}
}
public Node getNodeByValue(int value) throws Exception {
boolean isSearchingForNode = true;
Node currentNode = this.getHeadNode();
while (isSearchingForNode) {
if (currentNode.getValue() == value) {
return currentNode;
}
if (currentNode.hasNextNode()) {
currentNode = currentNode.getNextNode();
} else {
isSearchingForNode = false;
}
}
throw new Exception("Node not found");
}
}
class Node {
private int value;
private Node nextNode;
private Node previousNode;
Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public Node getNextNode() {
return nextNode;
}
public Node getPreviousNode() {
return previousNode;
}
public boolean hasNextNode() {
return this.nextNode != null;
}
public boolean hasPreviousNode() {
return this.previousNode != null;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
public void setPreviousNode(Node previousNode) {
this.previousNode = previousNode;
}
}