为什么我的双链接插入算法不起作用?

时间:2014-04-22 01:59:47

标签: data-structures linked-list

我有双链接插入算法的任务,我花了很多时间去做但仍然失败,sortLink方法仍然无法工作,有人可以帮我解决吗?代码是列出如下:

  • Link.java
  public class Link {

    private int value;
    private Link next;
    private Link prev;

    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public Link getNext() {
        return next;
    }
    public void setNext(Link next) {
        this.next = next;
    }
    public Link getPrev() {
        return prev;
    }
    public void setPrev(Link prev) {
        this.prev = prev;
    }
}
  • Test.java
public class Test {

    public static void main(String[] args) {
        testLink();
    }


    public static void sortLink(Link headLink){

        Link curLink=null,preLink=null,nextLink=null,link=null,pLink=null;

        curLink=headLink.getNext();
        headLink.setNext(null);
        headLink.setPrev(null);

        while(curLink!=null){

            pLink=curLink;

            link=curLink.getNext();


            if(curLink.getValue()<headLink.getValue()){
                curLink.setNext(headLink);
                curLink.setPrev(headLink.getPrev());
                headLink=curLink;
            }else{

                preLink=headLink;
                nextLink=headLink.getNext();

                while(nextLink!=null&&nextLink.getValue()<curLink.getValue()){
                    preLink=nextLink;
                    nextLink=nextLink.getNext();
                    curLink.setPrev(pLink);
                }
                curLink.setNext(preLink.getNext());

                preLink.setNext(curLink);
            }
            curLink=link;
        }

        showFE(headLink);
        //showEF(curLink);
    }

    public static void testLink(){
        Link link1=new Link();
        link1.setValue(7);
        Link link2=new Link();
        link2.setValue(3);
        Link link3=new Link();
        link3.setValue(1);
        Link link4=new Link();
        link4.setValue(2);
        Link link5=new Link();
        link5.setValue(9);
        Link link6=new Link();
        link6.setValue(6);
        Link link7=new Link();
        link7.setValue(4);
        Link link8=new Link();
        link8.setValue(8);
        Link link9=new Link();
        link9.setValue(5);

        link1.setNext(link2);
        link1.setPrev(null);

        link2.setNext(link3);
        link2.setPrev(link1);

        link3.setNext(link4);
        link3.setPrev(link2);

        link4.setNext(link5);
        link4.setPrev(link3);

        link5.setNext(link6);
        link5.setPrev(link4);

        link6.setNext(link7);
        link6.setPrev(link5);

        link7.setNext(link8);
        link7.setPrev(link6);

        link8.setNext(link9);
        link8.setPrev(link7);

        link9.setPrev(link8);
        link9.setNext(null);

        showFE(link1);

        //showEF(link9);

        sortLink(link1);
    }


    //show node from front to end
    public static void showFE(Link link){
        while(link!=null){
            System.out.print(link.getValue()+"-->");
            link=link.getNext();
        }
        System.out.println();
    }

    //从尾至头显示链表
//  public static void showEF(Link link){
//      while(link!=null){
//          System.out.print(link.getValue()+"<--");
//          link=link.getPrev();
//      }
//      System.out.println();
//  }
}

主要方法是sortLink,我试过但我无法正确运行此程序,

它可以按顺序输出结果,但是每个链接之间的关系不正确,所以如果我们要插入或删除一些链接,或者从头到尾显示链接,那就错了。

有人可以帮我纠正插入算法吗?

0 个答案:

没有答案