java使用模板类实现迭代器遍历“聚集”对象

什么叫“聚集”对象呢?简单理解就是”容器“(或者是可遍历对象)

经过简单的测试发现没有问题(写的比较匆忙),如果以上代码有任何问题,望各位批评指正

//使用模板类实现双向链表
public class template_linklist {
    public static void  main(String [] args)
    {
        TemplateTwoWayLinklist<String> templateTwoWayLinklist = new TemplateTwoWayLinklist<String>();
        templateTwoWayLinklist.pre_add("this is first node");
        templateTwoWayLinklist.pre_add("this is 2th node");
        templateTwoWayLinklist.pre_add("this is 3th node");

//        templateTwoWayLinklist.ergodic();
        iterator_impl<String> it = new iterator_impl<>(templateTwoWayLinklist);
       System.out.println(it.currentItem());
       it.next();
       System.out.println(it.currentItem());
    }
}

//节点类
class Node_OUT<T>{
    //申请指针域
    Node_OUT<T> next;
    Node_OUT<T> pre;
    //申请数据域
    T data;
    //构造函数
    Node_OUT()
    {
        data=null;
    }
    Node_OUT(T data)
    {
        this.data = data;
    }
}

//模板类的双向链表实现
class TemplateTwoWayLinklist<T>{
    //在类的内部申请头尾节点作为类变量
    private Node_OUT<T> head = new Node_OUT<T>();
    private Node_OUT<T> tail = new Node_OUT<T>();
    //链表的长度信息
    private int length;
    //构造函数,在链表一开始建立申请时就需要有头尾节点
    TemplateTwoWayLinklist()
    {
        head.next = tail;
        head.pre = null;

        tail.next = null;
        tail.pre = head;

    }

    void pre_add(T data)
    {
        Node_OUT<T> tNode_out = new Node_OUT<>(data);
        //申请一个临时节点
        Node_OUT<T> temp = new Node_OUT<>();
        //插入
        temp.next = head;
        int i=0;
        while (i<this.length)
        {
            temp.next = temp.next.next;
            i++;
        }
        //插入操作是先断开后链接
        tNode_out.pre = temp.next;
        tNode_out.next = temp.next.next;
        temp.next.next = tNode_out;
        temp.next.pre = tNode_out;
        this.length = this.length+1;

    }
    void ergodic()
    {
        //申请一个临时节点
        Node_OUT<T> temp = new Node_OUT<>();
        //插入
        temp.next = head;
        while(temp.next.next != null)
        {
            System.out.println(temp.next.data);
            temp.next = temp.next.next;
        }
    }
    boolean isEmpty()
    {
        return this.length==0;
    }
    Node_OUT<T> getFirst()j
    {
        return head;
    }
    public Node_OUT<T> getNext(Node_OUT<T> current)
    {
        return current.next;
    }
}

interface _iterator_<T>{
    //将“游标”指向第一个元素
    public void first();
    //将“游标”指向下一个元素
    public void next();
    //判断迭代器中是否还有元素
    public boolean isDone();
    //返回当前的元素
    public T currentItem();
}


class iterator_impl<T> implements _iterator_<T>{
    //迭代器类需要实现拥有一个可迭代的“聚集”(集合)对象作为属性值,这里以上面的双向链表为例
    //申请一个链表节点
    private TemplateTwoWayLinklist<T> linklist;
    // 申请一个表示指向当前节点的指针节点
    private Node_OUT<T> current;
    //构造函数,需要传进来一个链表,然后在迭代器内部实现next、first、判断、获取当前元素的功能
    public iterator_impl(TemplateTwoWayLinklist<T> linklist)
    {
        this.linklist = linklist;
        if(this.linklist.isEmpty())
        {
            throw new RuntimeException("迭代对象为空");
        }
        //拿到第一个节点(返回的是指向第一个节点的指针)
        this.current = linklist.getFirst();

    }
    public void first()
    {
        if(this.linklist.isEmpty())
        {
            throw new RuntimeException("迭代对象为空");
        }
        //拿到第一个节点(返回的是指向第一个节点的指针)完成初始化,以方便成员函数对“聚集”对象遍历

        this.current = linklist.getFirst();
    }
    public void next()
    {
        //注意的是变化的是next指针不是current
        this.current.next = this.current.next.next;
    }
    //判断传进来的链表是否为空
    public boolean isDone()
    {
        return true;
    }
    //获取当前指针指向的数据
    public T currentItem()
    {
        return this.current.next.data;
    }
}

j

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值