0% found this document useful (0 votes)
19 views5 pages

Faculty of Engineering Sciences and Technology

Uploaded by

aliyank13843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Faculty of Engineering Sciences and Technology

Uploaded by

aliyank13843
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

BS(SE)V

1.0
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY

class Node {
int data;
Node prev;
Node next;

Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}

class DoublyLinkedList {
private Node head;

public DoublyLinkedList() {
this.head = null;
}

public void addFirst(int data) {


Node newNode = new Node(data);
if (head != null) {
newNode.next = head;
head.prev = newNode;
}
head = newNode;
}

public void addAfter(Node prevNode, int data) {


if (prevNode == null) {
System.out.println("The given previous node cannot be null.");
return;
}
Node newNode = new Node(data);
newNode.next = prevNode.next;
newNode.prev = prevNode;
if (prevNode.next != null) {
prevNode.next.prev = newNode;
}
prevNode.next = newNode;
}

public void addLast(int data) {


Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}

public void removeFirst() {


if (head == null) {
System.out.println("The list is empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
head = head.next;
head.prev = null;
}

public void removeAfter(Node prevNode) {


if (prevNode == null || prevNode.next == null) {
System.out.println("No node exists to remove after the given node.");
return;
}
Node nodeToRemove = prevNode.next;
prevNode.next = nodeToRemove.next;
if (nodeToRemove.next != null) {
nodeToRemove.next.prev = prevNode;
}
}

public void removeLast() {


if (head == null) {
System.out.println("The list is empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.prev.next = null;
}

public void displayList() {


Node temp = head;
while (temp != null) {
System.out.print(temp.data + (temp.next != null ? " <-> " : "\n"));
temp = temp.next;
}
}

public Node search(int key) {


Node temp = head;
while (temp != null) {
if (temp.data == key) {
return temp;
}
temp = temp.next;
}
return null;
}

public static void main(String[] args) {


DoublyLinkedList dll = new DoublyLinkedList();
dll.addFirst(10);
dll.addLast(20);
dll.addLast(30);
dll.addAfter(dll.head, 15);
dll.displayList(); // Output: 10 <-> 15 <-> 20 <-> 30
dll.removeFirst();
dll.displayList(); // Output: 15 <-> 20 <-> 30
dll.removeLast();
dll.displayList(); // Output: 15 <-> 20
dll.removeAfter(dll.head);
dll.displayList(); // Output: 15
Node foundNode = dll.search(15);
System.out.println(foundNode != null ? foundNode.data : "Not Found");
}
}

You might also like