Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please do this in Java and make sure that the code works properly. The Main.java file should not be modified and can be seen below

Please do this in Java and make sure that the code works properly.

image text in transcribed

The Main.java file should not be modified and can be seen below

import java.util.Iterator; import java.util.Random; public class Main { public static void main(String[] args) { List list = new SortedList(); Random rand = new Random(1); int n, num = args.length == 1 ? Integer.parseInt(args[0]) : 11; long start, stop; System.out.println("insert"); for (int i = 0; i "); for (Integer j : list) { System.out.print(j + " "); } System.out.println(); } rand = new Random(1); System.out.println("remove"); for (int i = 0; i "); for (Integer j : list) { System.out.print(j + " "); } System.out.println(); } } } abstract class List implements Iterable { protected class Node { protected Node(T data) { this.data = data; } protected T data; protected Node next; } public abstract void insert(E data); public abstract void remove(E data); public abstract E retrieve(int index); public abstract boolean search(E data); protected Node head; }

class SortedList> extends List { public void insert(E data) { Node temp = new Node(data); if (head == null || data.compareTo(head.data) curr = head; while (curr.next != null && data.compareTo(curr.next.data) > 0) { curr = curr.next; } temp.next = curr.next; curr.next = temp; } } public Iterator iterator() { return new Iterator() { public boolean hasNext() { return curr != null; } public E next() { E temp = curr.data; curr = curr.next; return temp; } private Node curr = head; }; } public void remove(E data) { if (head != null) { if (data.compareTo(head.data) == 0) { head = head.next; } else { for (Node curr = head; curr.next != null; curr = curr.next) { if (data.compareTo(curr.next.data) == 0) { curr.next = curr.next.next; break; } } } } } public E retrieve(int index) { Node curr = head; int i = 0;

while (i curr = head; curr != null; curr = curr.next) { if (data.compareTo(curr.data) == 0) { return true; } } return false; } }

List.java is below and should NOT be modified.

/* * * List.java * */ public abstract class List implements Iterable { protected class Node { protected Node(T data) { this.data = data; } protected T data; protected Node next; } public abstract void insert(E data); public abstract void remove(E data); public abstract E retrieve(int index); public abstract boolean search(E data); protected Node head; }

SortedList.java is the file you will edit. Make it perfect. the code should run perfectly with the other classes not modified.

/* * * SortedList.java * */ public class SortedList> extends List { }
The Sorted List ADT Implement the Sorted List class. The Sorted List class extends the List class. Both can be seen here. Your assignment is to implement (recursively) all of the abstract methods of the List class. They are: insert (recursive) iterator remove (recursive) retrieve (recursive) search (recursive) You must also implement an Iterator inner class for the Sorted List class. You must submit a modified Sorted List.java file with your source code. Do not submit and do not modify the List.java file or the Main.java file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions