java 实现 iterator_JAVA链表中迭代器的实现_林竹w的博客-程序员宅基地

技术标签: java 实现 iterator  

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importtest_4_19.system;/*** java链表中迭代器的实现

* 2017年5月2号17:25

*@authorzcj

**/

classLink9

{public longdData;publicLink9 next;public Link9(longdd)

{

dData=dd;

}public voiddisplayLink()

{

System.out.print(dData+ " ");

}

}classLinkList9

{privateLink9 first;publicLinkList9()

{

first= null;

}public Link9 getFirst()//获取链表的first

{returnfirst;

}public void setFirst(Link9 f)//由于在迭代器里面first可能会改变,所以需要LinkList中提供first改变的方法

{

first=f;

}public booleanisEmpty()

{return first==null;

}publicListIterator getIterator()

{return new ListIterator(this);

}public voiddispalyList()

{

Link9 current=first;while(current != null)

{

current.displayLink();

current=current.next;

}

System.out.println("");

}

}classListIterator

{privateLink9 current;privateLink9 previous;privateLinkList9 ourList;publicListIterator(LinkList9 list)

{

ourList=list;

reset();

}public void reset()//把迭代器复位,并放在表头

{

current=ourList.getFirst();

previous= null;

}public boolean atEnd()//判断当前节点是否为最后一个链节点

{return (current.next == null);

}public void nextLink()//到下一个链接点

{

previous=current;

current=current.next;

}public Link9 getCurrent()//得到当前链接点的内容

{returncurrent;

}public void insertAfter(long dd)//在当前链接点后插入新的链节点

{

Link9 newLink= newLink9(dd);if(ourList.isEmpty())

{

ourList.setFirst(newLink);

current=newLink;

}else{

newLink.next=current.next;

current.next=newLink;

nextLink();

}

}public void insertBefore(long dd)//在当前链接点前插入新的链节点

{

Link9 newLink= newLink9(dd);if(previous == null)

{

newLink.next=ourList.getFirst();

ourList.setFirst(newLink);

reset();

}else{

newLink.next=previous.next;

previous.next=newLink;

current=newLink;

}

}public long deleteCurrent()//删除当前链节点

{long value =current.dData;if(previous == null)

{

ourList.setFirst(current.next);

reset();

}else{

previous.next=current.next;if(atEnd())

reset();elsecurrent=current.next;

}returnvalue;

}

}public classinterIterator5_9 {/***@paramargs

*@throwsIOException*/

public static void main(String[] args) throwsIOException {//TODO Auto-generated method stub

LinkList9 theList = newLinkList9();

ListIterator iter1=theList.getIterator();longvalue;

iter1.insertAfter(20);

iter1.insertAfter(40);

iter1.insertAfter(80);

iter1.insertBefore(60);while(true)

{

System.out.println("Enter first letter of ");

System.out.print("show,reset,next,get,before,after,delete: ");

System.out.flush();int choice =getChar();switch(choice) {case 's':if(!theList.isEmpty())

theList.dispalyList();elseSystem.out.println("List is empty!");break;case 'r':

iter1.reset();break;case 'n':if(!theList.isEmpty() && !iter1.atEnd())

iter1.nextLink();elseSystem.out.println("can't go to nextLink");break;case 'g':if(!theList.isEmpty())

{

value=iter1.getCurrent().dData;

System.out.println("returned" +value);

}elseSystem.out.println("List is Empty");break;case 'b':

System.out.println("Enter value to insert: ");

System.out.flush();

value=getInt();

iter1.insertBefore(value);break;case 'a':

System.out.println("Enter value to insert: ");

System.out.flush();

value=getInt();

iter1.insertAfter(value);break;case 'd':if(!theList.isEmpty())

{

value=iter1.deleteCurrent();

System.out.println("Deleted" +value);

}elseSystem.out.println("Can't delete");break;default:

System.out.println("Invalid entry");

}

}

}public static String getString() throwsIOException

{

InputStreamReader isr= newInputStreamReader(System.in);

BufferedReader br= newBufferedReader(isr);

String s=br.readLine();returns;

}public static char getChar() throwsIOException

{

String s=getString();return s.charAt(0);

}public static int getInt() throwsIOException

{

String s=getString();returnInteger.parseInt(s);

}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42511151/article/details/114170974

智能推荐

第四章IntelliJ IDEA 之 HelloWorld 项目创建及相关配置文件介绍-程序员宅基地

在博文“IntelliJ IDEA 的使用界面介绍”中,咱们通过创建一个 Static Web 项目大致了解了 IntelliJ IDEA 的使用界面,接下来,趁着这个热乎劲,咱们来创建第一个 Java 项目“HelloWorld”,进入如下界面:如上图所示,点击Create New Project,进入如下界面:上面的界面,咱们在前一篇博文中已经进行了介绍,在这里...

JVM上篇:内存与垃圾回收篇十三--垃圾回收概述与算法_java中gc1__院长大人_的博客-程序员宅基地

JVM上篇:内存与垃圾回收篇十三–垃圾回收概述与算法0. 大厂面试题0.1 蚂蚁金服你知道哪几种垃圾回收器,各自的优缺点,重点讲一下cms和G1?一面:JVM GC算法有哪些,目前的JDK版本采用什么回收算法?一面:G1回收器讲下回收过程?GC是什么?为什么要有GC?一面:GC的两种判定方法?CMS收集器与G1收集器的特点0.2 百度说一下GC算法,分代回收说下垃圾收集策略和算法0.3 天猫一面:JVM GC原理,JVM怎么回收内存一面:CMS特点,垃圾回收算法有哪些?各自_java中gc1

Java数据结构与算法3 链表_ava.util包下的哪个类实现了链表数据结构-程序员宅基地

链表思想链表是以节点的方式进行存储每个节点包含两个域:data+next:指向下一个节点链表的各个节点并不一定是连续存储的链表分为带头节点的链表和不带头节点的链表,根据实际需求来选择我们以水浒英雄好汉的排名来解释链表结构在Java中的实现class HeroNode { int id; String name; ..._ava.util包下的哪个类实现了链表数据结构

win10 解决.net framework 3.5 安装报错 0x800F0954问题-程序员宅基地

打开注册表:cmd+r 输入regedit,确定; 找到路径HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU,其中UseWUServer默认值为1,改成0; 打开服务列表,重启Windows Update service; 此时可以正常安装.net framework 3.5; 将第二步的修改还原...

OppenCV学习教程之实现自己的滤波器-程序员宅基地

目的本篇教程中,我们将学到:用OpenCV函数 filter2D 创建自己的线性滤波器。原理Note 以下解释节选自Bradski and Kaehler所著 Learning OpenCV 。卷积高度概括地说,卷积是在每一个图像块与某个算子(核)之间进行的运算。核是什么?核说白了就是一个固定大小的数值数组。该数组带有一个 锚点 ,一般位于数组中央。如何用核实现卷积?假如你想...

mfc设置临界区_mfc临界区-程序员宅基地

多线程编程时常常需要设置临界区来防止多个线程同时访问一个资源,设置方法如下1、CRITICAL_SECTION m_crtical_section;//定义一个临界区2、::InitializeCriticalSection(&m_crtical_section);//在初始化里面初始化临界区3、::EnterCriticalSection(&m_crtical_secti_mfc临界区

随便推点

4.9 使用变形工具编辑西瓜图标 [Illustrator CC教程]-程序员宅基地

原文:http://coolketang.com/staticDesign/5a97aea1fe88c20038bf1e75.html1. 本节课将为您演示[变形工具]的使用。鼠标长按工具箱中的[宽度工具],弹出隐藏的工具列表。 2. 然后在弹出的工具列表中,选择[变形工具]。变形工具可以随光标的移动,塑造对象形状,就像在铸造粘土一样。 3. 接着在西瓜图形的上方圆点处按下鼠标,并向下方滑动,以调...

你应当知道的人工智能发展历史-程序员宅基地

在AI 发展的今天,只有了解其来处,才能知其去处~

Papers Notes_6_ DCGAN--Unsupervised Representation Learning with Deep Convolutional GAN_Ashley-Yu的博客-程序员宅基地

Papers Notes_6_ DCGAN--Unsupervised Representation Learning with Deep Convolutional GANApproachArchitectureApproachscale up GANs using CNNs to model imagesthe all convolutional netreplace deterministic spatial pooling functions (such as maxpooling) wi

操作系统修炼指南——保护模式_os学习笔记-程序员宅基地

环境搭建000 实验环境搭建保护模式001 保护模式 002 段寄存器 003 段选择子与段描述符结构 004 段描述符属性分析 005 特权级 006 数据段权限检查 007 代码段权限检查与 jmp 008 跨段提权与调用门 009 调用门(无参) 010 调用门(有参) 011 中断门 012 陷阱门 013_os学习笔记

ThinkPHP3.2天气接口-程序员宅基地

首先在配置文件中配置路由 'URL_ROUTER_ON' => true, 'URL_ROUTE_RULES'=>array( 'index/:wather' => 'Api/read', ),在控制器中<?php// 本类由系统自动生成,仅供测试用途namespace Home\Controller; //..._thinkphp调用网上天气接口

抽象工厂模式 The Abstract Factory Pattern-程序员宅基地

抽象工厂模式——提供一个接口,用于创建相关或依赖对家的家族,而不需要明确指定具体类。(摘自《Head First Design Patterns》) 以下是自已用VS画了一个简图: 创建抽象工厂接口: public interface IPropertyFactory { string C