java 鏈表實現(測試是否有環)

 
package com.bb.bbs;

import java.util.ArrayList;

/**
 * 節點類:用於保存鏈表中的節點
 */
class Node{
	Node next;
	String data;//下一節點
	public static int maxs = 0;//getSize() 最大數量
	public static int maxg = 0;//getArray()最大數量
	public static int maxp = 0;//printNode()打印節點最大數量
	public static int maxc = 0;//contains()最大數量
         /**
          * 帶參數的構造方法
          */
	public Node(String data){
		this.data = data;
	}
         /**
          * 在當前節點上增加一個節點
          */
	public void addNode(Node node){
		if(this.next==null){
			this.next = node;
		}else{
			this.next.addNode(node);
		}
	}
         /**
          * 從root開始尋找,目的是移除一個節點
          */
	public boolean removeNode(Node previous,String data){
		if(this.data.equals(data)){
			previous.next = this.next;
			return true;
		}else{
			return this.next.removeNode(this, data);
		}
	}
         /**
          * 是否包含節點
          */
	public boolean contains(String data){
		maxc++;
		if(maxc==10){
			return false;
		}
		if(this.data.equals(data)){
			return true;
		}
		if(this.next == null){
			return false;
		}else{
			return this.next.contains(data);
		}
	}
         /**
          * 打印一個節點
          */
	public void printNode(){
		maxp++;
		if(maxp==10){
			return;
		}
		if(this.next!=null){
			System.out.println(this.next.data);
			this.next.printNode();
		}
	}
         /**
          * 查找並返回一個節點
          */
	public Node findNode(String data){
		if(this.data.equals(data)){
			return this;
		}else{
			return this.next.findNode(data);
		}
	}
         /**
          * 得到鏈表大小
          */ 
	public int getSize(int currentNum){
		maxs++;
		if(maxs==10){
			return 10;
		}
		if(this!=null){
			currentNum++;
		}
		if(this.next!=null){
			return this.next.getSize(currentNum);
		}else{
			return currentNum;
		}
	}
         /**
          * 將節點裏所有值封裝到一個ArrayList中
          */
	public void getArray(ArrayList tArrayList){
		maxg++;
		if(maxg==10){
			return;
		}
		tArrayList.add(this.data);
		if(this.next!=null){
			this.next.getArray(tArrayList);
		}
	}
}

/**
 * 鏈表類
 */
class Link{
	Node root;
	public void add(String data){
		if(data==null){
			return;
		}
		if(root ==null){
			root = new Node(data);
		}else{
			root.addNode(new Node(data));
		}
	}
	public boolean remove(String data){
		if(root.data.equals(data)){
			root = root.next;
			return true;
		}else{
			return this.root.next.removeNode(root, data);
		}
	}
	public boolean contains(String data){
		if(root.data.equals(data)){
			return true;
		}else{
			return root.contains(data);
		}
	}
	public void print(){
		if(root!=null){
			System.out.println(root.data);
			root.printNode();
		}
	}
	public Node find(String data){
		if(contains(data)){
			if(this.root.data.equals(data)){
				return root;
			}else{
				return root.findNode(data);
			}
		}
		return null;
	}
	public int size(){
		if(root.next ==null){
			return 1;
		}else{
			return root.next.getSize(1);
		}
	}
	public void getArray(ArrayList tArrayList){
		if(root!=null){
			tArrayList.add(root.data);
		}
		if(root.next!=null){
			root.next.getArray(tArrayList);
		}
	}
}

/** 
 * 測試入口
 */
public class LinkList {
	public static void main(String args[]){
                  //1、增加鏈表節點
		Link tLink = new Link();
		tLink.add("A");
		tLink.add("B");
		tLink.add("C");
		tLink.add("D");
                  tLink.print();
		//2、形成環 A->B->C->A->B->C->A->B->C...... 無限循環
		Node fnodeA = tLink.find("A");
		Node fnodeC = tLink.find("C");
		//如果有一個爲空
		if(fnodeA==null || fnodeC==null){
			System.out.println("沒有找到元素!");
		}else{
			System.out.println("已找到元素!");
			fnodeC.next = fnodeA;//出現環
		}
		int linksize = tLink.size();
		System.out.println("鏈表大小爲:"+linksize);
		ArrayList tArrayList = new ArrayList();
		tLink.getArray(tArrayList);
		for(Object o : tArrayList){
			String str = o.toString();
			System.out.println(str);
		}
		boolean flag = false;
		int circleLen = 0;
		//檢驗 "環" 是否存在
		for(int i=0;i<linksize;i++){
			for(int j=i;j<linksize;j++){
				if(j!=i && tArrayList.get(j).toString().equals(tArrayList.get(i).toString())){
					circleLen = j-i;
					flag = true;
					break;
				}
			}
		}		
		if(flag){
			System.out.println("有環,環的長度爲:"+circleLen);
		}else{
			System.out.println("無環");
		}
                  //3、移除一個節點
		tLink.remove("B");
                  //4、移除後打印所有節點
		tLink.print();
                  //打印鏈表長度
		tLink.root.maxs = 0;
		System.out.println(tLink.size());
                  //5、是否包含有C這個節點
		System.out.println(tLink.contains("C"));
	}
}
 



 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章