迭代器

  1. 代码
    1. 节点:单链表节点LinkNode 
    2. 链表:单链表 -->额外方法:getIterator()
      1. public class IteratorLinkList extends List {
            static LinkNode first = null;
            
            public LinkNode getFirst(){
                return first;
            }
            
            public  void setFirst(LinkNode first) {
                IteratorLinkList.first = first;
            }
        
            public boolean isEmpty() {
                return first==null;
            }
            public void  insertFirst(LinkNode o1) {// 头插法
                if(isEmpty()) {
                    first= o1;
                    return;
                }
                o1.next = first;
                first = o1;
            }
             
            // 在指定节点后插入新节点
            public static void insertAfter(LinkNode o1, LinkNode o2) {
                LinkNode current = first;
                LinkNode previous = null;
                while(current!=null){
                    if(current.iData == o1.iData && current.dData == o1.dData){
                        o2.next = current;
                        previous.next = o2;
                        return;
                    }
                    previous = current;
                    current = current.next;
                }
                throw new RuntimeException("没有找到该节点");
            }
            public   LinkNode  deleteFirst() {// 删除头节点
                if(isEmpty()) {
                    throw new RuntimeException("链表为空无法进行删除操作");
                }
                LinkNode temp = first;
                first= first.next; 
                return temp;
            }
            // 显示链表
            public void displayList() {
                LinkNode current = first;
                System.out.println("Display from Front--->End");
                while(current!=null){
                    System.out.print(current.iData + "  "+ current.dData+"  ");
                    System.out.println();
                    current= current.next;
                }
            }
            // 找到指定节点
            public static LinkNode findNode(LinkNode o) {
                LinkNode current = first;
                while(current!=null){
                    if(current.iData == o.iData && current.dData == o.dData){
                        return current;
                    }
                    current = current.next;
                }
                throw new RuntimeException("没有找到该节点");
            }
            // 删除指定节点
            public void deleteNode(LinkNode o) {
                if(isEmpty()) {
                    throw new RuntimeException("链表为空无法进行删除操作");
                }
                LinkNode current = first;
                LinkNode previous = null;
                while(current!=null){
                    if(current.iData == o.iData && current.dData == o.dData){
                        previous.next = current.next;
                        return;
                    }
                    previous = current;
                    current = current.next;
                }
                throw new RuntimeException("没有找到该节点");
            }
               public ListIterator getIterator()  // return iterator
                  {
                  return new ListIterator(this);  // initialized with
                  }  
        }
    3. 迭代器:
      • public class ListIterator {
            //current link
            private LinkNode current;
            //previous link
            private LinkNode previous;
            //the  target link list
            private IteratorLinkList ourList;
            
            
            public ListIterator(IteratorLinkList ourList) {
                super();
                this.ourList = ourList;
            }
            //reset the iterator :let it start at first
            public void reset(){
                current = ourList.getFirst();
                previous = null;
            }
            public boolean atEnd(){//whether at the end of target linklist
                return current.next==null;
            }
            public void nextLink() {        // step to next link
                previous = current;
                current = current.next;
            }
            public LinkNode getCurrent()  {     // get current link
                return current;
            }
             public void insertAfter(LinkNode dd){//insert after current link
                 if( ourList.isEmpty()) {
                     ourList.setFirst(dd);
                     reset();
                 } 
                 dd.next = current.next;
                 current.next = dd;
                 nextLink();
             }
             public void insertBefore(LinkNode dd){//insert before current link
                 if( ourList.isEmpty()) {// the list is empty
                     ourList.setFirst(dd);
                     reset();
                 } if(previous==null){// there is only one item in the list
                     ourList.setFirst(dd);
                     dd.next = current;
                     reset();
                 }else{// there is more than one item in the list
                    dd.next = current;
                    previous.next = dd;
                    current = dd;
                 }
             }
             public LinkNode deleteCurrent() {   // delete item at current
                 LinkNode temp = current;
                 if( ourList.isEmpty()) {// the list is empty
                     throw new RuntimeException("The List is empty ,could not be deleted any more");
                 } if(previous==null){// there is only one item in the list
                     ourList.setFirst(null); 
                     reset();
                 }else{// there is more than one item in the list
                    previous.next = current.next;
                    if( atEnd() )
                        reset();
                     else
                        current = current.next;
                 }
                 return temp;
             }
        }

         

        迭代器插入节点:在当前节点之后插入新的节点

      • 基于单链表的迭代器:

      • ListIterator

      • 方法总结:

         

      • 当前链表没有任何节点

         

         

        当前链表节点数>1

 
   
posted @ 2013-06-26 05:28  王超_cc  阅读(170)  评论(0编辑  收藏  举报