Java实现List

ArrayList由数组实现:

public class myArrayList {
	private Object[] element;
	private int size;

	public myArrayList(int initCapacity) {
		if (initCapacity < 0) {
			throw new IllegalArgumentException("Illegal Capacity:" + initCapacity);
		}
		element = new Object[initCapacity];
	}

	public myArrayList() {
		this(10);
	}

	public boolean add(Object obj) {
		// 数组扩容
		if (size >= element.length) {
			Object[] biggerlist = new Object[size * 2 + 1];
			System.arraycopy(element, 0, biggerlist, 0, element.length);
			element = biggerlist;
		}
		element[size++] = obj;
		return true;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	public Object get(int index) throws Exception {
		rangeChack(index);
		return element[index];
	}

	public boolean remove(int index) throws Exception {
		rangeChack(index);
		int numMoved = size - index - 1;
		if (numMoved > 0) {
			System.arraycopy(element, index + 1, element, index, numMoved);
		}
		return true;
	}

	private void rangeChack(int index) throws Exception {
		if (index < 0 || index >= size) {
			throw new Exception("Out of index");
		}
	}

	public static void main(String[] args) throws Exception {
		myArrayList list = new myArrayList();
		list.add("www");
		System.out.println(list.isEmpty());
		System.out.println(list.get(0));
	}
}

LinekdList(双向链表)由节点类连接实现

Node类:

//用来表示一个节点
public class Node {
	public Node() {
		super();
		this.previous = null;
		this.obj = null;
		this.next = null;
	}

	Node previous;
	Object obj;
	Node next;

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

	public Node 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 Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}
}

LinekdList(类):

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

	public void add(Object obj) {
		// 如果链表为空,last==first==obj
		Node node = new Node();
		if (first == null) {
			node.setPrevious(null);
			node.setObj(obj);
			node.setNext(null);

			first = node;
			last = node;
		} else {
			// 链表不为空,在last后增加节点
			node.setPrevious(last);
			node.setObj(obj);
			node.setNext(null);

			last.setNext(node);
			last = node;
		}
		size++;
	}

	public int size() {
		return size;
	}

	public Object get(int index) throws Exception {
		rangeCheck(index);
		Node tmp = first;
		if (first == null) {
			return null;
		} else {
			tmp = first;
			for (int i = 0; i < index; i++) {
				tmp = tmp.next;
			}
		}
		return tmp.obj;
	}

	public void insert(int index, Object obj) throws Exception {
		Node tmp = node(index);
		Node newNode = new Node();
		newNode.obj = obj;

		if (tmp != null) {
			Node pre = tmp.previous;

			pre.next = newNode;
			newNode.previous = pre;

			newNode.next = tmp;
			tmp.previous = newNode;
		}
	}

	public void remove(int index) throws Exception {
		Node tmp = node(index);
		Node pre = tmp.previous;
		Node next = tmp.next;

		pre.next = next;
		next.previous = pre;
		size--;
	}

	private Node node(int index) throws Exception {
		rangeCheck(index);
		Node tmp = first;
		if (first == null) {
			return null;
		} else {
			tmp = first;
			for (int i = 0; i < index; i++) {
				tmp = tmp.next;
			}
		}
		return tmp;
	}

	private void rangeCheck(int index) throws Exception {
		if (index < 0 || index >= size) {
			throw new Exception("Out of index");
		}
	}

	public static void main(String[] args) throws Exception {
		myLInkedList list = new myLInkedList();
		list.add("qq");
		list.add("dd");
		list.add("ff");
		list.add("hh");
		System.out.println(list.get(2));
		list.remove(2);
		System.out.println(list.get(2));
	}
}
发布了80 篇原创文章 · 获赞 68 · 访问量 7576

猜你喜欢

转载自blog.csdn.net/weixin_44048823/article/details/99203588