In Java: Modify the attached program code below According to the question a, b and c a. Replace the appendNode() method by an insertNode() method which inserts the new node in such a way to keep the list always sorted in increasing order. b. Add a recursive method displayReverse() which displays the list in reverse order. c. Do the needed changes to the main()in order to reflect the above two changes.

Question

In Java: Modify the attached program code below According to the question a, b and c

a. Replace the appendNode() method by an insertNode() method which inserts the new node in such a way to keep the list always sorted in increasing order.

b. Add a recursive method displayReverse() which displays the list in reverse order.

c. Do the needed changes to the main()in order to reflect the above two changes.

public class DoublyLinkedList {
  
private Node head;
private Node tail;
private int size;
  
DoublyLinkedList() {
tail = head = null;
size = 0;
}

public void addNode(String item) {
//adding a node at the end
Node newNode = new Node(item);
if(head == null) {
head = tail = newNode;
}
else {
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
}
size++;
}
  
public boolean remove(String item) {
Node current = head;
boolean found = false;
while((current != null)&&(!found)) {
if(current.element.equals(item))
found = true;
else
current = current.next;
}
if(found)
{
if(current == head) head = head.next;
if(current == tail) tail = tail.prev;
if(current.prev != null)
current.prev.next = current.next;
if(current.next != null)
current.next.prev = current.prev;
size--;
}
return found;
}
  
public boolean isEmpty() {
return size == 0;
}

public void display() {
if(head == null)
{
System.out.println("\nThe list is empty!");
return;
}
System.out.println();
Node current = head;
while(current != null) {
System.out.println(current.element);
current = current.next;
}
}
  
public void clearList() {
head = tail = null;
size = 0;
}
}

import java.util.Scanner;

public class LinkedListsExample {

public static void main(String[] args) {
// prompt user repeatedly to select an action (add, remove, display)
DoublyLinkedList list = new DoublyLinkedList();
Scanner in = new Scanner(System.in);
String choice;
String element;
  
do {
System.out.println("\nSelect one of the following actions:\n");
System.out.println("D: Display list");
System.out.println("A: Append an element to the list");
System.out.println("R: Remove an element from the list");
System.out.println("C: clear the list");
System.out.println("Q: Quit");
System.out.print("\nEnter your choice: ");
choice = in.nextLine();
if(choice.equalsIgnoreCase("D"))
list.display();
else if(choice.equalsIgnoreCase("A"))
{
System.out.print("\nEnter a string to append to the list: ");
element = in.nextLine();
list.addNode(element);
}
else if(choice.equalsIgnoreCase("R"))
{
System.out.print("\nEnter a string to remove from the list: ");
element = in.nextLine();
if(!list.remove(element))
System.out.println("\nThe string " + element + " was not found!");
}
else if(choice.equalsIgnoreCase("C"))
list.clearList();
else if(choice.equalsIgnoreCase("Q"))
return;
else
System.out.println("\nInvalid choice. Try again!");
} while(true);
}
  
}

public class Node {
  
String element;
Node prev;
Node next;
  
public Node(String newElement) {
element = newElement;
prev = next = null;
}
}

Expert Answer

Want to see the step-by-step answer?

Check out a sample Q&A here.

Want to see this answer and more?

Experts are waiting 24/7 to provide step-by-step solutions in as fast as 30 minutes!*

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Tagged in
Engineering
Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by students like you.

Q: Write a program that prompts the user to enter 50 integers and stores them in an array. The program ...

A: Program Plan:- 1. Initialise all the required header files. 2. declare the array of 50 numbers. 3. a...

Q: Information Security For a sniffer attack to succeed, what must the attacker do? How can an attacker...

A: Remember back in some movies, law agencies, and criminals used to bug the telephone lines in order t...

Q: Exercise 3.9Modify program P34_1.cpp to compute the side area, total area, and volume of a cylinder ...

A: The, given information is: Modify program P34_1.cpp to compute the side area, total area, and volume...

Q: Design an expanding opcode to allow all the following to be encoded in a 32-bit instruction:15 instr...

A: Format 1:

Q: Create a generic Map interface MyMap<K,V> that represents a Map structure. K is the type for a...

A: Java code for given question is solved in step 2. feel free to post follow-up questions in case you ...

Q: I* Your solution goes here */ while

A: The given is a java code under file named AutoBidder with below functionality:   1. It has a nextBid...

