Java容器---MyLinkedList

自定義容器LinkedList,實現部分功能

    1.創建Node類,存放節點信息

/**
 * 每個節點內三個對象表示
 * previous 前一個節點位置
 * obj 當前節點數據信息
 * next 下一個節點位置
 * @author Administrator
 *
 */
public class Node {

	 Node previous;
	 Object obj;
	 Node next;
	
	public Node(){
		
	}
	
	public Node(Node previous,Object obj,Node next){
		super();
		this.previous = previous;
		this.obj = obj;
		this.next = next;
	}

	public Object getPrevious() {
		return previous;
	}

	public void setPrevious(Node previous) {
		this.previous = previous;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public Object getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}
	
}
    2. 實現LinkedList
/**
 * 編程實現LinkedList
 * 
 * @author Administrator
 *
 */
public class MyLinkedList {

	private Node first;
	private Node last;
	private int size;

	public MyLinkedList() {

	}

	public int size() {
		return size;
	}

	// 在鏈表尾部增加指定對象
	public void add(Object obj) {
		Node n = new Node();
		// 鏈表頭是否爲空
		if (first == null) {

			n.setPrevious(null);
			n.setObj(obj);
			n.setNext(null);
			first = n;
			last = n;

		} else {
			// 向last節點後增加新的節點
			n.setPrevious(last);
			n.setObj(obj);
			n.setNext(null);

			last.setNext(n);

			last = n;

		}
		size++;
	}

	// 向指定索引位置增加對象,新節點的對應關係爲:previous指向索引位置節點的前一個節點,next指向索引位置節點
	public void add(int index, Object obj) {
		rangeCheck(index);
		Node temp = node(index);

		Node up = temp.previous;
		Node newNode = new Node();
		newNode.obj = obj;

		if (temp != null) {

			newNode.previous = up;
			up.next = newNode;

			newNode.next = temp;
			temp.previous = newNode;
			size++;
		}

	}

	public void set(int index, Object obj) {
		rangeCheck(index);
		Node temp = node(index);

		if (temp != null) {
			temp.obj = obj;
		}
	}

	public Object get(int index) {
		rangeCheck(index);
		Node temp = node(index);
		if (temp != null) {
			return temp.obj;
		} else {
			return null;
		}

	}

	// 移除索引位置節點,1 2 3,將1的下一個設置爲3,3的上一個設置爲1,實現雙向,移除2位置
	public void remove(int index) {
		Node temp = node(index);
		if (temp != null) {
			// 索引位置節點的上一個節點
			Node up = temp.previous;
			// 索引位置節點的下一個節點
			Node down = temp.next;
			// 上一個節點的next節點位置設置爲當前節點的下一個,同理操作下一個節點,實現移除索引位置節點
			up.next = down;
			down.previous = up;
		}
		size--;
	}

	public void clear() {
		Node temp = new Node();
		if (first != null) {
			temp = first;
			for (int i = 0; i < size - 1; i++) {
				temp.obj = null;
				temp = temp.next;
				size--;

			}

		}

	}

	public void rangeCheck(int index) {
		if (index < 0 || index > size - 1) {
			try {
				throw new Exception();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 尋找節點
	public Node node(int index) {
		rangeCheck(index);
		Node temp = null;
		
		if (first != null) {
			
			//size=50;[0,49],尋找第2個和尋找第48個節點的方法優化分析
			//一個簡單的優化算法:將index與size/2(szie>>1)進行比較,如果小於則從0開始遍歷,如果大於則從49開始向前遍歷
			if (index<(size>>1)) {
				temp = first;
				for (int i = 0; i < index; i++) {
					temp = temp.next;
				}
			}else{
				temp = last;
				for (int i = size-1; i > index; i--) {
					temp = temp.previous;
				}
			}
			
		}
		return temp;
	}

	public static void main(String[] args) {
		MyLinkedList list = new MyLinkedList();
		list.add("aaa");
		list.add(111);
		list.add(555);
		list.add(1, "ddd");
		list.set(1, 444);
		
		
		System.out.println(list.get(1));
//		for (int i = 0; i < list.size(); i++) {
//			System.out.println(list.get(i));
//		}

	}

}

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