Java数据结构和算法中文第二版.pdf 代码
Link.java package com.ch5.interIterator; public class Link { private long dData ; private Link next ; public Link(long key){ this.dData = key ; } public long getdData() { return dData; } public void setdData(long dData) { this.dData = dData; } public Link getNext() { return next; } public void setNext(Link next) { this.next = next; } public void displayLink(){ System.out.print(dData + " ") ; } } LinkList.java package com.ch5.interIterator; public class LinkList { private Link first ; public LinkList(){ first = null ; } public Link getFirst() { return first; } public void setFirst(Link first) { this.first = first; } public boolean isEmpty(){ return (first == null) ; } public ListIterator getIterator(){ return new ListIterator(this) ; } public void displayList(){ Link current = first ; while (null != current){ System.out.println(current.getdData() + " "); current = current.getNext() ; } System.out.println(" "); } } ListIterator.java package com.ch5.interIterator; public class ListIterator { private Link current ; private Link previous ; private LinkList ourList ; public ListIterator(LinkList ourList){ this.ourList = ourList ; reset() ; } public void reset() { current = ourList.getFirst() ; previous = null ; } public boolean atEnd(){ return (current.getNext() == null ) ; } public void nextLink(){ previous = current ; current = current.getNext() ; } public Link getCurrent(){ return this.current ; } public Link getPrevious(){ return this.previous ; } public void insertAfter(long value){ Link newLink = new Link(value) ; if (ourList.isEmpty()){ ourList.setFirst(newLink) ; current = newLink ; }else{ newLink.setNext(current.getNext()) ; current.setNext(newLink) ; nextLink() ;//当前节点为新增节点 } } public void insertBefor(long value){ Link newLink = new Link(value) ; if (this.previous == null){ newLink.setNext(ourList.getFirst()) ; ourList.setFirst(newLink) ; reset() ; }else{ newLink.setNext(previous.getNext()) ; previous.setNext(newLink) ; current = newLink ; } } 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InterIterApp { /** * @param args */ public static void main(String[] args) throws IOException { LinkList theList = new LinkList(); 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()){ iter.nextLink() ; }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 { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); 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: