d a void instance method replaceAll(E oldE, E newE) in the MyLinkedList class to replace the occurrence of all oldE value with the specified newE value. Implement this method in O(n) time. import java.util.*; public class PExamCh24 { public static void main(String[] args) {     new PExamCh24(); } public PExamCh24() {     // Create a list for strings     MyLinkedList list = new MyLinkedList<>();     // Add elements to the list     list.add("America"); // Add America to the list     System.out.println("(1) " + list);     list.add(0, "Japan"); // Add Canada to the beginning of the list     System.out.println("(2) " + list);     list.add("Russia"); // Add Russia to the end of the list     System.out.println("(3) " + list);     list.addLast("Japan"); // Add France to the end of the list     System.out.println("(4) " + list);     list.add(2, "Japan"); // Add Germany to the list at index 2     System.out.println("(5) " + list);     list.add(5, "Norway"); // Add Norway to the list at index 5     System.out.println("(6) " + list);     list.add(0, "Japan"); // Same as list.addFirst("Poland")     System.out.println("(7) " + list);     // Remove elements from the list     list.remove(0); // Same as list.remove("Australia") in this case     System.out.println("(8) " + list);     list.remove(2); // Remove the element at index 2     System.out.println("(9) " + list);     list.remove(list.size() - 1); // Remove the last element     System.out.print("(10) " + list + "\n(11) ");     list.replaceAll("Japan", "England");     for (String s : list) {       System.out.print(s.toUpperCase() + " ");     } } class MyLinkedList implements MyList {     public void replaceAll(E oldE, E newE) {       // WRITE YOUR CODE HERE     }     protected Node head, tail;     protected int size = 0; // Number of elements in the list     /**      * Create an empty list      */     public MyLinkedList() {     }     /**      * Create a list from an array of objects      */     public MyLinkedList(E[] objects) {       for (int i = 0; i < objects.length; i++) {         add(objects[i]);       }     }     /**      * Return the head element in the list      */     public E getFirst() {       if (size == 0) {         return null;       } else {         return head.element;       }     }     /**      * Return the last element in the list      */     public E getLast() {       if (size == 0) {         return null;       } else {         return tail.element;       }     }

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

Add a void instance method replaceAll(E oldE, E newE) in the MyLinkedList class to replace the occurrence of all oldE value with the specified newE value. Implement this method in O(n) time.

import java.util.*;

public class PExamCh24 {

public static void main(String[] args) {
    new PExamCh24();
}

public PExamCh24() {
    // Create a list for strings
    MyLinkedList<String> list = new MyLinkedList<>();

    // Add elements to the list
    list.add("America"); // Add America to the list
    System.out.println("(1) " + list);

    list.add(0, "Japan"); // Add Canada to the beginning of the list
    System.out.println("(2) " + list);

    list.add("Russia"); // Add Russia to the end of the list
    System.out.println("(3) " + list);

    list.addLast("Japan"); // Add France to the end of the list
    System.out.println("(4) " + list);

    list.add(2, "Japan"); // Add Germany to the list at index 2
    System.out.println("(5) " + list);

    list.add(5, "Norway"); // Add Norway to the list at index 5
    System.out.println("(6) " + list);

    list.add(0, "Japan"); // Same as list.addFirst("Poland")
    System.out.println("(7) " + list);

    // Remove elements from the list
    list.remove(0); // Same as list.remove("Australia") in this case
    System.out.println("(8) " + list);

    list.remove(2); // Remove the element at index 2
    System.out.println("(9) " + list);

    list.remove(list.size() - 1); // Remove the last element
    System.out.print("(10) " + list + "\n(11) ");

    list.replaceAll("Japan", "England");

    for (String s : list) {
      System.out.print(s.toUpperCase() + " ");
    }
}

class MyLinkedList<E> implements MyList<E> {
    public void replaceAll(E oldE, E newE) {
      // WRITE YOUR CODE HERE
    }

    protected Node<E> head, tail;
    protected int size = 0; // Number of elements in the list

    /**
     * Create an empty list
     */
    public MyLinkedList() {
    }

    /**
     * Create a list from an array of objects
     */
    public MyLinkedList(E[] objects) {
      for (int i = 0; i < objects.length; i++) {
        add(objects[i]);
      }
    }

    /**
     * Return the head element in the list
     */
    public E getFirst() {
      if (size == 0) {
        return null;
      } else {
        return head.element;
      }
    }

    /**
     * Return the last element in the list
     */
    public E getLast() {
      if (size == 0) {
        return null;
      } else {
        return tail.element;
      }
    }

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