# 链表迭代器

970人阅读 评论(0)

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:

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：360272次
• 积分：3729
• 等级：
• 排名：第8954名
• 原创：74篇
• 转载：31篇
• 译文：0篇
• 评论：62条
评论排行
最新评论