-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
139 lines (118 loc) · 3.93 KB
/
DoublyLinkedList.java
File metadata and controls
139 lines (118 loc) · 3.93 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
132
133
134
135
136
137
138
139
/**
* Question:
* Implement (from scratch, without using Java library components) a doubly linked list (of
* integers) class which allows new integers to be added to either end of the list, any referenced
* node to be removed, and the first and last integers to be read quickly.
*
* The class has an internal class node. This represents the nodes in the list.
* The class is quite simple, it allows to add new nodes at each end.
* The use of a head and tail parameter allows for fast access of the data.
* There is no need to traverse the list to get the data at each end.
* You can delete all the nodes that are referenced by using the removeNode method.**/
public class DoublyLinkedList {
static class Node{
//the data that the class contains
int data = 0;
Node prev = null;
Node next = null;
public Node(int data){
this.data = data;
}
}
Node start, end = null;
public void addStart(int add){
// adds node at the head of the list
// accounts if the list is empty or not
Node newNode = new Node(add);
if (start != null){
newNode.next = start;
newNode.prev = null;
start.prev = newNode;
start = newNode;
} else {
start = end = newNode;
start.prev = null;
end.next = null;
}
}
public void addEnd(int add){
// adds node at the tail of the list
// accounts if the list is empty or not
Node newNode = new Node(add);
if (end != null){
newNode.next = null;
newNode.prev = end;
end.next = newNode;
end = newNode;
} else {
start = end = newNode;
start.prev = null;
end.next = null;
}
}
public int firstNode(){
//returns data from the head node
return start.data;
}
public int endNode(){
//returns data from the tail
return end.data;
}
public void removeNode(Node node){
//accounts for all the unique cases
if (start == null || node == null) {
//no need to check for tail as it will be null
return;
}
// If node to be deleted is head node
if (start == node) {
start = node.next;
return;
}
if (end == node) {
//if node to be deleted is tail
end.prev.next = null;
end = end.prev;
return;
}
//accounts for all the nodes in between
Node current = start;
while (current != node){
current = current.next;
}
current.next.prev = current.prev;
current.prev.next = current.next;
}
public void displayNode() {
//displays all the nodes
Node currentNode = start;
if(start == null) {
System.out.println("Linked List is empty");
return;
}
System.out.println("Nodes: ");
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
while(currentNode != null) {
System.out.print(currentNode.data + " ");
currentNode = currentNode.next;
}
}
public static void main(String[] args){
DoublyLinkedList doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.addStart((9));
doublyLinkedList.addEnd((7));
doublyLinkedList.addEnd(5);
doublyLinkedList.addEnd(3);
doublyLinkedList.addStart(1);
doublyLinkedList.addEnd((12));
doublyLinkedList.addStart((31));
doublyLinkedList.displayNode();
System.out.println("\n");
doublyLinkedList.removeNode(doublyLinkedList.end.prev.prev);
doublyLinkedList.displayNode();
System.out.println();
System.out.println(doublyLinkedList.endNode());
System.out.println(doublyLinkedList.firstNode());
}
}