The Sorted List ADT Implement the SortedList class The Sorte
The Sorted List ADT
Implement the SortedList class. The SortedList 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
SortedList class. You must submit a modified SortedList.java
file with your source code. Do not submit and do not modify
the List.java file.
1st 3 attachments: the given code
List.java public abstract class List implements Iterable protected class NodT protected Node (T data) ( this.datadata; 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 NodeE> head; SortedList.javia public class SortedListcE extends ComparableSolution
Please check below code. I could not implement a Iterator but other things are done.
Please comment if you face any problems
Thanks
Programe :-
-----------------------------
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
public class SortedList<E> extends List<E>{
//used for recursion traversal
private Node<E> current;
private int index = 0;
public void forEach(Consumer<? super E> arg0) {
// TODO Auto-generated method stub
}
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return null;
}
public Spliterator<E> spliterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public void insert(E data) {
if(head == null)
{
head = new Node<E>(data);
head.next = null;
}
else{
if(current == null){
current = head;
}
else if(current.next == null){
current.next = new Node<E>(data);
current.next.next = null;
current = null;
return;
}
else{
current = current.next;
}
//Call recursive insert
insert(data);
}
}
@Override
public void remove(E data) {
//If head is null , so list is not created yet so do nothing
if(head == null)
{
return;
}
if(current == null){
//Remove if given key found at head element
if(head.data == data){
head = null;
return;
}
current = head;
}
//If element is not found in list
else if(current.next == null){
current = null;
return;
}
//IF element found in next element of current , so remove current.next
else if(current.next.data == data){
current.next = current.next.next;
current = null;
return;
}
//else call remove recursively
remove(data);
}
@Override
public E retrieve(int index) {
//If head is null , so list is not created yet so do nothing
if(head == null)
{
return null;
}
index--;
if(index == 0){
if(current == null){
return null;
}
else{
E tempdata = current.data;
current = null;
return tempdata;
}
}
else if(current == null){
current = head;
}
else if(current.next == null){
current = null;
return null;
}
else{
current = current.next;
}
return retrieve( index);
}
@Override
public boolean search(E data) {
//If head is null , so list is not created yet so do nothing
if(head == null)
{
return false;
}
if(current == null){
//Remove if given key found at head element
if(head.data == data){
return true;
}
current = head;
}
//If element is not found in list
else if(current.next == null){
current = null;
return false;
}
//IF element found in next element of current , so remove current.next
else if(current.next.data == data){
current = null;
return true;
}
//else call remove recursively
return search(data);
}
}