简单模拟LinkedList集合

MyLinkedList类:简单模拟,没有实现List接口,只是模拟了几个常用的方法。

import java.util.Iterator;
/**
 * @author YangXiaosa
 **/
public class MyLinkedList<E> {
private Node first;
private Node last;
private int size = 0;
public int size() {
return size;
}
// --------------------------------增加的方法---------------------------------------
// 在集合首位置添加元素
public void addFirst(E e) {
if (first == null) {
Node node = new Node(null, e, null);
first = node;
last = node;
size++;
} else {
Node node = new Node(null, e, first);
first.prve = node;
first = node;
size++;
}
}
// 在集合末尾添加元素
public void addLast(E e) {
if (last == null) {
addFirst(e);
} else {
Node node = new Node(last, e, null);
last.next = node;
last = node;
size++;
}
}
// 默认的添加方法(添加到尾部)
public void add(E e) {
addLast(e);
}
// 在集合指定位置添加元素
public void add(int index, E e) {
if (index == 0) {
addFirst(e);
} else if (index == size - 1) {
addLast(e);
} else {
Node node = getNode(index);
Node prve = node.prve;
Node newNode = new Node(prve, e, node);
prve.next = newNode;
node.prve = newNode;
size++;
}
}
// ----------------------------------根据索引获取元素方法--------------------------------
// 获取集合中指定位置的元素
public E get(int index) {
return getNode(index).item;
}
// 获取集合中指定元素第一次出现的索引
public int getIndex(E e) {
for (int i = 0; i < size; i++) {
if (get(i) == e) {
return i;
}
}
return -1;
}
// ----------------------------------删除方法-------------------------------------------
// 删除集合中指定位置的元素
public boolean remove(int index) {
Node node = getNode(index);
Node prve = node.prve;
Node next = node.next;
next.prve = prve;
prve.next = next;
node.next = null;
node.prve = null;
size--;
return true;
}
// 删除集合指定的所有元素
public boolean remove(E e) {
boolean b = false;
int index;
while ((index = getIndex(e)) != -1) {
remove(index);
b = true;
size--;
}
return b;
}
// -----------------------------------修改元素的方法------------------------------------
// 修改集合中指定位置的元素
public E set(int index, E e) {
Node node = getNode(index);
E e1 = node.item;
node.item = e;
return e1;
}
// 使用新元素替换指定的老元素
public boolean set(E oldE, E newE) {
boolean b = false;
int index;
while ((index = getIndex(oldE)) != -1) {
Node node = getNode(index);
node.item = newE;
b = true;
}
return b;
}
// ------------------------------------获取迭代器的方法--------------------
public Iterator<E> iterator() {
return new MyIterator();
}
// 定义迭代器内部类
private class MyIterator implements Iterator<E> {
private int course;
private Node current;
private Node prve;
private Node next;
private int expectedSize = size;


public boolean hasNext() {
checkSizeChange();
if (course < size) {
return true;
} else {
return false;
}
}
public E next() {
checkSizeChange();
if (course == 0) {
current = first;
prve = null;
next = current.next;
course++;
return current.item;
} else {
current = next;
prve = current.prve;
next = current.next;
course++;
return current.item;
}
}
@Override
public void remove() {
if (current == first) {
first = current.next;
first.prve = null;
current.next = null;
expectedSize = --size;
} else if (current == last) {
last = current.prve;
last.next = null;
current.prve = null;
expectedSize = --size;
} else {
prve.next = next;
next.prve = prve;
current.prve = null;
current.next = null;

expectedSize = --size;
}

course--;
}


public void checkSizeChange() {
if (expectedSize != size) {
throw new ModiWhenIteratorHadBeenCreatedException();
}
}
}
// 定义及节点内部类
class Node {
private Node prve;
private Node next;
public E item;


Node(Node prve, E e, Node next) {
this.prve = prve;
this.next = next;
item = e;
}
}
// 定义一个私有的根据索引获取节点的方法
private Node getNode(int index) {
if (index >= size) {
throw new outofBoundeException();
} else {
Node node = first;
for (int i = 1; i <= index; i++) {
node = node.next;
}
return node;
}
}
}

两个异常类:

public class ModiWhenIteratorHadBeenCreatedException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ModiWhenIteratorHadBeenCreatedException() {

}
public ModiWhenIteratorHadBeenCreatedException(String message) {
super(message);
}
}

public class outofBoundeException extends RuntimeException {
private static final long serialVersionUID = 1L;
public outofBoundeException() {

}
public outofBoundeException(String message) {
super(message);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值