鏈表——初識鏈表

鏈表——前言:

 

(小弟初學數據結構,有錯誤的地方望大家不吝賜教)

 

 

認識鏈表:


列表相比數組更具有優勢,鏈表不同於數據和其他數據結構依靠位置來進行訪問或者其他操作,
如數組是依靠下表來操作數據。而鏈表是通過關係來尋找或者操作數據。

 

鏈表是由“鏈接點” 組成,每個鏈接點是一個數據對象,每個鏈接點的對象中包含着下一個鏈接點的引用(next),
但是鏈表本身有一個字段是指向第一個鏈接點的引用的。

 

鏈表的特性:

 

插入 和 刪除 效率高,只需要變更指向的鏈接點即可。但是隨即訪問操作的效率很低,
由於鏈表是通過關係來確定數據的位置,所以,訪問某個數據時,必須通過大量的鏈接點關係,才能訪問到。
效率很低。

 



 

 

 

//鏈表
class LinkList{
	
	public static void main(String[] args) {
		LinkList l = new LinkList();
		//增加
		l.insertFirst(1,1d);
		l.insertFirst(2,2d);
		l.insertFirst(3,3d);
		l.displayList();
		//查詢
		Link findLink = l.find(6);
		if(findLink != null){
			System.out.println(findLink.dData);
		}
		//刪除
		l.deleteLike(7);
		l.displayList();
		
	}
	//第一個鏈接點的位置。
	public Link first;
	
	
	public LinkList(){
		first = null;
	}
	//插入數據:插入的新數據爲鏈表的收割鏈接點,並且將下一個鏈接點的地址指向插入前的首數據。
	public void insertFirst(int id, double dd){
		//新鏈接點
		Link link = new Link(id,dd);
		//將新鏈接點的關係子段 next 指向舊的首鏈接點。
		link.next = first;
		//將鏈表的第一個鏈接點指向新增的鏈接點地址。
		first = link;
	}
	
	//刪除:將當前first 指向第二個鏈接點斷開和第一個鏈接點的連接,
	public Link deleteFirst(){
		Link temp = first;
		first = first.next;
		return first;
	}
	
	//查詢
	public Link find(int key){
		//將link 
		Link current = first;
		//如果當前link 不是要查詢的 數據,則通過它next 子段,查詢到下一個鏈接點。
		//直到找到爲止
		while(current.iData != key){
			if(current.next != null){
				//如果不是要查詢的數據,則通過關係訪問下一個鏈接點,
				//注意,這裏沒有下標的概念,所有查詢通過關係來操作。
				current = current.next;
			}else{
				System.out.println("沒有找到數據");
				current = null;
				break;
			}
		}
		return current;
	}
	
	//刪除:主需要將刪除鏈接點的前一個鏈接點的next地址指向刪除鏈接點的下一個鏈接點位置。
	//鏈表刪除不需要考慮鏈表中的其他數據,之需要改變刪除連接之前鏈接點的地址而已。
	//操作的對象只有刪除鏈接點的前後兩個鏈接點。其他鏈接點不需要操作,因爲他們不像數組
	//依靠位置來決定數據的,這裏只是根據關係來操作,抽象說只是操作鏈接點指向,也就是改變關係。
	public void deleteLike(int key){
		//當前鏈接點
		Link current = first;
		//前一個鏈接點
		Link privous = first;
		
		boolean isfind = true;
		
		//判斷是否是要刪除的鏈接點
		while(current.iData != key){
			//如果還有下一個鏈接點,更換當前的兩個鏈接點
			if(current.next != null){
				//兩個鏈接點均想後移動一位,繼續判斷
				privous = current;
				current = current.next;
			}else{
				System.out.println("no find the Data");
				current = null;
				isfind = false;
				break;
			}
		}
		//判斷是否找到要刪除的數據
		if(isfind == true){
			//如果要刪除的是首個鏈接點,則將first 指向當前鏈接點的下一個點,first.next
			if(current == first){
				first = first.next;
			}else{
				//如果不是刪除首鏈接點,則將要刪除的鏈接點的前一個鏈接點的next地址指向
				//刪除點的下一個鏈接點即可。
				//注意:刪除操作只是改變的鏈接點的指向,並沒有實際刪除了數據。該數據將被回收機制處理。
				privous.next = current.next;
			}
		}else{
			System.out.println("no find the Data of delete");
		}
		
		
	}
	
	public Link displayList(){
		System.out.println("List (fist - -> last )");
		Link top = first;
		while(top!= null){
			top.displayLink();
			top = top.next;
		};
		return top;
	}
	
	public boolean isEmpty(){
		return (first == null );
	}
}

//鏈接點對象
class Link{
	public int iData;
	public double dData;
	
	//關係子段,用於存儲下一個鏈接點的位置
	public Link next;
	public Link(int id, double dd){
		this.iData = id;
		this.dData = dd;
	}
	public void displayLink(){
		System.out.println("{" + iData + "," + dData + "}");
	}
}

 

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