public class Link {
    public long dData;//date item
    public Link next;
    public Link(long dd){
        dData = dd;
    }
    public void displayLink(){
        System.out.print( dData + " ");
    }
}

public class SortedList {
    private Link first;
    public SortedList(){
        first=null;
    }
    public boolean isEmpty(){
        return first==null;
    }
    public void insert(long key){
        Link link = new Link(key);
        Link previous=null;
        Link current=first;
        while(current!=null&&key>current.dData){
            previous=current;
            current=current.next;
        }
        if(previous==null){
            first=link;
        }
        else{
            previous.next=link;
        }
        link.next=current;
    }
    public Link remove(){
        Link temp=first;
        first=first.next;
        return temp;
    }
    public void displayList(){
        System.out.print("List(first-->last):");
        Link current = first;
        while(current!=null){
            current.displayLink();
            current=current.next;
        }
        System.out.println("");
    }
}

public class SortedListApp {
    public static void main(String[] args) {
        SortedList list = new SortedList();
        list.insert(20);
        list.insert(15);
        list.insert(30);
        list.insert(25);
        list.insert(10);

        list.displayList();
        list.remove();
        list.displayList();
    }
}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