package e_link.I_Iterator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 链表实现迭代器
*
* @author Administrator
*
*/
public class InterIterApp {
public static void main(String[] args) throws IOException {
LinkList theList = new LinkList();
ListIterator iterator = theList.getIterator();
long value;
iterator.insertAfter(20);
iterator.insertAfter(30);
iterator.insertAfter(40);
iterator.insertAfter(50);
iterator.insertBefore(60);
while (true) {
int cho = getChar();
switch (cho) {
case 's':// 遍历
if (!theList.isEmpty())
theList.displayList();
else
System.out.println("List is empty");
break;
case 'r':// 迭代器复位并设在表头
iterator.reset();
break;
case 'n':// 下一个链接点
if (!theList.isEmpty() && !iterator.atEnd())
iterator.nextLink();
else
System.out.println("不能去下一个链接");
break;
case 'g':// 获取当前数据
if (!theList.isEmpty()) {
value = iterator.getCurrent().dData;
System.out.println(value);
} else
System.out.println("链表为空");
break;
case 'b': // 在链接点前面插入新链接点
value = getInt();
iterator.insertBefore(value);
break;
case 'a': // 在链接点后面插入新链接点
value = getInt();
iterator.insertAfter(value);
break;
case 'd': // 删除当前
if (!theList.isEmpty()) {
value = iterator.deleteCurrent();
System.out.println(value);
} else
System.out.println("不能删除");
break;
default:
System.out.println("Invalid entry");// 无效
}
}
}
/*
* 返回控制台输出的字符
*/
public static String getString() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}
private static int getChar() throws IOException {
String s = getString();
return s.charAt(0);
}
private static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}package e_link.I_Iterator;
public class Link {
public long dData;
public Link next;
public Link(long dData) {
this.dData = dData;
}
public void displayLink(){
System.out.print(dData+" ");
}
}package e_link.I_Iterator;
public class LinkList {
private Link first;
public LinkList() {
first = null;
}
public void setFirst(Link first) {
this.first = first;
}
public Link getFirst() {
return first;
}
public boolean isEmpty() {
return first == null;
}
public ListIterator getIterator() {
return new ListIterator(this);
}
public void displayList() {
Link current = first;
while (current!= null) {
current.displayLink();
current = current.next;
}
System.out.println();
}
}package e_link.I_Iterator;
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;
}
/*
* 迭代器到达末尾,返回null
*/
public boolean atEnd() {
return current.next == null;
}
/*
* 迭代器移动到下一个链接点
*/
public void nextLink() {
previous = current;
current = current.next;
}
/*
* 获取当前
*/
public Link getCurrent() {
return current;
}
/*
* 迭代器后面插入一个新链接点
*/
public void insertAfter(long dd) {
Link newLink = new Link(dd);
if (ourList.isEmpty()) {
ourList.setFirst(newLink);
current = newLink;// 跳到当前增加的对象链接点
} else {
newLink.next = current.next;
current.next = newLink;
nextLink();// 跳到当前增加的对象链接点
}
}
/*
* 迭代器前面插入一个新链接点
* 执行后,让current指向新插入的链接点
*/
public void insertBefore(long dd) {
Link newLink = new Link(dd);
if (previous == null) {
newLink.next = ourList.getFirst();
ourList.setFirst(newLink);// 让current指向新插入的链接点
reset();
} else {
newLink.next = previous.next;
previous.next = newLink;
current = newLink;// 让current指向新插入的链接点
}
}
/*
* 删除当前链接点
* 执行后,让current指向新插入的链接点
*/
public long deleteCurrent() {
long value = current.dData;
if (previous == null) {
ourList.setFirst(current.next);
reset();
}
else{
previous.next=current.next;
if(atEnd()){//如果current下一个为null
reset();//current等于第一个对象
}else{
current=current.next;//让current指向新插入的链接点
}
}
return value;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 链表实现迭代器
*
* @author Administrator
*
*/
public class InterIterApp {
public static void main(String[] args) throws IOException {
LinkList theList = new LinkList();
ListIterator iterator = theList.getIterator();
long value;
iterator.insertAfter(20);
iterator.insertAfter(30);
iterator.insertAfter(40);
iterator.insertAfter(50);
iterator.insertBefore(60);
while (true) {
int cho = getChar();
switch (cho) {
case 's':// 遍历
if (!theList.isEmpty())
theList.displayList();
else
System.out.println("List is empty");
break;
case 'r':// 迭代器复位并设在表头
iterator.reset();
break;
case 'n':// 下一个链接点
if (!theList.isEmpty() && !iterator.atEnd())
iterator.nextLink();
else
System.out.println("不能去下一个链接");
break;
case 'g':// 获取当前数据
if (!theList.isEmpty()) {
value = iterator.getCurrent().dData;
System.out.println(value);
} else
System.out.println("链表为空");
break;
case 'b': // 在链接点前面插入新链接点
value = getInt();
iterator.insertBefore(value);
break;
case 'a': // 在链接点后面插入新链接点
value = getInt();
iterator.insertAfter(value);
break;
case 'd': // 删除当前
if (!theList.isEmpty()) {
value = iterator.deleteCurrent();
System.out.println(value);
} else
System.out.println("不能删除");
break;
default:
System.out.println("Invalid entry");// 无效
}
}
}
/*
* 返回控制台输出的字符
*/
public static String getString() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}
private static int getChar() throws IOException {
String s = getString();
return s.charAt(0);
}
private static int getInt() throws IOException {
String s = getString();
return Integer.parseInt(s);
}
}package e_link.I_Iterator;
public class Link {
public long dData;
public Link next;
public Link(long dData) {
this.dData = dData;
}
public void displayLink(){
System.out.print(dData+" ");
}
}package e_link.I_Iterator;
public class LinkList {
private Link first;
public LinkList() {
first = null;
}
public void setFirst(Link first) {
this.first = first;
}
public Link getFirst() {
return first;
}
public boolean isEmpty() {
return first == null;
}
public ListIterator getIterator() {
return new ListIterator(this);
}
public void displayList() {
Link current = first;
while (current!= null) {
current.displayLink();
current = current.next;
}
System.out.println();
}
}package e_link.I_Iterator;
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;
}
/*
* 迭代器到达末尾,返回null
*/
public boolean atEnd() {
return current.next == null;
}
/*
* 迭代器移动到下一个链接点
*/
public void nextLink() {
previous = current;
current = current.next;
}
/*
* 获取当前
*/
public Link getCurrent() {
return current;
}
/*
* 迭代器后面插入一个新链接点
*/
public void insertAfter(long dd) {
Link newLink = new Link(dd);
if (ourList.isEmpty()) {
ourList.setFirst(newLink);
current = newLink;// 跳到当前增加的对象链接点
} else {
newLink.next = current.next;
current.next = newLink;
nextLink();// 跳到当前增加的对象链接点
}
}
/*
* 迭代器前面插入一个新链接点
* 执行后,让current指向新插入的链接点
*/
public void insertBefore(long dd) {
Link newLink = new Link(dd);
if (previous == null) {
newLink.next = ourList.getFirst();
ourList.setFirst(newLink);// 让current指向新插入的链接点
reset();
} else {
newLink.next = previous.next;
previous.next = newLink;
current = newLink;// 让current指向新插入的链接点
}
}
/*
* 删除当前链接点
* 执行后,让current指向新插入的链接点
*/
public long deleteCurrent() {
long value = current.dData;
if (previous == null) {
ourList.setFirst(current.next);
reset();
}
else{
previous.next=current.next;
if(atEnd()){//如果current下一个为null
reset();//current等于第一个对象
}else{
current=current.next;//让current指向新插入的链接点
}
}
return value;
}
}