您的位置:首页 > 其它

简单实现MyLinkedList

2018-01-05 18:42 405 查看
package cn.limbo.java_structure;

import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* 1:首先包含到两端的链,表的大小以及其他方法
* 2:Node类,可以设计为一个私有的嵌套类(内部类).一个节点包含数据以及
* 到前一个节点的链和到下一个节点的链,必须要有构造方法
* 3:LinkedListIterator类,该类抽象了位置的概念,是一个私有类,并且实现了
* 接口Iterator,并且提供了next hasNext remove方法.
* 主要解决前后是否有节点的问题
* 1:区别节点和当前元素
* 2:链表结构基本操作都是对引用的操作
*/
public class MyLinkedList<T> implements Iterable<T> {

private int theSize;//表示当前数组的长度

private int modCount = 0;

private Node<T> beginMarker;

private Node<T> endMarker;

public MyLinkedList() {
clear();
}

//不同于MyArrayList

/**
* change the size if this collection to zero
*/
private void clear() {
beginMarker = new Node<>(null, null, null);
endMarker = new Node<>(null, beginMarker, null);
beginMarker.next = endMarker;

theSize = 0;
modCount++;
}

public int size() {
return theSize;
}

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

public boolean add(T t) {
add(size(), t);
return true;
}

private void add(int idx, T t) {
addBefore(getNode(idx), t);
}

public T get(int idx) {
return getNode(idx).data;
}

public T set(int idx, T newVal) {
Node<T> p = getNode(idx);
T oldVal = p.data;
p.data = newVal;
return oldVal;
}

public T remove(int idx) {
return remove(getNode(idx));
}

private void addBefore(Node<T> p, T x) {
//向一个双链表中的插入操作到p点的位置
Node<T> newNode = new Node<T>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}

/**
* 从一个双链表中删除由p指定的节点
*/
private T remove(Node<T> p) {
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount--;
return p.data;
}

/**
* Get the Node at position idx,which must range from 0 to size().
*/
private Node<T> getNode(int idx) {
Node<T> p;
if (idx < 0 || idx > size()) {
throw new IndexOutOfBoundsException();
}
if (idx < size() / 2) {
//从头开始节点开始想回遍历
p = beginMarker.next;
for (int i = 0; i < idx; i++) {
p = p.next;
}
} else {
p = endMarker;
for (int i = size(); i > idx; i--) {
p = p.prev;
}
}
return p;
}

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

//私有节点类
private static class Node<T> {

public T data;
public Node<T> prev;
public Node<T> next;

public Node(T t, Node<T> p, Node<T> n) {
data = t;
prev = p;
next = n;
}
}

private class LinkedListIterator implements Iterator<T> {

private Node<T> current = beginMarker.next;

private int expectedModCount = modCount;

private boolean okToRemove = false;

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

public T next() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}
T nextItem = current.data;
current = current.next;
okToRemove = true;
return nextItem;
}

public void remove() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!okToRemove ) {
throw new IllegalStateException();
}
MyLinkedList.this.remove(current.prev);
okToRemove=false;
expectedModCount++;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: