Implement the sortedlist class

Assignment Help JAVA Programming
Reference no: EM131592032

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<E> extends Iterable<E> {
void insert(E data);
void remove(E key);
E retrieve(int index);
boolean search(E key);
}

/*
*
* AbstractList.java
*
*/

public abstract class AbstractList<E> implements List<E> {

protected class Node<T> {

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

protected T data;
protected Node<T> next;
}

protected Node<E> head;
}

/*
*
* SortedList.java
*
*/

public class SortedList<E extends Comparable<? super E>> extends AbstractList<E> {

}

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;
}

}

Reference no: EM131592032

Questions Cloud

How can you quickly calculate years worked : Discuss the YEARFRAC function. How can you quickly calculate years worked for the rest of the employees without entering the formula row by row?
Installment-sales-default and repossession : Seaver Company uses the installment-sales method in accounting for its installment sales. On January 1, 2014, Seaver Company had an installment.
Translate the research results into everyday terms : Discuss research findings on cultural differences in the expression of emotion, interpretation of behavior, and traditions.
Review the given case related to pacific crossburgers inc : (Franchise Entries) Pacific Crossburgers Inc. charges an initial franchise fee of $70,000. Upon the signing of the agreement, a payment of $28,000 is due.
Implement the sortedlist class : Implement the SortedList class. The SortedList class extends the AbstractList class. Both can be seen here. Your assignment is to implement
Research in gathering information : In the article some of the focus is on the issue that management might use research in gathering information and that research discovery is a valid effort.
How will you engage them in this process : Who are the people you will need to sell on the benefits of this service in order to gain access to your clients?
Find an example online of two difficult business decisions : ssume that you have the following decision-making options: consult others before making a decision, find an example online of two difficult business decisions
What topical area might you present to a group : Discuss this topical area, some of the challenges in presenting this information, and the outline of what you might actually present.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd