-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoublyLinkedList.java
More file actions
147 lines (126 loc) · 3.79 KB
/
DoublyLinkedList.java
File metadata and controls
147 lines (126 loc) · 3.79 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
140
141
142
143
144
145
146
147
import java.util.NoSuchElementException;
/*
null <-- [previous|data|next] <--> [previous|data|next] <--> [previous|data|next] <--> [previous|data|next] --> null
^ ^
Head Tail
In Doubly Linked list The node contain address of next node as well as addess of previous node.
We can add element, remove element and traverse the Doubly Linked List in both forward and Backward direction.
In Doubly Linked List we can delete the node even if we don't have pointer to its previous node.
*/
public class DoublyLinkedList {
private ListNode head;
private ListNode tail;
private int length;
// For creating a node in DLL
private class ListNode {
private int data;
private ListNode next;
private ListNode previous;
public ListNode(int data) {
this.data = data;
}
}
public DoublyLinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
// to check the DLL conatin data or not
public boolean isEmpty() {
return length ==0;
}
public int length() {
return length;
}
// Insert at the begining of the Doubly Linked List
public void insertFirst(int value) {
ListNode newNode = new ListNode(value);
if(isEmpty()){
tail = newNode;
} else {
head.previous = newNode;
}
newNode.next = head;
head = newNode;
length++;
}
// Insert at the end of Doubly Linked List
public void insertLast(int value) {
ListNode newNode = new ListNode(value);
if(isEmpty()){
head = newNode;
} else {
tail.next = newNode;
newNode.previous = tail;
}
tail = newNode;
length++;
}
// Print DLL in Forward direction
public void displayForward() {
if(head == null)
return;
ListNode temp = head;
while(temp != null) {
System.out.print(temp.data+" --> ");
temp = temp.next;
}
System.out.println("null");
}
// Print DLL in Backward direction
public void displayBackward() {
if(tail == null)
return;
ListNode temp = tail;
while (temp != null) {
System.out.print(temp.data+" --> ");
temp = temp.previous;
}
System.out.println("null");
}
// Delete the First node in DLL
public ListNode deleteFirst(){
if(isEmpty()){
throw new NoSuchElementException();
}
ListNode temp = head;
if(head == tail){
tail = null;
} else {
head.next.previous = null;
}
head = head.next;
temp.next = null;
length--;
return temp;
}
// To remove the Last Element from Doubly Linked List
public ListNode deleteLast(){
if(isEmpty()){
throw new NoSuchElementException();
}
ListNode temp = tail;
if(tail == head){
head = null;
} else {
tail.previous.next = null;
}
tail = tail.previous;
temp.previous=null;
length--;
return temp;
}
public static void main(String [] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.insertLast(1);
dll.insertLast(10);
dll.insertLast(15);
dll.insertLast(25);
dll.displayForward();
dll.displayBackward();
System.out.println(dll.deleteFirst().data);
dll.displayForward();
System.out.println(dll.deleteLast().data);
dll.displayForward();
}
}