###### 链表迭代器

Java数据结构和算法中文第二版.pdf 代码

package com.ch5.interIterator;

private long dData ;

this.dData = key ;
}

public long getdData() {
return dData;
}

public void setdData(long dData) {
this.dData = dData;
}

return next;
}

this.next = next;
}

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

}

package com.ch5.interIterator;

first = null ;
}

return first;
}

this.first = first;
}

public boolean isEmpty(){
return (first == null)  ;
}

public ListIterator getIterator(){
return new ListIterator(this) ;
}

public void displayList(){
while (null != current){
System.out.println(current.getdData() + " ");
current = current.getNext() ;
}
System.out.println(" ");

}
}

ListIterator.java

package com.ch5.interIterator;

public class ListIterator {

this.ourList = ourList ;
reset() ;
}

public void reset() {
current = ourList.getFirst() ;
previous = null ;
}

public boolean atEnd(){
return (current.getNext() == null ) ;
}

previous = current ;
current = current.getNext() ;
}

return this.current ;
}

return this.previous ;
}

public void insertAfter(long value){
if (ourList.isEmpty()){
}else{
}
}

public  void insertBefor(long value){
if (this.previous == null){
reset() ;
}else{
}
}

public long deleteCurrent(){
if (current == null) {
return -1 ;
}
long value = current.getdData() ;

if (previous == null){
ourList.setFirst(current.getNext()) ;
reset() ;
}else{
previous.setNext(current.getNext()) ;
if (atEnd()){
reset() ;
}else{
current = current.getNext() ;
}
}
return value ;

}

}

InterIterApp.java

package com.ch5.interIterator;

import java.io.IOException;

public class InterIterApp {

/**
* @param args
*/
public static void main(String[] args) throws IOException {

ListIterator iter = theList.getIterator();

long value;
iter.insertAfter(20);
iter.insertAfter(40);
iter.insertAfter(80);
iter.insertBefor(60);

while (true) {
System.out.print("Enter first letter of show, reset, ");
System.out.print("next, get, before, after, delete:");
System.out.flush();

int choice = getChar();
switch (choice) {
case 's':
if (!theList.isEmpty()) {
theList.displayList();
} else {
System.out.println("List is empty!");
}
break;
case 'r':
iter.reset() ;
break ;
case 'n':
if (!theList.isEmpty() && !iter.atEnd()){
}else{
System.out.println("Can't go to next Link !");
}
break ;
case 'g':
if (! theList.isEmpty()){
value = iter.getCurrent().getdData() ;
System.out.println("Returned :" + value);
}else{
System.out.println("List is empty!");
}
break ;
case 'b':
System.out.println("Enter value to insert: ");
System.out.flush() ;
value = getInt() ;
iter.insertBefor(value) ;
break ;
case 'a':
System.out.println("Enter value to insert: ");
System.out.flush() ;
value = getInt() ;
iter.insertAfter(value) ;
break ;
case 'd':
if (!theList.isEmpty()){
value = iter.deleteCurrent() ;
System.out.println("Deleted : " + value );
}else{
System.out.println("List is empty!");
}
break ;
case 'q':
System.out.println("Quit , Bye Bye.") ;
System.exit(0) ;
default:
System.out.println("Invaild Entry.") ;
break ;
}
}
}

public static String getString() throws IOException {
return s;
}

public static char getChar() throws IOException {
String s = getString();
return s.charAt(0);
}

public static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}

Enter first letter of show, reset, next, get, before, after, delete:s
20
40
60
80

Enter first letter of show, reset, next, get, before, after, delete:a
Enter value to insert:
10
Enter first letter of show, reset, next, get, before, after, delete:s
20
40
60
10
80

Enter first letter of show, reset, next, get, before, after, delete:d
Deleted : 10
Enter first letter of show, reset, next, get, before, after, delete:s
20
40
60
80

Enter first letter of show, reset, next, get, before, after, delete:

#### 链表与迭代器

2016-09-07 19:14:19

#### 浅谈STL list<T>链表容器和迭代器的使用C++实现

2017-05-26 21:46:10

#### JAVA链表中迭代器的实现

2015-05-17 21:48:02

#### java链表的get方法遍历VS使用迭代器遍历

2015-09-30 15:05:55

#### 学习STL，实现一个单链表的迭代器

2015-09-10 10:38:00

#### 数据结构与算法（C语言版）__链表的迭代器

2016-12-24 23:12:05

#### [C++]List with iterator(链表的迭代器实现）

2016-05-30 16:21:34

#### 初学Qt：链表迭代器

2017-11-06 11:24:25

#### Java（7-2 迭代器和链表详细说明）

2017-11-01 01:31:41

#### Python 使用单链表实现队列 (基于class, 包含迭代器)

2015-02-03 14:21:15