java實現鏈表操作

java實現簡單鏈表

線性表接口的抽象

package com.fsc.mylinkedlist;

/**
 * 線性表操作接口
 * @author fsc
 *
 * @param <T>
 */
public interface ListInterface<T> {
	/**
	 * 向表尾添加元素
	 */
	public boolean add(T newEntry);
	/**
	 * 向表的指定位置添加元素
	 */
	public boolean add(T newEntry, int givenPosition);
	/**
	 * 移除指定位置元素
	 */
	public T remove(int givenPosition);
	/**
	 * 用新元素替換指定位置元素
	 */
	public T replace(T newEntry, int givenPosition);
	/**
	 * 獲取指定位置元素
	 */
	public T getEntry(int givenPosition);
	/**
	 * 表中是否包含指定元素
	 */
	public boolean contains(T anEntry);
	/**
	 * 表長
	 */
	public int getLength();
	/**
	 * 表是否爲空
	 */
	public boolean isEmpty();
	/**
	 * 展示表
	 */
	public void display();
}


爲了插入和刪除代碼的一致性,引入了哨兵節點(也就是下面代碼中出現的firstNode)。

下標的選擇和數組的規則相同,都是從0開始。爲了滿足下標的規則,在代碼中相應作出了調整,下面是代碼

package com.fsc.mylinkedlist;

public class LList<T> implements ListInterface<T> {
	private Node firstNode;
	private Node lastNode;
	private int length;

	public LList() {
		clear();
	}

	/**
	 * 將鏈表置爲初始狀態
	 */
	public final void clear() {
		// firstNode爲哨兵節點
		firstNode = new Node(null);
		lastNode = null;
		length = 0;
	}

	/**
	 * 向鏈表尾部插入數據域
	 */
	@Override
	public boolean add(T newEntry) {
		return add(newEntry, length);
	}

	/**
	 * 向鏈表指定位置插入數據 position爲0時代表向鏈表頭插入元素(第一個元素)
	 * 
	 * @param newEntry
	 * @param position
	 * @return
	 */
	public boolean add(T newEntry, int position) {
		if (newEntry == null) {
			throw new IllegalArgumentException("數據域不能爲null");
		}
		if (position < 0 || position > length) {
			throw new IllegalArgumentException("插入的位置不合法");
		}
		
		Node nextNode = new Node(newEntry);
		
		//向表尾插入的特殊情況處理表尾引用
		if(position == length){
			if(length == 0){
				firstNode.next = nextNode;
				lastNode = nextNode;
			}else{
				lastNode.next = nextNode;
				lastNode = nextNode;
			}
		}else{
			// 向currentNode後插入newNode
			Node currentNode = getNodeAt(position);
			// 插入
			nextNode.next = currentNode.next;
			currentNode.next = nextNode;
		}
		
		
		length++;
		return true;
	}
	
	/**
	 * 刪除指定位置的節點  傳入0代表刪除表頭節點
	 * 鏈表是從0開始計數
	 */
	@Override
	public T remove(int givenPosition) {
		
		if (givenPosition < 0 || givenPosition >= length) {
			throw new IllegalArgumentException("刪除的位置不合法");
		}
		Node currentNode = getNodeAt(givenPosition);
		Node result = currentNode.next;
		currentNode.next = currentNode.next.next;
		return result.data;
	}
	
	@Override
	public T replace(T newEntry, int givenPosition) {
		if (newEntry == null) {
			throw new IllegalArgumentException("數據域不能爲null");
		}
		
		if (givenPosition < 0 || givenPosition >= length) {
			throw new IllegalArgumentException("替換的位置不合法");
		}
		
		//要替換節點之前的節點
		Node currentNode = getNodeAt(givenPosition);
		T result = currentNode.next.data;
		currentNode.next.data = newEntry;
		return result;
	}
	
	@Override
	public T getEntry(int givenPosition) {
		
		if (givenPosition < 0 || givenPosition >= length) {
			throw new IllegalArgumentException("傳入的位置不合法");
		}
		Node currentNode = getNodeAt(givenPosition);
		return currentNode.next.data;
	}


	@Override
	public boolean contains(T anEntry) {
		if (anEntry == null) {
			throw new IllegalArgumentException("數據域不能爲null");
		}
		
		boolean result = false;
		Node currentNode = firstNode.next;
		while(currentNode != null){
			T data = currentNode.data;
			if(anEntry.equals(data)){
				result = true;
				break;
			}
			currentNode = currentNode.next;
		}
		return result;
	}

	/**
	 * 從鏈表的頭節點開始打印鏈表的數據域
	 */
	@Override
	public void display() {
		StringBuilder sb = new StringBuilder();
		Node currentNode = firstNode.next;
		while (currentNode != null) {
			sb.append(currentNode.data);
			currentNode = currentNode.next;
			if (currentNode != null) {
				sb.append("->");
			}
		}

		System.out.println(sb.toString());
	}
	
	

	@Override
	public int getLength() {
		return length;
	}

	@Override
	public boolean isEmpty() {
		return length == 0;
	}




	/**
	 * 獲取指定位置的節點 節點的計數從0開始,getNodeAt(0)代表獲取firstNode(哨兵節點)
	 * 
	 * @param givenPosition
	 * @return 若要找的位置不存在節點返回null
	 */
	private Node getNodeAt(int givenPosition) {
		// 從頭節點向後找
		Node currentNode = firstNode;
		for (int i = 0; i <= length; ++i) {
			if (givenPosition == i) {
				return currentNode;
			}
			currentNode = currentNode.next;
		}
		return null;
	}

	/**
	 * 節點類
	 * 
	 * @author fsc
	 * 
	 */
	private class Node {
		private T data;
		private Node next;

		public Node(T data) {
			this(data, null);
		}

		public Node(T data, Node next) {
			this.data = data;
			this.next = next;
		}

	}

}




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