1. 程式人生 > >java 連結串列實現

java 連結串列實現

這裡我分享下自己用Java實現的連結串列 :

首先給出一個連結串列模型:



第一步:  建立空連結串列   



第二步:建立頭節點



第三部:建立尾節點



到此為止 一個比較有完整意義的連結串列已經構造出 

增加節點



刪除節點:


總結:我們可以看到連結串列在增加節點和刪除節點的時候,只需要改變next指標的指向,就能達到我們想要的操作目的。而對於陣列來說,增加和刪除一個元素都要將目標刪除元素後面的每個元素往前移動一位。 如果增刪很頻繁的話,連結串列會比陣列會更快。對於遍歷操作而言,連結串列需要通過next指標遍歷每一個元素,相對於陣列遍歷直接訪問下標來說,會比較慢。  所以在增刪比較頻繁的時候我們應該考慮是否使用連結串列這種資料結構。

貼出程式碼:

package test;

public class LinkList<T> { 
	
	private Node<T> head; //連結串列的頭節點
	private Node<T> tail; //連結串列的尾節點
	
	/**
	 * 構造一個空連結串列
	 */
	public LinkList() { 
		head = tail = null;    
	}  
	
	/**
	 * 連結串列內部的節點類
	 */
	private static class Node<T> {  
		T data;//節點的資料
		Node<T> next;//該節點的指向下一個節點的指標
		
		Node(T data) { 
		    this.data = data;  
		    this.next = null;   
		}  
  
	}  
	
	public void addHead(T point) {//為空連結串列增加頭結點    
		this.head = new Node<T>(point);  
		if(tail == null) {  
		    tail = head;  
		}  
	}  
	
	public void addTail(T point){//為連結串列增加尾節點  
	    tail = new Node<T>(point);  
	    head.next = tail;  
	}  
	
	public void insert(T point) {
		if (this.head == null) {
			addHead(point);
			
		} else if (this.tail == this.head) {
			addTail(point);
			
		} else {
	    	Node<T> preNext = head.next;  
			@SuppressWarnings({ "unchecked", "rawtypes" })
			Node<T> newNode = new Node(point);  
	        preNext = head.next;  
	        this.head.next = newNode;  
	        newNode.next = preNext;  
		}
	     
	}  
	
	public void printLinkList() {    //列印連結串列
		Node<T> curr = this.head;  
		if (isEmpty()) {  
			try {
				throw new Exception("linklist is empty");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		while(curr != null){  
			System.out.print(curr.data+" ");      
			curr = curr.next;  
		}  	
	}  
	
	public void delete(T data){//刪除某一節點
		Node<T> curr = head, prev = null;
		boolean suc = false;//是否刪除成功標誌
		while (curr != null) {
			if (curr.data.equals(data)) {
				//判斷是什麼節點
				if (curr == head) {   //如果刪除的是頭節點
					System.out.println('\n'+"delete head node");
					head = curr.next;
					suc = true;
					return;
				}
				if (curr == tail) { //如果刪除的是尾節點
					System.out.println('\n'+"delete tail node");
					tail = prev;
					prev.next = null;
					suc = true;
					return;
				} else {//如果刪除的是中間節點(即非頭節點或非尾節點)
					System.out.println('\n'+"delete center node");
					prev.next = curr.next;
					suc = true;
					return;
				}
			}

			prev = curr;
			curr = curr.next;	
		}
		
		if(suc == false) {
			System.out.println('\n'+"沒有這個資料");
		}	
	
	}
	
	public boolean isEmpty(){//判斷連結串列是否為空
		return this.head == null || this.tail == null;
    } 

    public static void main(String[] args) {  
		LinkList<Integer> mylist = new LinkList<Integer>();//構造一個空連結串列  
		mylist.insert(5);  
		mylist.insert(6);  
		mylist.insert(7);  
		mylist.insert(3);  
		mylist.printLinkList();  
		mylist.delete(1);
		mylist.printLinkList();  
		mylist.delete(5);
		mylist.printLinkList();
		mylist.delete(6);
		mylist.printLinkList();  
    }  
  
} 




輸出結果:

5 3 7 6 
沒有這個資料
5 3 7 6 
delete head node
3 7 6 
delete tail node
3 7