模拟LinkedList底层实现的示例

 这个示例的目的是模拟链表的实现方式,与ArrayList的底层实现相比查询慢,增删改快的优缺点。

package test;

public class DesignLinkedList {
   
	private int size;
	private Node first;
	private Node last;


	public void add(Object obj) {
		Node n = new Node();
		if(first==null) {
			
			n.setPrevious(null);
			n.setSelfObj(obj);
			n.setNext(null);
			
			first =n;
			last =n;
		}else {
			//直接往最后一个节点处添加
			n.setPrevious(last);
			n.setSelfObj(obj);
			n.setNext(null);
			
			last.setNext(n);
			last = n;
		}
		
		size++;
		
	}
	
	public int size() {
		return size;
	}
	
	
	public Object get(int index){
		//越界处理
		rangeCheck(index);
		Node temp=Node(index);
		
		return temp.getSelfObj();
	}
	
	public Node Node(int index) {
		rangeCheck(index);
		Node temp =null;
		if(first !=null) {
			temp = first;
			for(int i=0; i<index;i++) {
				temp=temp.getNext();
			}
	}
		return temp;
	}
	
	private void rangeCheck(int index)  {
		if (index < 0 || index >= size) {
			try {
				throw new Exception();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void remove(int index) {
		rangeCheck(index);
		
		Node temp=Node(index);
		
		if(temp!=null) {
			Node up=temp.getPrevious();
			Node down = temp.getNext();
			
			if(down !=null) {
				down.setPrevious(up);
			}
			up.setNext(down);
			size--;
		}
		
	}
	
	
	public void add(int index, Object obj) {
		rangeCheck(index);
		//先获取当前位置的节点
		Node currentNode = Node(index);
		//創建新節點
		Node temp = new Node();
		temp.setSelfObj(obj);
		
		if(currentNode!=null){		
			Node up= currentNode.getPrevious();
			up.setNext(temp);
			temp.setNext(currentNode);
			size++;
		}
		
	}
	
	public static void main(String[] arg) throws Exception {
		DesignLinkedList dll= new DesignLinkedList();
		//当只有"aaa"一个元素被添加的话,first 和 last的都是aaa, 节点的previous 和next的对象都是null
		dll.add("aaa"); 
		//第二个元素被添加之后,将当前last节点的next对象设置为ccc,然后然后将当前对象设置为last对象。		
		dll.add("cccc");
		dll.add("bbb");
		System.out.println(dll.size());
		System.out.println(dll.get(0));
		System.out.println(dll.get(1));
		dll.remove(2);
		System.out.println(dll.size);
		dll.add(1,"eee");
		System.out.println(dll.size);
		System.out.println(dll.get(1));
	}
	
	
}
package test;

//用来定义链表中的一个节点
public class Node {
	
	private Node previous;
	private Object selfObj;
	private Node next;
	
	public Node() {
		super();
	}
	public Node(Node previous, Object selfObj, Node next) {
		super();
		this.previous = previous;
		this.selfObj = selfObj;
		this.next = next;
	}
	public Node getPrevious() {
		return previous;
	}
	public void setPrevious(Node previous) {
		this.previous = previous;
	}
	public Object getSelfObj() {
		return selfObj;
	}
	public void setSelfObj(Object selfObj) {
		this.selfObj = selfObj;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}

	
	
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值