LinledList 底层简单实现

LinkdeList底层是由链表实现的,使用链表会有较高的增删效率,但是查找效率会比较低。

使用泛型会有利于方法的重用性。

package Conllection;

/**
 * LinkedList 链表节点
 * 
 * @author zhaoy
 *
 */
public class Node {
	Node previous;// 上一个节点
	Node next;// 下一个节点
	Object element;// 数据

	public Node(Object element) {
		super();
		this.element = element;
	}

}
package Conllection;

/**
 * LinledList 简单实现
 * 
 * @author zhaoy
 *
 */
public class LinkedList<T> {
	private int size;
	private Node first;
	private Node last;

	/**
	 * 添加节点方法
	 * 
	 * @param index
	 * @param obj
	 */
	private void add(int index, T obj) {
		CheckRange(index);
		Node nowNode = new Node(obj);
		if (size == 0) {// 创建第一个
			nowNode.next = null;
			nowNode.previous = null;
			first = nowNode;
			last = nowNode;
		} else if (index == 0) {// 头插
			nowNode.next = first;
			first.previous = nowNode;
			first = nowNode;
		} else if (index != size) {// 中间插
			Node temp = getNode(index);
			Node temp1 = temp.previous;
			temp1.next = nowNode;
			nowNode.next = temp;
			temp.previous = nowNode;
			nowNode.previous = temp1;
		} else if (index == size) {// 尾插
			last.next = nowNode;
			nowNode.previous = last;
			nowNode.next = null;
			last = nowNode;
		}
		size++;
	}
	/**
	 * 添加节点方法
	 * @param obj
	 */
	public void add(T obj) {
		Node nowNode = new Node(obj);
		if (size == 0) {// 创建第一个
			nowNode.next = null;
			nowNode.previous = null;
			first = nowNode;
			last = nowNode;
		}else {
			last.next = nowNode;
			nowNode.previous = last;
			nowNode.next = null;
			last = nowNode;
		}
		size++;
	}
	/**
	 * 根据索引值移除节点
	 * @param index
	 */
	public void remove(int index) {
		CheckRange(index);
		Node temp = getNode(index);
		if(index==0) {//删头
			first=temp.next;
			temp.next=null;
			temp.previous=null;
		}else if(index==(size-1)) {//删尾
			last=temp.previous;
			temp.previous=null;
			last.next=null;
		}else {//删中间
			temp.previous.next=temp.next;
			temp.next.previous=temp.previous;
			temp.next=null;
			temp.previous=null;
		}
		size--;
	}
	/**
	 * 根据索引值获取数据
	 * @param index
	 * @return
	 */
	public Object get(int index) {
		CheckRange(index);
		Node temp = getNode(index);
		return temp.element;
	}
	/**
	 * 根据索引位置找到节点
	 * 
	 * @param index
	 * @return
	 */
	private Node getNode(int index) {
		Node temp = first;
		for (int i = 0; i < index; i++) {
			temp = temp.next;
		}
		return temp;
	}

	/**
	 * 检验索引值
	 * 
	 * @param index
	 */
	public void CheckRange(int index) {
		System.out.println("size:"+size);
		if (index < 0 || index > size-1) {
			throw new RuntimeException("索引不合法:" + index);
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		Node temp = first;
		while (temp != null) {
			sb.append(temp.element + ",");
			temp = temp.next;
		}
		sb.setCharAt(sb.length() - 1, ']');
		return sb.toString();
	}

	/**
	 * 主方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<String>();
		list.add("aa");
		list.add("bb");
		list.add("cc");
		list.add("dd");
		list.add(0, "dd");
		System.out.println(list);
		list.remove(0);
		System.out.println(list);
		System.out.println(list.get(0));
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值