Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java, for a doubly linked list, how can I use this search to remove a certain

ID: 3805193 • Letter: I

Question

In java, for a doubly linked list, how can I use this search to remove a certain node

   public String search(String key) {

       Node current = head;

       while (current.element != key) {

           current = current.next;

           if (current == null)

               return "Not found!";

               if (current == head)

                   head = current.next;

               else

                   current.prev.next = current.next;

           if (current == tail)

               tail = current.prev;

           else

               current.next.prev = current.prev;

       }

       return "Found!";

   }

Create a doubly linked list whose nodes contain Strings. Your list should include the following methods Insert a node in the list in alphabetical order Find a node that matches a string Traverse the list forwards and print Traverse the list backwards and print Delete a node from the list Delete/destroy the list

Explanation / Answer

HI, Please find my implementation.

Please let me know in case of any issue.

public String search(String key) {

// if list is empty
if(head == null)
   return "Not found!";

// if first element has value equal to key
if(head.element.equals(key)){
   head = head.next; // setting up new head
   if(head != null)
       head.prev = null;
   return "Found!";
}

       Node current = head;
while (current.next != null) {

   if(current.next.element.equals(key)){

       current.next = current.next.next; // deleting current's next node

       if(current.next != null)
           current.next.prev = current;

       return "Found!";
   }
  
current = current.next;
}

return "Not Found";

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote