1. 程式人生 > >java 實現基礎的連結串列 增刪改查 虛擬頭結點

java 實現基礎的連結串列 增刪改查 虛擬頭結點

  • 上機課時間有剩,實現了個連結串列,但想做到盡善盡美,以後有更多的做法和能完善的功能會在部落格裡面分享,大家一起交流學習~~
  • 有測試用例,重寫了toString方法方便測試
  • 維護了size變數
  • 細節,考慮了被刪除結點是否被java回收機制回收
  • 採用了虛擬頭結點,在刪除和新增操作的時候是需要找到待刪或待增結點的上一個結點的,這時虛擬頭結點的作用就體現出來了,因為head是沒有上一個節點的,虛擬頭結點可以統一增刪邏輯
  • 使用範型類
  • 待完善~

 

 

/**
 * 
 * @author 葉葉
 *
 * @param 連結串列 增刪改查
 */
public class LinkedList<E> {
	
	public class ListNode{
		E e;
		ListNode next;
		public ListNode(){
			this(null,null);
		}
		
		public ListNode(E e){
			this.e = e;
		}
		
		public ListNode(E e, ListNode next){
			this.e = e;
			this.next = next;
		}
		
		public String toString() {
			return e.toString();
		}
		
	}
	private ListNode dummyHead;
	private int size;
	
	public LinkedList(){
		dummyHead = new ListNode();
		size=0;
	}
	
	public int getSize(){
		return size;
	}
	
	public boolean isEmpty(){
		return size==0;
	}
	
	public void add(int index, E e){
		if(index<0 || index>size)
			throw new IllegalArgumentException("add fail");
		ListNode pre = dummyHead;
		for(int i=0;i<index;i++){
			pre = pre.next;
		}
		ListNode x = new ListNode(e, pre.next);
		pre.next = x;
		size++;
	}
	
	public void addFirst(E e){
		add(0,e);
	}
	
	public void addLast(E e){
		add(size,e);
	}	
	
	public E get(int index){
		if(index<0 || index>=size)
			throw new IllegalArgumentException("get fail");
		ListNode cur = dummyHead.next;
		for(int i=0;i<index;i++){
			cur = cur.next;
		}
		return cur.e;
	}
	
	public E getFirst(){
		return get(0);
	}

	public E getLast(){
		return get(size - 1);
	}
	
	public void remove(int index){
		if(index<0 || index>=size)
			throw new IllegalArgumentException("remove fail");
		ListNode pre = dummyHead;
		for(int i=0;i<index;i++){
			pre = pre.next;
		}
		//ListNode cur = pre.next;
		pre.next = pre.next.next;//其實已經刪除了。但被刪除但節點沒有被清除
		//cur.next = null;
		size--;
	}
	
	public void removeFirst(){
		remove(0);
	}
	
	public void removeLast(){
		remove(size-1);
	}
	
	public void set(int index, E e){
		if(index<0 || index>=size)
			throw new IllegalArgumentException("set fail");
		ListNode cur = dummyHead.next;
		for(int i=0;i<index;i++){
			cur = cur.next;
		}
		cur.e = e;
	}
	
	public void setFirst(E e){
		set(0,e);
	}
	
	public void setLast(E e){
		set(size-1,e);
	}
	
	
	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		ListNode cur = dummyHead.next;
		while(cur!=null){
			res.append(cur.toString()+"->");
			cur = cur.next;
		}
		res.append("null");
		return res.toString();
	}

	public static void main(String[] args) {
		LinkedList<Integer> list = new LinkedList<>();
		list.addFirst(10);
		list.addFirst(12);
		list.addFirst(8);
		System.out.println(list.toString()+"   size:"+list.getSize());
		list.remove(1);
		System.out.println(list.toString()+"   size:"+list.getSize());
		list.addLast(100);
		System.out.println(list.toString()+"   size:"+list.getSize());
		list.add(2,101);
		System.out.println(list.toString()+"   size:"+list.getSize());
		list.setLast(150);
		System.out.println(list.toString()+"   size:"+list.getSize());
		System.out.println("isEmpty:"+list.isEmpty()+"   size:"+list.getSize());

	}

}