链表迭代器

原创 2012年03月29日 17:03:36

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:

相关文章推荐

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

List with iterator(链表的迭代器实现)我们都知道STL容器内部都有相应的迭代器,本文主要讨论的就是链表内部的迭代器实现。测试代码#include #include using n...

双向循环链表——STL迭代器

“test.cpp” #define _CRT_SECURE_NO_WARNINGS 1 #include using namespace std; template struct ListNod...

·链表操作+迭代器 字符消除

绝世大坑: list.insert(it,xx)是在

使用迭代器(Iterator)来实现链表的相关操作

在这一篇里,要讲的是如何使用迭代器对链表进行相关的操作,比如删除,获取,插入等操作,那首先介绍一下什么是迭代器,java里面的迭代器的实用性很大,在我们实际对链表进行删除操作时,如果是要对某个限制条件...
  • hasser1
  • hasser1
  • 2015年08月25日 16:43
  • 370

链表与迭代器

链表的使用中find操作是常见的,很多时候也需要对链表进行遍历。如果使用迭代器遍历每一个对象,这样就会使数据结构与遍历对象(迭代器)区分开,java中的Iterator作为轻量级对象,创建代价比较小。...
  • cai2016
  • cai2016
  • 2016年09月07日 19:14
  • 204

python数据结构学习笔记-2016-11-07-03-多重链表以及相应的迭代器

9.3 多重链表         多重链表(multi-linked list):每一个结点都含有多个指针,之前的双链表就是多重链表。         9.3.1 多条链         下图就...

简单实现c++数组,链表及其迭代器

#include #include #include #includeusing namespace std;template class MyArray { private: int m_n...

数据结构与算法(C语言版)__链表的迭代器

STL中的链表#include链表有一个迭代器,迭代器可以一个一个的输出数据,功能强大。今天我们也设计链表迭代器。 我们自己设计的链表迭代器//MyList.h template class Lis...

双向链表(迭代器)

//双向链表(迭代器)#include #includeusing namespace std;//链表结点 template struct _ListNode { _ListN...
  • bitboss
  • bitboss
  • 2016年09月21日 19:26
  • 1171
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:链表迭代器
举报原因:
原因补充:

(最多只允许输入30个字)