Question

To class DoublyLinkedList, add method search(E e) that receives an element e and returns the node...

To class DoublyLinkedList, add method search(E e) that receives an element e and returns the node number that has the element (assume first node is number 1), if there is no node has that element , it returns -1

java language

Homework Answers

Answer #1

Steps to make a Doubly Linked List:

1. First we will define a class node which will consist of a node and two pointers that is next and previous.

2.Once our class is made we can insert any number of nodes, this inserting can easily be done at previous, next of a certain position and in a normal straight pattern too.

3.As Doubly Linked List can traverse in both the forward and backward directions, many operations like insertion, deletion though the search operation takes place in the same time for both the list.

4. As a disadvantage, here each node has a heigher amount of memory space.

Algorithm of Search Operation:

Brute Force :

1. Taken a bool variable f which will currently be false.

2. A pointer Current will initially point to head.

3. Iterate through each element, and check if the node data is equal to the required element.

4.If yes then make f = true.

5. If f is true, return the node number else return -1.

Code:

public class SearchList {
  
    class Node{

        // three pointers for the doubly linked list

        int data;
        Node previous;
        Node next;

        public Node(int data) {
            this.data = data;
        }
    }

    //Initialise the head and tail of doubly linked list to be formed
    Node head, tail = null;

    //This function will add a node to the list
    public void add_data(int data) {
        Node newNode = new Node(data);

        if(head == null) {

            head = tail = newNode;
            head.previous = null;
            tail.next = null;
        }
        else {
            tail.next = newNode;
            newNode.previous = tail;
            tail = newNode;
            tail.next = null;
        }
    }

    //searchNode() will search a given node in the list
    public void searchNode(int key) {

        //starting with the 1st node
        int i = 1;
        boolean f = false;

        //current Node will point to head
        Node current = head;

        if(head == null) {
            System.out.println("List empty");
            return;
        }

        while(current != null) {

            //Compare the key each Node in the doubly linked list
            if(current.data == key) {
                f = true;
                break;
            }
            current = current.next;
            i++;
        }
        if(f)
             System.out.println("Node is present at: " + i);
        else
             System.out.println("-1");
    }

    public static void main(String[] args) {

        SearchList dBLL = new SearchList();
        dBLL.add_data(20);
        dBLL.add_data(50);
        dBLL.add_data(87);
        dBLL.add_data(64);
        dBLL.add_data(32);

        //Case when Node will be found
        dBLL.searchNode(50);

        //Case when Node will not be found
        dBLL.searchNode(2);
    }
}

NOTE: Save the file as SearchList.java.

Attaching the screenshot of code and output for the sake of reference. If any doubts can comment in the thread.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an element e and insert that in a new node after the third node in the list (assume the first node in the list is number 1). You should ensure that the linked list has at least three nodes, otherwise throw an exception. You should consider all possible cases.
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node...
public class LinkedStackOfStrings { private Node first; private class Node { private String item; private Node next; } public boolean isEmpty() { return (first == null); } public void push(String item) { // Insert a new node at the beginning of the list. Node oldFirst = first; first = new Node(); first.item = item; first.next = oldFirst; } public String pop() { // Remove the first node from the list and return item. String item = first.item; first = first.next;...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
1. Create a linked list using the Node class that contains the first million prime numbers...
1. Create a linked list using the Node class that contains the first million prime numbers (primes4.txt). It should have a menu with these options: 1 = Search for a Number (let the user enter a number to search for) 2 = Add a new Number (let the user enter a number and add it to the head of the list) 3 = Delete a Number (let the user enter a number and delete it if found) 4 = Exit...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
public class LinkedList {       private Node list;       public LinkedList()       {           list =...
public class LinkedList {       private Node list;       public LinkedList()       {           list = null;       }       public Node getList()       {           return list;       }       . . .//other methods       // insert method printHighEarners here       // insert method lowestPaidEmployeeRec       private class Node       {           public Employee data;           public Node next;           public Node(Employee emp)           {               data = emp;               next = null;           }           public String toString()...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...