Implement the sortedlist class the sortedlist class extends


The description and requirement is below. I also upload my code here. But my problem is, I do not know how to do a main class for this. So it does not run. Please help me to do a main class so that it can compile. And please help me to check my code. Thank you very much!

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

public interface List extends Iterable {
void insert(E data);
void remove(E key);
E retrieve(int index);
boolean search(E key);
}

/*
*
* AbstractList.java
*
*/

public abstract class AbstractList implements List {

protected class Node {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node next;
}

protected Node head;
}

/*
*
* SortedList.java
*
*/

public class SortedList> extends AbstractList {

}

My written code:
import java.util.Iterator;

public class SortedList> extends AbstractList {

private int size;
Node curr = head;

Node prev = null;

Node temp;
public SortedList() {
head = null;
size=0;
}

//Insert

public void insert(E data) {
if(head == null){
head = new Node(data);
size++;
}
else{
Node curr = head;
Node prev = null;
Node temp = null;
while(curr.next != null && data.compareTo(curr.next.data) >= 0){
prev = curr;
curr = curr.next;
}
if (curr.next != null) {
temp.next = curr;
prev.next = temp;
}
else {
temp.next = head;
head = temp;

}
if (head == null) {
head.data = (E) temp;
head.next = null;
}
curr.next = new Node(data);
size++;
}
}
//Remove
public void remove(E data) {
if(head == null){
head = new Node(data);
size++;
}
else {
Node curr = head;
Node prev = null;
Node temp = null;
while(curr.next != null){
prev = curr;
curr = curr.next;
}
if (curr != null && temp == curr.data) {
prev.next = curr.next;
}
if (prev !=null){
prev.next = curr.next;
}
else{
head = curr.next;
}
if (head == null) {
head.data = (E) temp;
head.next = null;
}
curr.next = new Node(data);
size++;

}
}
//Retrieve
public E retrieve(int index){
int i = 0;

while(curr.next != null && i !=index){

curr = curr.next;
}
if (i == index){
return curr.data;
}
else {
return null;

}
}
//Search
public boolean search(E data){
for(int i = 1; i if(curr.next == null) {
return false;
}
else {
return true;
}
}
return true;
}
//Iterator
public Iterator iterator(){
return new Iterator(){

Node curr=head;

public void remove() {
throw new UnsupportedOperationException();
}


public boolean hasNext() {
return (curr.next != null);
}


public E next() {
E temp = (E) curr.data;
curr = curr.next;
return temp;

}
};

 


}
//Source List
public interface List extends Iterable {
void insert(E data);
void remove(E key);
E retrieve(int index);
boolean search(E key);
}
//Source AbstratList
public abstract class AbstractList implements List {

protected class Node {

protected Node(T data) {
this.data = data;
}

protected T data;
protected Node next;
}

protected Node head;
}

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Implement the sortedlist class the sortedlist class extends
Reference No:- TGS02387574

Now Priced at $10 (50% Discount)

Recommended (92%)

Rated (4.4/5)