博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迭代器
阅读量:7238 次
发布时间:2019-06-29

本文共 4849 字,大约阅读时间需要 16 分钟。

  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

 
   

转载于:https://www.cnblogs.com/cici-new/p/3155908.html

你可能感兴趣的文章
担心被淘汰?请看这份财会人员晋升指南!
查看>>
JB的小程序之旅-小程序基础(登录授权、请求数据)
查看>>
(七)微服务分布式云架构spring cloud - common-service 项目构建过程
查看>>
简谈socket在直播软件开发上的应用
查看>>
JavaScript数组增删改查知识梳理
查看>>
日常抄书之React中Diff算法思路
查看>>
(二)大型互联网分布式企业微服务云架构
查看>>
初探和实现websocket心跳重连
查看>>
宁撞金钟一下,不打破鼓三千,IT人要有志气,要进就进大的好的公司
查看>>
实现一个jQuery的API
查看>>
Python学习教程(Python学习路线):Day11-文件和异常
查看>>
证书更新
查看>>
移动端swiper嵌iframe无法滑动的解决方案
查看>>
spring cloud构建互联网分布式微服务云平台- hystrix工作原理
查看>>
立即执行函数
查看>>
腾讯X5内核集成一些建议和爬坑记录
查看>>
直接使用 x y 调用frame的x y 值
查看>>
iOS富文本的简单使用
查看>>
云栖专辑| 阿里毕玄:程序员的成长路线
查看>>
Redis分布式缓存安装和使用
查看>>