|
| 1 | +// Node class representing each element of the doubly linked list |
| 2 | +class Node { |
| 3 | + int data; |
| 4 | + Node prev, next; |
| 5 | + |
| 6 | + // Constructor to create a new node with a given data |
| 7 | + public Node(int data) { |
| 8 | + this.data = data; |
| 9 | + prev = next = null; |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +// DoublyLinkedList class for basic operations |
| 14 | +public class DoublyLinkedList { |
| 15 | + Node head; |
| 16 | + |
| 17 | + // Constructor to initialize an empty doubly linked list |
| 18 | + public DoublyLinkedList() { |
| 19 | + head = null; |
| 20 | + } |
| 21 | + |
| 22 | + // Insert a new node at the end of the doubly linked list |
| 23 | + public void insert(int data) { |
| 24 | + Node newNode = new Node(data); |
| 25 | + |
| 26 | + // If the list is empty, make the new node the head |
| 27 | + if (head == null) { |
| 28 | + head = newNode; |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Otherwise, traverse to the end of the list |
| 33 | + Node last = head; |
| 34 | + while (last.next != null) { |
| 35 | + last = last.next; |
| 36 | + } |
| 37 | + |
| 38 | + // Update the next and prev pointers to insert the new node |
| 39 | + last.next = newNode; |
| 40 | + newNode.prev = last; |
| 41 | + } |
| 42 | + |
| 43 | + // Delete a node with the given data from the doubly linked list |
| 44 | + public void delete(int data) { |
| 45 | + if (head == null) return; // List is empty, nothing to delete |
| 46 | + |
| 47 | + Node current = head; |
| 48 | + |
| 49 | + // Traverse the list to find the node to be deleted |
| 50 | + while (current != null && current.data != data) { |
| 51 | + current = current.next; |
| 52 | + } |
| 53 | + |
| 54 | + // Node not found |
| 55 | + if (current == null) return; |
| 56 | + |
| 57 | + // If the node to be deleted is the head |
| 58 | + if (current == head) { |
| 59 | + head = current.next; |
| 60 | + } |
| 61 | + |
| 62 | + // Update pointers to remove the node |
| 63 | + if (current.next != null) { |
| 64 | + current.next.prev = current.prev; |
| 65 | + } |
| 66 | + |
| 67 | + if (current.prev != null) { |
| 68 | + current.prev.next = current.next; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Traverse the list from the head to the end |
| 73 | + public void traverseForward() { |
| 74 | + Node current = head; |
| 75 | + System.out.print("Doubly Linked List (forward): "); |
| 76 | + while (current != null) { |
| 77 | + System.out.print(current.data + " "); |
| 78 | + current = current.next; |
| 79 | + } |
| 80 | + System.out.println(); |
| 81 | + } |
| 82 | + |
| 83 | + // Traverse the list from the end to the head (reverse) |
| 84 | + public void traverseBackward() { |
| 85 | + if (head == null) return; // Empty list |
| 86 | + |
| 87 | + // Traverse to the last node |
| 88 | + Node last = head; |
| 89 | + while (last.next != null) { |
| 90 | + last = last.next; |
| 91 | + } |
| 92 | + |
| 93 | + // Traverse backward using the prev pointers |
| 94 | + System.out.print("Doubly Linked List (backward): "); |
| 95 | + while (last != null) { |
| 96 | + System.out.print(last.data + " "); |
| 97 | + last = last.prev; |
| 98 | + } |
| 99 | + System.out.println(); |
| 100 | + } |
| 101 | + |
| 102 | + // Search for a node with the given data |
| 103 | + public boolean search(int data) { |
| 104 | + Node current = head; |
| 105 | + |
| 106 | + // Traverse the list to find the data |
| 107 | + while (current != null) { |
| 108 | + if (current.data == data) return true; |
| 109 | + current = current.next; |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + // Driver method for testing the doubly linked list implementation |
| 116 | + public static void main(String[] args) { |
| 117 | + DoublyLinkedList dll = new DoublyLinkedList(); |
| 118 | + |
| 119 | + // Inserting nodes into the doubly linked list |
| 120 | + dll.insert(10); |
| 121 | + dll.insert(20); |
| 122 | + dll.insert(30); |
| 123 | + dll.insert(40); |
| 124 | + dll.insert(50); |
| 125 | + |
| 126 | + // Traversing forward |
| 127 | + System.out.println("Traversing forward:"); |
| 128 | + dll.traverseForward(); // Expected output: 10 20 30 40 50 |
| 129 | + |
| 130 | + // Traversing backward |
| 131 | + System.out.println("\nTraversing backward:"); |
| 132 | + dll.traverseBackward(); // Expected output: 50 40 30 20 10 |
| 133 | + |
| 134 | + // Deleting a node |
| 135 | + System.out.println("\nDeleting node 30:"); |
| 136 | + dll.delete(30); |
| 137 | + dll.traverseForward(); // Expected output (without 30): 10 20 40 50 |
| 138 | + |
| 139 | + // Searching for elements |
| 140 | + System.out.println("\nSearching for 40: " + dll.search(40)); // Output: true |
| 141 | + System.out.println("Searching for 60: " + dll.search(60)); // Output: false |
| 142 | + } |
| 143 | +} |
0 commit comments