package org.chen.link;public class List {

public List pre;
public List next;
public Object data;
public List(){
pre=this;
next=this;
}

public void addHead(Object obj){
List node=new List();
node.data=obj;
node.next=____;//
this.next=node;
node.next.pre=node;
node.pre=____;//
}
public List reverse(){
List listNew=new List();
List nextNode=______;//
while(_______){//
Object obj=nextNode.data;
listNew.addHead(obj);
____________ ;//
}
return listNew;
}
}这个程序我试了好久,都没有搞定,大家能不能帮我把空补上,加注释解释一下;
最好能够加一些数据测试一下,这样有助于理解,谢谢啊!

解决方案 »

  1.   


    package org.chen.link;public class List {
        
        public List pre;
        public List next;
        public Object data;
        public List(){
            pre=this;
            next=this;
        }
        
        public void addHead(Object obj){
            List node=new List();
            node.data=obj;
            node.next=this;//双向循环,画图出来就知道,下面一个一样,不过没见过这种链表,呵呵
            this.next=node;
            node.next.pre=node;
            node.pre=this;//
        }
        public List reverse(){
            List listNew=new List();
            List nextNode=this.pre;//这个是随时想的,要停电了,你试试看
            while(nextNode!=this){//
                Object obj=nextNode.data;
                listNew.addHead(obj);
                nextNode=nextNode.pre ;//
            }
            return listNew;
        }
    }
      

  2.   

    public class List {
        
        public List pre;
        public List next;
        public Object data;
        public List(){
            pre=this;
            next=this;
        }
        
        public void addHead(Object obj){
            List node=new List();
            node.data=obj;
            node.next=this;//新增结点的后指针指向最开始的头指针
            this.next=node;
            node.next.pre=node;
            node.pre=this.pre;//将添加结点以前的头指针赋给新增结点的头指针,这样才能保证循环,2楼有问题
        }
        public List reverse(){//猜测应该是反转
            List listNew=new List();
            List nextNode=this.next;//每次将后指针指向的节点添加为表头,这里是一个复制的过程
            while(nextNode!=this){//控制条件,循环结束
                Object obj=nextNode.data;
                listNew.addHead(obj);
                nextNode=nextNode.next;//指针后移
            }
            return listNew;
        }
    }