Q: Evaluate the following expressions given below step by step to show the explanation. e1 = (10 * Z Y...

A: The, answer has given below:

Q: Subject:Database

A: Objective: A relation R (A,B,C,D,E,F,G,H) is given. We need to convert this functional dependency up...

Q: Convert the grammar S → aSb bSa a|b into Greibach Normal Form

A: Click to see the answer

Q: A salesman has n things to sale .The cost price of all n things is different out of which p things h...

A: We have the formulas for Loss and Profit as Profit = selling price- cost price  (We are selling the ...

Q: Identify the contents of the registers, memory location (C070H), and the flags as the following inst...

A: Given: M=C070 H Now solve the problem: LXI H, 2020H : (load register pair immediately) After executi...

Q: Clustering is a side effect of linear probing. Group of answer choices True False Also explain why?

A: The, answer has given below;

Q: Evaluate the following statement: DOM is a limited interface which works only on JavaScript and HTML...

A: he HTML DOM model is constructed as a tree of Objects: The HTML DOM Tree of Objects With the obje...

Q: C Programming Language (Code With C Programming Language) Problem Title : Visible Trees There is a l...

A: #include <stdio.h>int main(){ //number of trees int N; scanf("%d",&N); //array to st...

Q: Write python code Given list: numbers = [120, 200, 100, 50, 150]   Sort the list based on how close ...

A: By default, sort() doesn't need any additional parameters. However, it has two optional parameters.T...

Q: a) Determine the correct class of the following IP addresses:Address Class?191.107.2.10 172.16.16.15...

A: (a)There are five classes, A, B, C, D, and E, in classic addressing. Each class has a number of valu...

Q: Write a user-defined function that adds or subtracts two polynomials of any order. Name the function...

A: The, given information is :  Name the function p=polyadd(p1,p2,operation). The first two input argum...

Q: Using a text editor, create a file that contains a list of at least 15 six-digit account numbers. Re...

A: Actually, java is a object oriented programming language. It is a platform independent.

Q: code is to be written in C A palindrome is a word that reads the same forward and backwards. For exa...

A: Introduction of the Program: The C program creates char type arrays that stores string and the progr...

Q: p.s: see image for question.

A: #include <iostream>using namespace std;double findslope(double x1,double x2,double y1,double y...

Q: The three-layer architecture has the primary objective of separating each user’s view of the databas...

A: Click to see the answer

Q: please provide some explanations to help me understand the topic (image).

A: Based on our rule, we should answer only the first question when multiple questions are posted. Plea...

Q: Code in c++ using switch/case statements, Im not looking for help using the website..

A: Actually, c++ is a powerful general purpose language.

Q: please answer to number 1  thank youu

A: a) f(n) = 11.2+12.3+...+1n(n+1)f(1) = 11.2 is TrueLet f(k) is truef(k) = 11.2+12.3+...+1k(k+1) =k --...

Q: I am having dificulties answering this from a developers point of view. Can you help me?

A: Solution is in Step 2.

Q: Write bash script which takes array as an input of size 10 bind its even indexes to accept even valu...

A: Actually, array is a collection of elements.

Q: Briefly state the principle following multple access protocol  a). TDMA; B).FDMA; C) Pure ALOHA d)Sl...

A: Solution:  Multiple access protocols  : They are group  of protocols  that operate in Medium Access ...

Q: Write a program in C++. Program Information You recently visited a clock shop and it started you thi...

A: Actually, c++ is a general language.

Q: Explain the Address Resolution Protocol(ARP).

A: Address Resolution Protocol: The ARP is a communication protocol used to locate the device's MAC add...

Q: Give solution in ms excel

A: Explanation: Add the necessary data in column1 to column7. In column8, use the formula “=(SUM(D2:G2...

Q: Question-3 There are n number of students in a class. Assume a teacher enters and wants to shake han...

A: Solution is in Step 2.

Q: Write the method catch22(). * * Given an array of ints, return true if the array contains a 2 next t...

A: public boolean catch22(int[] nums) {  boolean isPresent = false;    for(int i = 0; i < nums.lengt...

Q: Explain the concept of tunneling IPv6 datagrams through IPv4 routers

A: Tunneling mechanism  Tunneling mechanism is the mechanism in which all the routers between two IPv6...

Q: Please answer the question posted and give full explanation of the answer. Please past the successfu...

A: Java Programming Input a String Split it to integers Add them and display the result

Q: Find the values to be placed in TCCR1A and TCCR1B if Timer1 should be programmed to operate in CTC m...

A: As we know Timers work by incrementing a counter variable called counter register. The counter regis...

Q: The following figure shows the graph of the derivative f`of a function f, where f is continuous on t...

A: Solution : 

Q: Write microC program to 8 bits up counter using pic16f84a

A: Actually, 1 byte=8 bits.

Q: (#2) computer science: theory of computation or automata theoryplease provide (step-by-step) explana...

A: Click to see the answer