java_數據結構_鏈表

Java鏈表類List的源代碼如下

/**
 * @Project: struts2
 * @Title: LinkedList.java
 * @Package com.yza.struct
 * @author yongzhian
 * @date 2014-10-8 上午11:43:17
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.yza.struct;

import java.util.NoSuchElementException;

/**
 * @ClassName LinkedList
 * @Description 鏈表
 * @author yongzhian
 * @Date 2014-10-8
 */
public class LinkedList {
	private Node head = null;
	private Node tail = null;
	private Node current = null;
	private int length = 0;

	/*清空鏈表*/
	public void clearAll() {
		head = null;
		tail = null;
		current = null;
		length = 0;
	}

	/*鏈表復位,使第一個結點成爲當前結點*/
	public void reset() {
		current = null;
	}

	/* 判斷鏈表是否爲空 */
	public boolean isEmpty() {
		return (length == 0);
	}

	/* 判斷當前結點是否爲最後一個結點 */
	public boolean isEnd() {
		if (length == 0) {
			throw new java.lang.NullPointerException();
		} else if (length == 1) {
			return true;
		} else {
			return (current == tail);
		}
	}
	 /*返回當前結點的下一個結點的值,並使其成爲當前結點*/ 
	public Node nextNode() {
		if(this.length==1){
			throw new NoSuchElementException();
		}else if(length==0) {
			throw new java.lang.NullPointerException(); 
		}else {  
			this.current =  this.current.next; 
				return this.current;
		}
	}
	public void insert(int d) {
		Node n = new Node(d);
		if(this.length == 0 ){
			this.head = n;
			this.tail = n;
			this.current = n;
		}else{
			n.next = this.current.next;
			this.current = n;
		} 
		this.length++;
	}
 /* (non-Javadoc)
 * <p>Title: toString</p> 
 * <p>Description: 方法描述</p> 
 * @return
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
	// TODO Auto-generated method stub
	return "head:"+this.head+ "  tail:"+this.tail +"  current :"+this.current +"  "+ this.length;
}
	public static void main(String[] args) {
		LinkedList ll = new LinkedList();
		if(ll.isEmpty()){
			System.out.println(ll +" isEmpty " +ll.isEmpty()); 

		}else{
			System.out.println(ll +"  isEnd" +ll.isEnd()); 

		}
		ll.insert(3);
		
		if(ll.isEmpty()){
			System.out.println(ll +" isEmpty " +ll.isEmpty()); 

		}else{
			System.out.println(ll +"  isEnd" +ll.isEnd()); 

		}
		ll.insert(4);
		System.out.println(ll);
		ll.insert(5);
		if(ll.isEmpty()){
			System.out.println(ll +" isEmpty " +ll.isEmpty()); 

		}else{
			System.out.println(ll +"  isEnd" +ll.isEnd()); 

		}
	}
}

class Node {
	int value;
	Node next;

	Node(int value) {
		this.value = value;
		next = null;
	}
	/* (non-Javadoc)
	 * <p>Title: toString</p> 
	 * <p>Description: 方法描述</p> 
	 * @return
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return " "+ this.value;
	}
}
雙向鏈表可以用類似的方法實現只是結點的類增加了一個指向前趨結點的指針。

class Node {
	int value;
	Node next;
	Node previous; 
	
	Node(int value) {
		this.value = value;
		next = null;
		previous=null; 
	}
	/* (non-Javadoc)
	 * <p>Title: toString</p> 
	 * <p>Description: 方法描述</p> 
	 * @return
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return " "+ this.value;
	}



發佈了117 篇原創文章 · 獲贊 2 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章