Knowee
Questions
Features
Study Tools

Write JAVA functions to implement DELETE_FIRST_NODE and TRAVERSEoperations in doubly linked lis

Question

Write JAVA functions to implement DELETE_FIRST_NODE and TRAVERSEoperations in doubly linked lis

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here are the Java functions to implement DELETE_FIRST_NODE and TRAVERSE operations in a doubly linked list.

First, let's define the Node class:

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

    // constructor to create a new node
    Node(int d) {
        data = d;
        prev = null;
        next = null;
    }
}

Now, let's define the DoublyLinkedList class:

class DoublyLinkedList {
    Node head; // head of list

    // Function to delete the first node of the Doubly Linked List
    void deleteFirstNode() {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }
        else {
            if (head != null) {
                head = head.next; // make the next node as head
                if (head != null)
                    head.prev = null; // remove the prev of new head
            }
        }
    }

    // Function to traverse the Doubly Linked List
    void traverse() {
        Node node = head;
        if (node == null) {
            System.out.println("List is empty");
            return;
        }
        while (node != null) {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
}

You can use these functions in your main method like this:

public class Main {
    public static void main(String[] args) {
        DoublyLinkedList dll = new DoublyLinkedList();
        dll.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);

        dll.head.next = second;
        second.prev = dll.head;
        second.next = third;
        third.prev = second;

        dll.traverse(); // prints: 1 2 3
        dll.deleteFirstNode();
        dll.traverse(); // prints: 2 3
    }
}

This will create a doubly linked list with three nodes, traverse the list, delete the first node, and then traverse the list again to show the updated list.

This problem has been solved

Similar Questions

Write a Java function that delete odd nodes from the doubly linked list.

Given a doubly Linked list and a position. The task is to delete a node from a given position (position starts from 1) in a doubly linked list and return the head of the doubly Linked list.

How is the deletion of a node at the end of a Doubly Linked List different from a Singly Linked List?a)It does not require any pointer update.b)It requires traversing the entire list.c)It involves updating two pointers.d)It requires finding the middle of the list.

What is the correct sequence of steps to delete a node at the head of a linked list? Question 27Select one: 1. Update the head pointer to point to the next node, 2. Free the memory allocated to the head node, 3. Return the updated linked list 1. Update the next pointer of the head node to null, 2. Free the memory allocated to the head node, 3. Update the head pointer to point to the next node 1. Free the memory allocated to the head node, 2. Update the head pointer to point to the next node, 3. Return the updated linked list

What happens when you try to delete a node in a doubly linked list with only one node?The list becomes empty.The list becomes corrupted.The deletion fails.An error is thrown.

1/3

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.