### 单向链表

public interface List<T> extends Iterable<T> {
T get(int index);
int indexOf(T t);
boolean remove(T t);
T remove(int index);
void clear();
int size();
}

public class LinkedList<T> implements List<T> {

private int size;

private Node tail;

@Override
public Iterator<T> iterator() {
return new MyIterator();
}

private class MyIterator implements Iterator<T> {

public boolean hasNext() {
return current != null;
}

public T next() {
T element = current.element;
current = current.next;
return element;
}
}

/**
* 用于保存每个节点数据
*/
private class Node {
T element;
Node next;

Node(T element, Node next) {
this.element = element;
this.next = next;
}
}

/**
* 检查是否越界
*
* @param index
* @param size
*/
private void checkIndexOutOfBound(int index, int size) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index large than size");
}
}

/**
* 采用尾插法  插入新节点
*
* @param t
*/

@Override
}

@Override
public void add(int index, T t) {
checkIndexOutOfBound(index, size);
} else {
if (index == 0) {
} else {
Node prevNode = getNode(index - 1);
prevNode.next = new Node(t, prevNode.next);
size++;
}
}
}

//空链表
//首尾都指向新的节点
tail = head = new Node(t, null);
} else {
Node newNode = new Node(t, null);
//让尾部的next指向新的节点
tail.next = newNode;
//把尾部设置为新的节点
tail = newNode;
}
size++;

}

/**
* 采用头插法 插入新节点
*
* @param element
*/
if (tail == null) {
}
size++;
}

/**
* 根据索引获取节点
*
* @param index
* @return
*/
private Node getNode(int index) {
checkIndexOutOfBound(index, size - 1);
for (int i = 0; i < size; i++, current = current.next) {
if (index == i) {
return current;
}
}
return null;
}

@Override
public T get(int index) {
Node node = getNode(index);
if (node != null) {
return node.element;
}
return null;
}

@Override
public int indexOf(T t) {
for (int i = 0; i < size; i++, current = current.next) {
if (t == null && current.element == null) {
return i;
}
if (t != null && t.equals(current.element)) {
return i;
}
}
return -1;
}

/**
* 删除尾节点
*
* @return element
*/
public T removeLast() {
Node delete = tail;
if (delete == null) {
throw new NoSuchElementException();
}
//如果当前只有一个节点
} else {
//因为是单向链表，无法直接获取最后节点的上一个节点
Node pre = getNode(size - 2);
//解除引用
pre.next = null;
//重新设置tail节点
tail = pre;
}
size--;
return delete.element;
}

/**
* 删除头节点
*
* @return element
*/
public T removeFirst() {
throw new NoSuchElementException();
}
//如果当前只有一个节点
if (delete == tail) {
} else {
//解除被删除元素的next引用
delete.next = null;
}
size--;
return delete.element;
}

@Override
public boolean remove(T t) {
int index = indexOf(t);
if (index == -1) {
return false;
}
remove(index);
return true;
}

@Override
public T remove(int index) {
checkIndexOutOfBound(index, size - 1);
Node delete;
//如果删除的是头部
if (index == 0) {
return removeFirst();
} else {
Node pre = getNode(index - 1);
//待删除的节点
delete = pre.next;
//解除待删除节点和它前一个节点的引用
pre.next = delete.next;
//解除待删除节点和下一个节点的引用
delete.next = null;
}
size--;
return delete.element;
}

@Override
public void clear() {
tail = null;
size = 0;
}

@Override
public int size() {
return size;
}

@Override
public String toString() {
if (size == 0) {
return "[]";
}
StringBuilder builder = new StringBuilder();
while (current != null) {
builder.append(current.element).append("->");
current = current.next;
}
builder.append("null] tail");
return builder.toString();
}
}

### 双向链表

    private Node getNodeFast(int index) {
checkIndexOutOfBound(index, size - 1);
//如果在链表的后半部分
if (index > size / 2) {
Node current = tail;
for (int i = size - 1; i >= 0; i--, current = current.prev) {
if (index == i) {
return current;
}
}
} else {
//从头节点向尾节点方向遍历
return getNode(index);
}
return null;
}

    Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}

