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