迭代器

作者: shenlong77 | 来源:发表于2017-09-13 11:19 被阅读0次

迭代器与链表伴随使用,为了获取链表的当前节点,可以使用迭代器。
下面是单链表的迭代器,需要为单链表增加一个getIterator的方法,其他不变
链表类

public class SingleList {
    //头节点
    public Link first;
    //在链表的头部插入一个元素
    public void insertFirst(int num){
        Link link=new Link(num);
        link.next=first;
        first=link;
    }
    //在链表的头部删除一个元素
    public Link deleteFirst(){
        if(first==null){
            return null;
        }
        Link temp=first;
        first=first.next;
        return temp;
    }
    //遍历显示链表中的所有元素
    public void display(){
        Link current=first;
        while(current!=null){
            System.out.println(current.num+",");
            current=current.next;
        }
    }
    //根据变量查询指定元素节点
    public Link find(int num){
        Link current=first;
        while(current.num!=num){
            if(current.next==null){
                return null;
            }else{
                current=current.next;
            }
        }
        return current;
    }
    //根据指定变量删除节点
    public Link delete(int num){
        Link current=first;
        Link previous=null;
        while(current.num!=num){
            if(current.next==null){
                return null;
            }else{
                previous=current;
                current=current.next;
            }
        }
        if(current==first){
            first=first.next;
        }else{
            previous.next=current.next;
        }
        return current;
    }
    //设置迭代器
    public Iterator getIterator(){
        Iterator iterator=new Iterator(this);
        return iterator;
    }
}

迭代器类

public class Iterator {
    private Link previous;
    private Link current;
    private SingleList ourList;
    public Iterator(SingleList list){
        ourList=list;
        reset();
    }
    //将当前项指向链表的第一项
    public void reset(){
        current=ourList.first;
        previous=null;
    }
    //指针向后移动一项
    public void moveNext(){
        if(current.next==null){
            reset();
        }else{
            previous=current;
            current=current.next;
        }
    }
    //获取当前节点
    public Link getCurrent(){
        return current;
    }
    //判断是否到了最后一项
    public Boolean isLast(){
        if(current.next==null){
            return true;
        }else{
            return false;
        }
    }
}

相关文章

  • 迭代器

    本节实验我们将为大家讲解迭代器,主要介绍 5 种常见迭代器:输入、输出迭代器,前向逆向迭代器,双向迭代器和随机迭代...

  • 迭代器与生成器

    迭代(iteration)与可迭代(iterable) 迭代器 迭代器协议(iterator protocol) ...

  • 10.迭代器与生成器

    一、迭代器 1). 迭代器概述 类比Java中的迭代器,参考迭代器模式https://www.jianshu.co...

  • 9 ES6 迭代器、生成器

    1、Iterator迭代器 ->遵循迭代模式思想实现,分为内部迭代器、外部迭代器,聚焦点:添加迭代接口 (1).迭...

  • C++boolan part3_week4

    1. 迭代器 1.1 迭代器的种类 使用随机访问迭代器的容器:array, vector,deque使用双向迭代器...

  • 迭代器模式

    内部迭代器和外部迭代器 简单地讲,内部迭代器就是类似 forEach 的迭代器,其内部迭代逻辑已经制定好,只要传入...

  • vector erase的用法

    正向迭代器: 反向迭代器:

  • 10 python中的迭代器和生成器

    1 迭代器 遵循了迭代器协议的对象为迭代器,即内部调用iter()方法转化成的对象为成迭代器。 1.1 迭代器的特...

  • 第7章迭代器模式

    第7章迭代器模式 7.1 jQuery中的迭代器 7.2 实现自己的迭代器 7.3 内部迭代和外部迭代 1内部迭代...

  • 我们来讲讲Python的迭代器

    迭代器 迭代器的作用:迭代器的作用就是迭代,可以让不是序列但是表现出序列行为,例如迭代字典的键,文件的行 迭代器的...

网友评论

      本文标题:迭代器

      本文链接:https://www.haomeiwen.com/subject/hquksxtx.html