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.
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 {...
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 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;...
The class CourseList implements a simple database of students registered in a course. Complete the method...
The class CourseList implements a simple database of students registered in a course. Complete the method countAttendees in the code below such that it returns the number of students in the instance of CourseList who have not attended any classes. Your response may be written in Java or pseudocode. Your code cannot modify the linked list. The class Node is similar to the linked list node object discussed in lecture. You can use helper methods, but this is not necessary....
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting...
Add a method to RedBlackBST to print a RB-BST indexing the subnodes. Test the tree inserting element by element and printing the results with (1) S E A R C H E X A M P L E (where the value is the index of the letter in the initial word) and (2) K R I S T E N public class RedBlackBST<Key extends Comparable<Key>, Value> { private Node root; private class Node // BST node with color bit (see...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
An array is sorted (in ascending order) if each element of the array is less than...
An array is sorted (in ascending order) if each element of the array is less than or equal to the next element. An array of size 0 or 1 is sorted Compare the first two elements of the array; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean-valued method named isSorted that accepts an integer array, and the number of elements in the array...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT