-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDouble_linkedlist_Deletion.java
More file actions
135 lines (121 loc) · 4.03 KB
/
Copy pathDouble_linkedlist_Deletion.java
File metadata and controls
135 lines (121 loc) · 4.03 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
// Double_linkedlist_Deletion class manages the doubly linked list operations
package datastructures;
public class Double_linkedlist_Deletion {
private class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
private Node head;
private Node tail;
private int size;
public Double_linkedlist_Deletion() {
this.head = null;
this.tail = null;
this.size = 0;
}
// Method to delete node from the beginning of the doubly linked list
public void deleteFromBeginning() {
if (isEmpty()) {
System.out.println("Doubly linked list is empty. Deletion not possible.");
return;
}
if (head == tail) { // Only one element in the list
head = null;
tail = null;
} else {
head = head.next;
head.prev = null;
}
size--;
}
// Method to delete node from the end of the doubly linked list
public void deleteFromEnd() {
if (isEmpty()) {
System.out.println("Doubly linked list is empty. Deletion not possible.");
return;
}
if (head == tail) { // Only one element in the list
head = null;
tail = null;
} else {
tail = tail.prev;
tail.next = null;
}
size--;
}
// Method to delete node at a specific index in the doubly linked list
public void deleteAt(int index) {
if (isEmpty()) {
System.out.println("Doubly linked list is empty. Deletion not possible.");
return;
}
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
if (index == 0) {
deleteFromBeginning();
} else if (index == size - 1) {
deleteFromEnd();
} else {
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next;
}
current.prev.next = current.next;
current.next.prev = current.prev;
size--;
}
}
// Method to check if the doubly linked list is empty
public boolean isEmpty() {
return size == 0;
}
// Method to display the elements of the doubly linked list
public void display() {
Node current = head;
System.out.print("Doubly linked list: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
Double_linkedlist_Deletion dll = new Double_linkedlist_Deletion();
// Inserting elements for demonstration
dll.insertAtEnd(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
dll.insertAtEnd(40);
dll.insertAtEnd(50);
dll.display(); // Output: Doubly linked list: 10 20 30 40 50
// Deleting element from the beginning
dll.deleteFromBeginning();
dll.display(); // Output: Doubly linked list: 20 30 40 50
// Deleting element from the end
dll.deleteFromEnd();
dll.display(); // Output: Doubly linked list: 20 30 40
// Deleting element at specific index
dll.deleteAt(1); // Deleting element at index 1
dll.display(); // Output: Doubly linked list: 20 40
}
// Method to insert data at the end of the doubly linked list (used for demonstration)
private void insertAtEnd(int data) {
Node newNode = new Node(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
tail = newNode;
}
size++;
}
}