Write JAVA functions to implement DELETE_FIRST_NODE and TRAVERSEoperations in doubly linked lis
Question
Write JAVA functions to implement DELETE_FIRST_NODE
and TRAVERSE
operations in doubly linked list.
Solution
Certainly! Below are Java functions to implement the DELETE_FIRST_NODE
and TRAVERSE
operations in a doubly linked list.
Step 1: Define the Node Class
First, we need to define the node structure for the doubly linked list.
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Step 2: Define the Doubly Linked List Class
Next, we define the doubly linked list class, which will contain methods for our required operations.
class DoublyLinkedList {
private Node head;
public DoublyLinkedList() {
head = null;
}
// 1. DELETE_FIRST_NODE operation
public void deleteFirstNode() {
if (head == null) {
System.out.println("List is empty. No nodes to delete.");
return;
}
// If there's only one node
if (head.next == null) {
head = null;
return;
}
// More than one node
head = head.next;
head.prev = null;
}
// 2. TRAVERSE operation
public void traverse() {
Node current = head;
if (current == null) {
System.out.println("The list is empty.");
return;
}
System.out.print("Doubly linked list: ");
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
// Method to add nodes for testing purposes
public void addNode(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
}
}
Step 3: Example Usage
Here's how you can use the doubly linked list with the provided functions:
public class Main {
public static void main(String[] args) {
DoublyLinkedList list = new DoublyLinkedList();
list.addNode(10);
list.addNode(20);
list.addNode(30);
list.traverse(); // Output: 10 20 30
list.deleteFirstNode();
list.traverse(); // Output: 20 30
list.deleteFirstNode();
list.traverse(); // Output: 30
list.deleteFirstNode();
list.traverse(); // Output: The list is empty.
}
}
Conclusion
The provided Java functions allow you to implement the DELETE_FIRST_NODE
and TRAVERSE
operations on a doubly linked list. You can add nodes for testing and verify the operations accordingly.
Similar Questions
Write JAVA functions to implement DELETE_FIRST_NODE and TRAVERSEoperations in doubly linked lis
The following LinkedList operation has similar function as removeFirst().Aremove()Bremove(Object o)Cremove(int index)DremoveFirstNode()
What is doubly Linked List? What are its applications?Explain how an element can be deleted from doublylinked list using C program
In a doubly linked list, which pointer of a node points to the previous node?nextprevpreviousback
To traverse a doubly linked list, you typically start from:Group of answer choicesEither the tail node or the head nodeThe tail nodeThe head nodeAny node
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.