链表迭代器

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值