public class DuplexLinkedList<T> implements List<T> {

private int size;

private Node tail;

@Override
public Iterator<T> iterator() {
return new MyIterator();
}

private class MyIterator implements Iterator<T> {

public boolean hasNext() {
return current != null;
}

public T next() {
T element = current.element;
current = current.next;
return element;
}
}

/**
* 用于保存每个节点数据
*/
private class Node {
T element;
Node prev;
Node next;

Node(T element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}

@Override
public String toString() {
return element + "";
}
}

/**
* 检查是否越界
*
* @param index
* @param size
*/
private void checkIndexOutOfBound(int index, int size) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("index large than size");
}
}

/**
* 采用尾插法  插入新节点
*
* @param t
*/

@Override
}

@Override
public void add(int index, T t) {
checkIndexOutOfBound(index, size);
} else {
if (index == 0) {
} else {
Node prevNode = getNodeFast(index - 1);
prevNode.next = new Node(t, prevNode.next, prevNode);
size++;
}
}
}

//空链表
//首尾都指向新的节点
tail = head = new Node(t, null, null);
} else {
Node newNode = new Node(t, null, tail);
//让尾部的next指向新的节点
tail.next = newNode;
//把尾部设置为新的节点
tail = newNode;
}
size++;

}

/**
* 采用头插法 插入新节点
*
* @param element
*/
if (tail == null) {
}
size++;
}

/**
* 根据索引获取节点
*
* @param index
* @return
*/
private Node getNode(int index) {
checkIndexOutOfBound(index, size - 1);
for (int i = 0; i < size; i++, current = current.next) {
if (index == i) {
return current;
}
}
return null;
}

/**
* 如果需要查找的index节点在链表的后半部分，则从后往前遍历，否则按照顺序遍历
*
* @param index
* @return
*/
private Node getNodeFast(int index) {
checkIndexOutOfBound(index, size - 1);
if (index > size / 2) {
Node current = tail;
for (int i = size - 1; i >= 0; i--, current = current.prev) {
if (index == i) {
return current;
}
}
} else {
//从头节点向尾节点方向遍历
return getNode(index);
}
return null;
}

@Override
public T get(int index) {
Node node = getNodeFast(index);
if (node != null) {
return node.element;
}
return null;
}

@Override
public int indexOf(T t) {
for (int i = 0; i < size; i++, current = current.next) {
if (t == null && current.element == null) {
return i;
}
if (t != null && t.equals(current.element)) {
return i;
}
}
return -1;
}

@Override
public boolean remove(T t) {
int index = indexOf(t);
if (index == -1) {
return false;
}
remove(index);
return false;
}

@Override
public T remove(int index) {
checkIndexOutOfBound(index, size - 1);
Node delete;
//如果删除的是头部
if (index == 0) {
return removeFirst();
} else {
delete = getNodeFast(index);
Node pre = delete.prev;
Node next = delete.next;
pre.next = next;
if (next != null) {
next.prev = pre;
} else {
tail = pre;
}
delete.next = null;
delete.prev = null;
}
size--;
return delete.element;
}

/**
* 删除头结点
*
* @return
*/
public T removeFirst() {
throw new NoSuchElementException();
}
} else {
Node next = delete.next;
next.prev = null;
delete.next = null;
}
size--;
return delete.element;
}

/**
* 删除尾节点
*
* @return
*/
public T removeLast() {
if (tail == null) {
throw new NoSuchElementException();
}
Node delete = tail;
//如果只有一个元素
} else {
Node pre = delete.prev;
pre.next = null;
delete.prev = null;
tail = pre;
}
size--;
return delete.element;
}

@Override
public void clear() {
tail = null;
size = 0;
}

@Override
public String toString() {
if (size == 0) {
return "[]";
}
StringBuilder builder = new StringBuilder();
while (current != null) {
builder.append(current.element).append("->");
current = current.next;
}
builder.append("null] tail");
return builder.toString();
}

@Override
public int size() {
return size;
}
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120