Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Linkedlist插入法_Java - Fatal编程技术网

Java Linkedlist插入法

Java Linkedlist插入法,java,Java,这是我们的第一堂课AbstractLinkedList 当我运行此代码时,example.reverse应使其反向并返回LinkedList,而不更改原始LinkedList。 但是它的返回没有反转你不能使用你的JDK支持泛型吗?head.value.compareTo(value)

这是我们的第一堂课
AbstractLinkedList

当我运行此代码时,
example.reverse
应使其反向并返回LinkedList,而不更改原始LinkedList。
但是它的返回没有反转

你不能使用
你的JDK支持泛型吗?
head.value.compareTo(value)<0
而不是
head.value
。我们无法实现可比性这是我们要做的作业和教学compare@Orbay那么,如何为
T
类型的对象定义顺序?一个
T
小于另一个意味着什么?我们给了两个类第一个是我将编辑我的提问,你可以在那里看到。我们需要第二个。你必须实施哪一个
Odev1LinkedList
?我们实现了
AbstractLinkedList
public abstract class AbstractLinkedList<T> {
    public class Node<T> {
        public T value;
        Node<T> next;

        public Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }
    }
    Node<T> head;

    public Node<T> getHead() {
        return head;
    }
    public void addFirst(T value) {
        head = new Node<>(value, head);
    }
    public void addLast(T value){
        if(head==null)
            head = new Node<>(value, null);
        else {
            Node<T> node = head;
            while (node.next!=null)
                node = node.next;
            node.next = new Node<>(value, null);
        }

    }
    public void print(){
        System.out.println(toString());
    }
    @Override
    public String toString(){
        if(head==null) return "boş";
        String r="";
        Node<T> node=head;
        while(node!=null) {
            r += node.value + (node.next!=null?" ":"");
            node=node.next;
        }
        return r;
    }

    public abstract void insertInOrder(T value);
    public abstract AbstractLinkedList<T> reverse();
    public abstract AbstractLinkedList<T> concatenate(AbstractLinkedList<T> list);
}
public class Odev1LinkedList<T extends Comparable<T>> extends AbstractLinkedList<T> {

    @Override
    public AbstractLinkedList<T> reverse() {

        Odev1LinkedList a = new Odev1LinkedList();
        Node<T> root = new Node(this, head);
        Node<T> iter = root;
        Node<T> temp ;
        a.head = null;

        do {            


        temp = iter;

        a.addFirst(temp.value);


        iter = iter.next;


        }while (temp !=root );

        return a;
    }
public class MyLinkedList<T extends Comparable<T>> { ... }