您的位置:首页 > 理论基础 > 数据结构算法

数据结构和算法------LinkedList的实现

2016-11-13 16:36 381 查看
package com.example;

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

/**
* LinkedList泛型类的实现
* Created by zfy on 2016/11/13.
*/

public class MyLinkedList<AnyType> implements Iterable<AnyType> {
/**
* List长度
*/
private int theSize;
/**
* 代表自从构造以来,对链表所做改变的次数add||remove调用的次数
*/
private int modCount = 0;
/**
* 头结点
*/
private Node<AnyType> beginMarker;
/**
* 未尾节点
*/
private Node<AnyType> endMarker;

/**
* 构造方法
*
* @author ZFY
*/
MyLinkedList() {
doClear();
}

/**
* 清除初始化List的接口,供外部调用
*
* @author ZFY
*/
public void clear() {
doClear();
}

/**
* 清除初始化List的私有方法 创建并连接头结点和尾节点,然后设置大小为0
*
* @author ZFY
*/
private void doClear() {
beginMarker = new Node<>(null, null, null);
endMarker = new Node<>(null, beginMarker, null);
beginMarker.next = endMarker;
theSize = 0;
modCount++;

}

/**
* 返回List大小的方法
*
* @return List大小
* @author ZFY
*/
public int size() {
return theSize;
}

/**
* 判空
*
* @return true:空;
* <p>
* false:非空;
* @author ZFY
*/
public boolean isEmpty() {
return size() == 0;
}

/**
* 添加元素到列表尾部
*
* @param x 要添加的元素.
* @return true 表示添加成功.
* @author ZFY
*/
public boolean add(AnyType x) {
add(size(), x);
return true;
}

/**
* 添加元素到指定位置
*
* @param index 添加元素的位置.
* @param x     添加的元素.
* @author ZFY
*/
public void add(int index, AnyType x) {
addBefore(getNode(index, 0, size()), x);
}

/**
* 查找指定位置处的节点的data域值
*
* @param index
* @return
* @author ZFY
*/
public AnyType get(int index) {
return getNode(index).data;
}

/**
* @param index  位置索引
* @param newVal 新值
* @return oldVal 被替换的节点的数据域
* @author ZFY
*/
public AnyType set(int index, AnyType newVal) {
Node<AnyType> p = getNode(index);
AnyType oldVal = p.data;
p.data = newVal;
return oldVal;
}

/*
* 在p指向的节点之前添加一个节点
*
* */
private void addBefore(Node<AnyType> p, AnyType x) {
/* 写法一: */
Node<AnyType> newNode = new Node<>(x, p.prev, p);
newNode.prev.next = newNode;
p.prev = newNode;
/*
* 写法二: Node<AnyType> newNode = new Node<AnyType>(x, p.prev, p);
* p.prev=newNode.prev.next = newNode;
*/
/*
* 写法三:
*  p.prev=newNode.prev.next = new Node<AnyType>(x, p.prev, p);
*/
theSize++; //调整当前List的长度
modCount++;//调整对当前List的操作次数
}

/*
* 删除index处的节点
* */
public AnyType remove(int index) {
return remove(getNode(index));
}

/*
* 删除p节点
*
* */
private AnyType remove(Node<AnyType> p) {
p.next.prev = p.prev;
p.prev.next = p.next;
theSize--;//调整当前List的长度
modCount++;//调整对当前List的操作次数

return p.data;
}

/*
* 查找索引index处的节点
* */
private Node<AnyType> getNode(int index) {
return getNode(index, 0, size() - 1);
}

/**
* 查找索引index处的节点.并且范围在lower和upper之间
*
* @param index index to search at.
* @param lower lowest valid index.
* @param upper highest valid index.
* @return internal node corresponding to index
* @throws IndexOutOfBoundsException if index is not between lower and upper,inclusive
* @author ZFY
*/
private Node<AnyType> getNode(int index, int lower, int upper) {
Node<AnyType> p;
//如果不在之情范围内,则抛出异常
if (index < lower || index > upper) {
throw new IndexOutOfBoundsException();
}
// 如果索引index小于列表长度的一般,那么从表头节点开始查找
if (index < size() / 2) {
p = beginMarker.next;
for (int i = 0; i < index; i++) {
p = p.next;
}
} else { // 如果索引index大于列表长度的一般,从表尾节点开始查找
p = endMarker;
for (int i = size(); i > index; i--) {
p = p.prev;
}
}

return p;
}

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

/**
* 描述双链表的一个节点的类
*
* @author ZFY
*/
private static class Node<AnyType> {
AnyType data; // 数据域
Node<AnyType> prev;// 前指针所指向的节点
Node<AnyType> next;// 后指针所指向的节点

Node(AnyType d, Node<AnyType> p, Node<AnyType> n) {
data = d;
prev = p;
next = n;
}
}

private class LinkedListIterator implements Iterator<AnyType> {
//表示包由调用next所返回的项的节点
private Node<AnyType> current = beginMarker.next;
//用于防止在迭代时候修改List
private int expectedModCount = modCount;
/*标记当前是否可以移除结合中的元素的标记量
每次调用 next 只能调用一次remove方法。
故,在next方法中置为true(表示当前可以移除元素),在remove方法中置为false(表示当前不能移除元素);
*/
private boolean okToRemove = false;

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

@Override
public AnyType next() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!hasNext()) {
throw new NoSuchElementException();
}

AnyType nextItem = current.data;
current = current.next;
okToRemove = true;
//            System.out.print("current:" + current.data);
return nextItem;
}

@Override
public void remove() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (!okToRemove) {
throw new IllegalStateException();
}

MyLinkedList.this.remove(current.prev);
expectedModCount++;
okToRemove = false;

}

}

}


数据结构和算法——ArrayList的实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