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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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 Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education