-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
88 lines (72 loc) · 2.07 KB
/
Copy pathMain.java
File metadata and controls
88 lines (72 loc) · 2.07 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
package day31;
class DLLNode {
int data;
DLLNode next;
DLLNode prev;
DLLNode(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
DLLNode head;
void deleteNode(int key) {
// Start from the head
DLLNode current = head;
// Find the node to be deleted
while (current != null && current.data != key) {
current = current.next;
}
// Node not found
if (current == null) {
System.out.println("Node not found");
return;
}
// Update the next node's prev pointer
if (current.next != null) {
current.next.prev = current.prev;
}
// Update the prev node's next pointer
if (current.prev != null) {
current.prev.next = current.next;
} else {
// If the node to be deleted is the head
head = current.next;
}
// Finally, delete the node
current = null;
}
// Method to add nodes to the list for testing
void push(int new_data) {
DLLNode new_node = new DLLNode(new_data);
new_node.next = head;
new_node.prev = null;
if (head != null) {
head.prev = new_node;
}
head = new_node;
}
void printList() {
DLLNode node = head;
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
}
public class Main {
public static void main(String[] args) {
DoublyLinkedList dll = new DoublyLinkedList();
dll.push(2);
dll.push(4);
dll.push(8);
dll.push(10);
System.out.println("Original list: ");
dll.printList();
System.out.println("\nDeleting node with value 8");
dll.deleteNode(8);
System.out.println("Updated list: ");
dll.printList();
}
}