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; }

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

1 Rating

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

Java

Related Computer Science Q&A

Find answers to questions asked by students like you.

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: Event Listeners Go to the co_credit.js file in your editor. Create an event listener for the window ...

A: Actually, JavaScript is a programming language for the web.

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

A: Click to see the answer

Q: Prepare a "Functionality Requirement Analysis" document which informs technical people about require...

A: Solution: Requirement Analysis can be stated as the process of  defining user expectations to build ...

Q: Create a CourseException class that extends Exception and whose constructor receives a String that h...

A: public class Course {private String department;private int courseNumber;private float credits;public...

Q: CSC 235.S70 (Fall 2020) Homework #4 Due: 12/10/20) (Given 11/24/20 Following is a data description o...

A: Actually, database is a used to stores the data.

Q: different types of testing to be performed for on online application Load testing Capacity testing ...

A: Load testing is a type of non-functional testing. A load test is type of software testing which is c...

Q: What is hot potato routing?

A: Hot potato routing Hot potato routing is a routing technique which is used to enable packet routing...

Q: Convert the following C++ programs into Pep/9 assembly 1) #include Using namespace std; void minimu...

A: Given: #include <iostream> using namespace std;  void minimum (int i1, int i2)  {          if ...

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: 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: Using for loop, write a program that accepts an integer from the user between 100 and 1000 and check...

A: Iam giving the Code in C language as you did not mention: #include <stdio.h>int main(){   ...

Q: Make a table comparing all network topologies

A: Actually, network is a all devices communicate with other.

Q: Recall the airplane puzzle from the lab: the first person to get on the plane picks a random seat,  ...

A: Actually, given information is: Enter your answer as a decimal number  with at most three digits aft...

Q: Draw the diagrams of given addressing modes. Show it with examples also. 1- Indexed Addressing a. Po...

A: Indexed Addressing Mode:

Q: In Python:  So this is part of a large project I am working on but I'm having a hard time finding ou...

A: The ask is to build the study hours application by doing the following: - read the student details f...

Q: 4- Please I want solution of this question urgently. Thanks

A: Code: #include <stdio.h>   int main() {     int x,y,z;//declaring variables x,y and z     prin...

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: Homework #4 Due: 12/10/20) (Given 11/24/20 Following is a data description of a library: The library...

A: Actually, database is used to stores the data.

Q: Need code in C++. I have answer till part 7,please go through the code and correct it if you find an...

A: A class is a user defined blueprint or prototype from which objects are created.  It represents the ...

Q: create the program for a game that simulates rolling two dice by generating two random numbers betwe...

A: Open flowgorithm. Click on the line in between Main and End. Select Output. Double click on Output. ...

Q: Translate the following code into pep9 assembly language and show the screenshot of pep9 code. #inc...

A: This is the  lowest level of computing (machine language). It  has seven registers and four status b...

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: A magic square is a square array of non-negative integers where the sums of the numbers on each row,...

A: def isMagicSquare(array): """ Python function that tests whether given array is a magic sq...

Q: Suppose that your company is trying to decide whether it should buy special equipment to prepare hig...

A: Solution is in Step 2.

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

A: Geometry classpublic static class Geometry {    public static double areaOfCircle(double radius) {  ...

Q: Write a modular program that analyzes a year’s worth of rainfall data. In addition to main, the prog...

A: Note: Programming language is missing in the question. So we will answer this program in C++. If you...

Q: List 5 application examples of Land Registration using blockchain technology For each example provid...

A: Actually, recently used technology is blockchain technology.

Q: For each design pattern, please cover: 1) The short description of the pattern. 2) When do we use th...

A: Factory Design Pattern In the factory design pattern, the objects are created without showing the cr...

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: The following properties are required to solve the problems in the given scenario: Atomicity, Consis...

Q: Make an application that displays a series of at least five student ID numbers (that you have stored...

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

Q: Explain the benefit of hierarchical routing in terms of scale and administrative autonomy.

A: Scale - As the number of routers becomes large, the overhead involved in computing, storing, and com...

Q: using simple loop    Write a c++ program that takes 10 character type input from user. In the end, y...

A: #include<iostream> using namespace std; void Count(string str) { int upper = 0, lower = 0, ...