跪求各路帅哥用迭代写一个sortedlist ADT代码
小弟初学者,作业快要交了,实在不会写啊求代码,最好是完整啦~谢谢题目如下:
The Sorted List ADT
Implement the SortedList class. The SortedList class extends
the AbstractList class. Both can be seen here:
/*
*
* 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> {
}
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.
--------------------编程问答-------------------- 这个是非迭代的代码,求迭代版的啊!
public class Main {
public static void main(String[] args) {
List<Integer> list = new List<Integer>();
for (int i = 0; i < 10; ++i) {
list.insert(i);
}
System.out.println("traverse");
list.traverse();
System.out.println("backwards");
list.writeBackwards();
}
}
class List<E> {
private class Node<T> {
private Node(T data) {
this.data = data;
}
private T data;
private Node<T> next;
}
public void insert(E data) {
Node<E> temp = new Node<E>(data);
temp.next = head;
head = temp;
}
public void traverse() {
traverse(head);
}
public void writeBackwards() {
writeBackwards(head);
}
private void writeBackwards(Node<E> curr) {
if (curr != null) {
writeBackwards(curr.next);
System.out.println(curr.data);
}
}
private void traverse(Node<E> curr) {
if (curr != null) {
System.out.println(curr.data);
traverse(curr.next);
}
}
private Node<E> head;
}
--------------------编程问答-------------------- 哎,不行啊啊啊啊 --------------------编程问答-------------------- 呦呦切克闹啊 --------------------编程问答--------------------
挺有意思的,作业还是自己写比较好
补充:Java , Java SE