日期:2014-05-20  浏览次数:20578 次

关于链表的一个问题
程序有点长,但很简单,就是一个普通的链表。
请问程序中的
        end.next   =   newEnd;
        end   =   newEnd;
这两条语句有什么用处?第二条没有看懂~~


public   class   LinkedList   {

        public   LinkedList()   {
        }
       
        public   LinkedList(Object[]   items){
        if   (items   !=   null){
        for   (int   i   =   0;   i   <   items.length;   i++){
        addItem(items[i]);
        }
        current   =   start;
        }
        }
       
        public   void   addItem(Object   item){
        ListItem   newEnd   =   new   ListItem(item);
        if   (start   ==   null){
        start   =   end   =   newEnd;
        }   else   {
        end.next   =   newEnd;
        end   =   newEnd;
        }
        }
       
        public   Object   getFirst(){
        current   =   start;
        return   start   ==   null   ?   null   :   start.item;
        }
       
        public   Object   getNext(){
        if   (current   !=   null){
        current   =   current.next;
        }
        return   current   ==   null   ?   null   :   current.item;
        }
       
        private   ListItem   start   =   null;
        private   ListItem   end   =   null;
        private   ListItem   current   =   null;
        private   class   ListItem   {
        public   ListItem(Object   item){
        this.item   =   item;
        next   =   null;
        }
       
        public   String   toString(){
        return   "ListItem "   +   item;
        }
       
        ListItem   next;
        Object   item;
        }
}

------解决方案--------------------
将结尾的end连到newEnd上
再将newEnd指定为结尾
------解决方案--------------------
就是楼上的意思
链表这东西用手画画图就很好理解了~
------解决方案--------------------