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

  Create the mouse program

Question 1: Create the Mouse program. Test it with interesting data. Question 2: Modify the MouseDriver program you just created in Question #1. Currently, it uses the same value of growthRate for both mice. Change the program so it accepts a d..

  Develop a set of classes for a college

Person: A Person contains a first name, last name, street address, zip code, and phone number. The class also includes a method that sets each data field and display method that displays all of a Person's information.

  Implement the jvm dload instruction for the mic-two

Implement the JVM DLOAD instruction for the Mic-2. It has a 1-byte index and pushes the local variable at this position onto the stack. Then it pushes the next higher word onto the stack as well.

  Create a new variable according to the number

Create a new variable according to the number of inputted in the console - Since each of these values have to be stored

  Writing an applet with graphics in this programming

writing an applet with graphics in this programming activity you will write an applet that uses graphics. you will draw

  Write a java gui application called indexjava

Write a Java GUI application called Index.java that inputs several lines of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text.

  Write additional code that conforms to this outline

It combines several topics and skills that a student of CSE 214 is already expected to have mastered, including arrays, basic OOP concepts in Java

  Write specifications for method which advances date by one

Write specifications for the method which advances any given date by one day. Comprise a statement of purpose, pre- and post-conditions, and description of parameters.

  Create a third string consisting of the first half of string

Show the length of both strings - Create a third string consisting of the first half of string (1) and the second half of string.

  Write a java class which implements methods

write a Java class which implements methods to perform symbolic computations on fractions. The input to your program will be 2 arrays which you will transform

  Java program that iterates through the integers

Write a Java program that iterates through the integers from 1 to 20, computing the square of each number and writing the information to a file called squares.txt.

  Create a sub-method to read the grades

Create a sub-method to read the grades, store the grades in a two-dimensional array (grade book), and then return the grade book to the caller

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