标签:
package com.algorithm.list; import java.util.Iterator; import java.util.NoSuchElementException; /** * 线性数组 * * @author chao * * @param <E> */ public class MyArrayList<E> implements Iterable<E> { private static final int DEFAULT_CAPACITY = 10; private int theSize; private E[] theItems; public MyArrayList() { clear(); } public void clear() { theSize = 0; ensureCapacity(DEFAULT_CAPACITY); } public void ensureCapacity(int newCapacity) { if (newCapacity < theSize) { return; } E[] oldEs = theItems; theItems = (E[]) new Object[newCapacity]; for (int i = 0; i < theSize; i++) { theItems[i] = oldEs[i]; } } public int size() { return theSize; } public void trimToSize() { ensureCapacity(size()); } public boolean isEmpty() { return theSize == 0; } public E get(int index) { if (index < 0 || index > theSize) { throw new ArrayIndexOutOfBoundsException(); } return theItems[index]; } public E set(int index, E newval) { if (index < 0 || index >= theSize) { throw new ArrayIndexOutOfBoundsException(); } E oldvallue = theItems[index]; theItems[index] = newval; return oldvallue; } @Override public Iterator<E> iterator() { return new ArraylistIterator(); } public boolean add(E value) { add(size(), value); return true; } public void add(int index, E value) { if (theItems.length == theSize) { ensureCapacity(theSize * 2 + 1); } for (int i = theSize; i > index; i--) { theItems[i] = theItems[i - 1]; } theItems[index] = value; theSize++; } public E remove(int index) { E oldvalue = theItems[index]; for (int i = index; i < theSize; i++) { theItems[i] = theItems[i + 1]; } theSize--; return oldvalue; } class ArraylistIterator<E> implements Iterator<E> { private int current = 0; @Override public boolean hasNext() { return current < theSize; } @Override public E next() { if (!hasNext()) { throw new NoSuchElementException(); } return (E) theItems[current++]; } @Override public void remove() { MyArrayList.this.remove(--current); } } }2、链表
package com.algorithm.list; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; /** * 链表数据结构 * * @author chao * * @param <E> */ public class MyLinkedList<E> implements Iterable<E> { private class Node<T> { public Node() { } public Node(T ele, Node next, Node pre) { this.ele = ele; this.next = next; this.pre = pre; } T ele; Node next; Node pre; } private Node<E> head; private Node<E> tail; private int size; private int modcount = 0; public MyLinkedList() { clear(); } public void clear() { head = new Node<E>(null, null, null); tail = new Node<E>(null, head, null); head.next = tail; size = 0; modcount++; } @Override public Iterator<E> iterator() { return new LinkedListIterator(); } private class LinkedListIterator implements Iterator<E> { private Node<E> current = head.next; private boolean okToremove = false; private int expectedModcount = modcount; @Override public boolean hasNext() { return current != tail; } @Override public E next() { if (modcount != expectedModcount) { throw new ConcurrentModificationException(); } else if (!hasNext()) { throw new NoSuchElementException(); } current = current.next; okToremove = true; return (E) current.pre.ele; } @Override public void remove() { if (!okToremove) { throw new IllegalStateException(); } else if (modcount != expectedModcount) { throw new ConcurrentModificationException(); } MyLinkedList.this.remove(current.pre); okToremove = false; expectedModcount++; } } public boolean isempty() { return size == 0; } public int size() { return size; } private Node getNode(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } Node<E> p = null; if (index < size / 2) { p = head.next; for (int i = 0; i < index; i++) p = p.next; } else { p = tail; for (int i = size; i > index; i--) p = p.pre; } return p; } public E get(int index) { return (E) getNode(index).ele; } private E remove(Node node) { node.next.pre = node.pre; node.pre.next = node.next; size--; modcount++; return (E) node.ele; } public E remove(int index) { return remove(getNode(index)); } public void add(int index, E ele) { addBefore(getNode(index), ele); } private void addBefore(Node node, E ele) { Node newnode = new Node<E>(ele, node.pre, node); node.pre.next = newnode; node.pre = newnode; size++; modcount++; } public void add(E ele) { add(size, ele); } }
标签:
原文地址:http://blog.csdn.net/robertcpp/article/details/51582292