-
Notifications
You must be signed in to change notification settings - Fork 173
/
doublyLinkedList.java
128 lines (108 loc) · 3.92 KB
/
doublyLinkedList.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
public class InsertMid {
//Represent a node of the doubly linked list
class Node{
int data;
Node previous;
Node next;
public Node(int data) {
this.data = data;
}
}
public int size = 0;
//Represent the head and tail of the doubly linked list
Node head, tail = null;
//addNode() will add a node to the list
public void addNode(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next will point to null, as it is the last node of the list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
//Size will count the number of nodes present in the list
size++;
}
//addInMid() will add a node to the middle of the list
public void addInMid(int data) {
//Create a new node
Node newNode = new Node(data);
//If list is empty
if(head == null) {
//Both head and tail will point to newNode
head = tail = newNode;
//head's previous will point to null
head.previous = null;
//tail's next point to null, as it is the last node of the list
tail.next = null;
}
else {
//current will point to head
Node current = head, temp = null;
//Store the mid position of the list
int mid = (size % 2 == 0) ? (size/2) : ((size+1)/2);
//Iterate through list till current points to mid position
for(int i = 1; i < mid; i++){
current = current.next;
}
//Node temp will point to node next to current
temp = current.next;
temp.previous = current;
//newNode will be added between current and temp
current.next = newNode;
newNode.previous = current;
newNode.next = temp;
temp.previous = newNode;
}
size++;
}
//display() will print out the nodes of the list
public void display() {
//Node current will point to head
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
//Prints each node by incrementing the pointer.
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
InsertMid dList = new InsertMid();
//Add nodes to the list
dList.addNode(1);
dList.addNode(2);
System.out.println("Original list: ");
dList.display();
//Adding node '3' in the middle
dList.addInMid(3);
System.out.println( "Updated List: ");
dList.display();
//Adding node '4' in the middle
dList.addInMid(4);
System.out.println("Updated List: ");
dList.display();
//Adding node '5' in the middle
dList.addInMid(5);
System.out.println("Updated List: ");
dList.display();
}
}