java實現循環鏈表


前面已經介紹了java實現單鏈表:http://www.cnblogs.com/lixiaolun/p/4643886.html

其實兩者的主要差別就在於如何判斷是否到了鏈表的結尾:

在單鏈表中

while(temp.next!=null)
{
	temp=temp.next;
}

在循環鏈表中

while(temp.next!=header)
{
	temp=temp.next;
}

 

下面是循環鏈表的代碼和測試代碼:

循環鏈表的代碼:

package circularlinkedlist;

public class CircularLinkedList {
	
	class Element
	{
		public Object value=null;
		private Element next=null;
	}
	private Element header = null;//頭結點
	/**
	 * 初始化鏈表
	 * */
	void initList()
	{
		header = new Element();
		header.value=null;
		header.next=header;
	}
	
	/**
	 * 插入鏈表
	 * */
	void insertList(Object o)
	{
		Element e=new Element();
		e.value=o;
		if(header.next==header)//第一次插入元素
		{
			header.next=e;
			e.next=header;
		}else//不是第一次插入元素
		{
			//temp引用在棧中,temp和header引用都指向堆中的initList()中new的Element對象
			Element temp = header;
			while(temp.next!=header)//尋找最后一個元素
			{
				temp=temp.next;
			}
			temp.next=e;
			e.next=header;//新插入的最后一個節點指向頭結點
		}
	}
	
	/**
	 * 刪除鏈表中第i個元素
	 * */
	void deletelist(Object o)
	{
		Element temp =header;
		while(temp.next!=header)
		{
			//判斷temp當前指向的結點的下一個結點是否是要刪除的結點
			if(temp.next.value.equals(o))
			{
				temp.next=temp.next.next;//刪除結點
			}else
			{
				temp=temp.next;//temp“指針”后移
			}
		}
	}
	
	/**
	 * 獲取鏈表的第i個位置的元素
	 * */
	Element getElement(int i)
	{
		if(i<=0 || i>size())
		{
			System.out.println("獲取鏈表的位置有誤!返回null");
			return null;
		}
		else
		{
			int count =0;
			Element element = new Element();
			Element temp = header;
			while(temp.next!=header)
			{
				count++;
				if(count==i)
				{
					element.value=temp.next.value;
				}
				temp=temp.next;
			}
			return element;
		}
	}
	/**
	 * 鏈表長度
	 * */
	int size()
	{
		Element temp = header;
		int size=0;
		while(temp.next!=header)
		{
			size++;
			temp=temp.next;
		}
		return size;
	}
	
	/**
	 * 判斷鏈表中是否存在某元素
	 * */
	Boolean isContain(Object o)
	{
		Element temp =header;
		while(temp.next!=header)
		{
			if(temp.next.value.equals(o))
			{
				return true;
			}
			temp=temp.next;
		}
		return false;
	}
	/**
	 * 打印鏈表
	 * */
	void print()
	{
		System.out.print("打印鏈表:");
		Element temp =header;
		while(temp.next!=header)
		{
			temp=temp.next;
			System.out.print(temp.value+"\t");
		}
		System.out.println();
	}
}

測試代碼:

package circularlinkedlist;

public class CircularLinkedListMain {

	public static void main(String[] args) {
		CircularLinkedList clList = new CircularLinkedList();
		clList.initList();
		clList.insertList(1);
		clList.insertList(2);
		clList.insertList(3);
		clList.insertList(4);
		clList.insertList(5);
		clList.print();
		
		System.out.println("鏈表長度:"+clList.size());
		clList.deletelist(1);
		clList.deletelist(5);
		clList.print();
		System.out.println("第1個元素值為:"+clList.getElement(1).value);
		System.out.println("第2個元素值為:"+clList.getElement(2).value);
		System.out.println("第3個元素值為:"+clList.getElement(3).value);
	
		System.out.println(clList.isContain(2));
		System.out.println(clList.isContain(6));
//		System.out.println(clList.isContain(5));
	}

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM