1-3、LinkedList的实现

废话少说,先上代码

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

public class MyLinkedList<E> {

// List的元素个数
private int theSize;
// 修改次数,用于使用迭代器的时候进行比对,保证线程安全
private int modCount = 0;
// 链表头
private Node<E> beginMarker;
// 链表尾
private Node<E> endMarker;

public MyLinkedList() {
clear();
}

/**
* 将链表初始化为空链表,只包含表头和表尾
*/
private void clear() {
beginMarker = new Node<E>(null, null, null);
endMarker = new Node<E>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;
}

public int size() {
return theSize;
}

/**
* 判断是否为空
*
* @return
*/
public boolean isEmpty() {
return theSize == 0;
}

/**
* 向链表表尾添加元素
*
* @param e
* @return
*/
public boolean add(E e) {
add(theSize, e);
return true;
}

/**
* 向指定位置插入元素
*
* @param index
* @param e
*/
public void add(int index, E e) {
addBefore(getNode(index), e);
}

/**
* 获得指定索引处的元素
*
* @param index
* @return
*/
public E get(int index) {
return getNode(index).data;
}

/**
* 将指定索引处的元素删除,并将删除的元素返回
*
* @param index
* @return
*/
public E remove(int index) {
return remove(getNode(index));
}

/**
* 删除改元素并返回
*
* @param p
* @return
*/
private E remove(Node<E> p) {
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;
modCount++;
return p.data;
}

/**
* 获得指定索引处的节点
*
* @param index
* @return
*/
private Node<E> getNode(int index) {
Node<E> p;
if (index < 0 || index > theSize)
throw new IndexOutOfBoundsException();
if (index < theSize / 2) {
p = beginMarker.next;
for (int i = 0; i < index; i++) {
p = p.next;
}
} else {
p = endMarker;
for (int i = theSize; i > index; i--) {
p = p.prev;
}
}
return p;
}

/**
* 在指定节点前插入一个节点元素
*
* @param p
* @param e
*/
private void addBefore(Node<E> p, E e) {
Node<E> newNode = new Node<E>(e, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
theSize++;
modCount++;
}

/**
* 获得该List的迭代器
*
* @return
*/
public Iterator<E> iterator() {
return new LinkedListIterator();
}

/**
* 嵌套类,表示一个节点
*
* @author zyf
*
* @param <E>
*/
private static class Node<E> {
private E data;
private Node<E> prev;
private Node<E> next;

public Node(E data, Node<E> prev, Node<E> next) {
super();
this.data = data;
this.prev = prev;
this.next = next;
}

}

/**
* 迭代器内部类,实现了Iterator接口,因此可以进行迭代操作
*
* @author zyf
*
*/
private class LinkedListIterator implements Iterator<E> {
private Node<E> current = beginMarker.next;
private int expectedModCount = modCount;
private boolean okToRemove = false;

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

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

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



该实现是基于这个模型的
[img]http://dl.iteye.com/upload/attachment/600120/3e984048-07be-3e84-beaa-b5ba94233444.jpg[/img]
一个表头,一个表尾,而且是双向的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值