自己实现LinkedList(非所有功能测试通过)
MyList:
package javahomework.day31;
import java.util.ConcurrentModificationException;
import java.util.ListIterator;
import java.util.NoSuchElementException;
public class MyLinklist<E> implements MyList<E>{
Node head; //头
Node tail; //尾
int size; //集合元素个数
int modCount;//集合修改次数
public MyLinklist() {
head = new Node(null,null,null);
tail = new Node(head,null,null);
head.next = tail;
}
/**
*
* @return 重写toString 返回集合所有元素
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
Node node = head.next;
if(!isEmpty()){
for(int i=0;i<size-1;i++){
sb.append(node.val+",");
node = node.next;
}
sb.append(node.val);
}
sb.append("]");
return sb.toString();
}
private class Node{
Node pre;
Node next;
E val;
public Node(E val) {
this.val = val;
}
public Node(Node pre, E val,Node next) {
this.pre = pre;
this.next = next;
this.val = val;
}
}
/**
*
* @param e
* @return 添加某个元素
*/
@Override
public boolean add(E e) {
add(size,e);
return true;
}
/**
* 在指定位置插入元素
* @param index
* @param element
*/
@Override
public void add(int index, E element) {
checkIndexof(index);
if(isEmpty()){
Node node = new Node(head,element,tail);
head.next = node;
tail.pre = node;
}else {
if(index == size){
Node node = new Node(null,element,tail);
node.pre = tail.pre;
tail.pre.next = node;
tail.pre = node;
}else {
Node node = getNode(index);
Node addnode = new Node(null,element,node);
addnode.pre = node.pre;
node.pre.next = addnode;
node.pre = addnode;
}
}
size++;
modCount++;
}
/**
* 得到指定位置的结点
* @param index
* @return
*/
private Node getNode(int index) {
checkIndexForGet(index);
Node node = null;
if(!isEmpty()){
node = head.next;
for(int i =0;i<index;i++){
node = node.next;
}
}
return node;
}
/**
* 判断需要元素的索引是否合法
* @param index
*/
private void checkIndexForGet(int index) {
if(index<0||index>size-1){
throw new IllegalArgumentException("index="+ index);
}
}
/**
* 判断是否可以需要操作的位置是否合法
* @param index
*/
private void checkIndexof(int index) {
if(index<0||index>size){
throw new IllegalArgumentException("index = "+ index);
}
}
/**
* 清空集合
*/
@Override
public void clear() {
head.next = tail;
tail.pre = head;
size=0;
modCount++;
}
/**
* 判断集合是否包含此元素
* @param o
* @return
*/
@Override
public boolean contains(Object o) {
int n = indexOf(o);
return n!=-1;
}
/**
*
* @param index
* @return 返回指定位置的元素的值
*/
@Override
public E get(int index) {
checkIndexForGet(index);
Node node = head.next;
int n = 0;
while(node!=tail){
if(index==n){
return node.val;
}
n++;
node = node.next;
}
return null;
}
/**
* 返回元素最先出现的位置
* @param o
* @return
*/
@Override
public int indexOf(Object o) {
if(o==null){
Node node = head.next;
int n = 0;
while(node!=tail){
if(node.val==null){
return n;
}
n++;
node = node.next;
}
}else {
Node node = head.next;
int n = 0;
while(node!=tail){
if(node.val.equals(o)){
return n;
}
n++;
node = node.next;
}
}
return -1;
}
/**
*
* @param o
* @return 返回元素最后出现的位置
*/
@Override
public int lastIndexOf(Object o) {
if(o==null){
Node node = tail.pre;
int n = size-1;
while(node!=head){
if(node.val==null){
return n;
}
n--;
node = node.pre;
}
}else {
Node node = tail.pre;
int n = size-1;
while(node!=head){
if(node.val.equals(o)){
return n;
}
n--;
node = node.pre;
}
}
return -1;
}
/**
*
* @return 判断是否为空,空则返回true
*/
@Override
public boolean isEmpty() {
return size==0;
}
/**
* 移除指定位置元素
* @param index
* @return 返回移除的元素
*/
@Override
public E remove(int index) {
checkIndexForGet(index);
Node node = getNode(index);
node.pre.next = node.next;
node.next.pre = node.pre;
size--;
modCount++;
return (E) node;
}
/**
* 移除指定元素
* @param o
* @return 修改成功返回true
*/
@Override
public boolean remove(Object o) {
int i = indexOf(o);
if(i!=-1){
remove(i);
return true;
}else {
return false;
}
}
/**
* 把指定位置元素的值修改为element
* @param index
* @param element
* @return 返回修改前的值
*/
@Override
public E set(int index, E element) {
checkIndexForGet(index);
Node node = getNode(index);
node.val = element;
return null;
}
/**
* 返回元素的个数
* @return
*/
@Override
public int size() {
return size;
}
@Override
public ListIterator<E> listIterator() {
return new MyItr();
}
@Override
public ListIterator<E> listIterator(int index) {
return new MyItr(index);
}
private class MyItr implements ListIterator<E>{
int cursor;
int lastret=-1;
Node currentNode;
Node lastretnode = currentNode;
int expectedModCount = modCount;
/**
* 无参有参构造方法
*/
public MyItr() {
currentNode = head.next;
}
public MyItr(int index) {
this.cursor = index;
if(index==size){
currentNode = tail;
}else {
currentNode = getNode(index);
}
}
/**
* 是否有下一个元素
* @return
*/
@Override
public boolean hasNext() {
return cursor<size;
}
/**
* 返回下一个元素的值
* @return
*/
@Override
public E next() {
ConcurrentModificationException();
if(hasNext()){
lastret=cursor;
cursor++;
lastretnode = currentNode;
currentNode = currentNode.next;
}else {
throw new NoSuchElementException();
}
return (E) lastretnode.val;
}
/**
* 并发性检查
*/
private void ConcurrentModificationException() {
if(expectedModCount!=modCount){
throw new ConcurrentModificationException();
}
}
/**
* 是否有上一个元素
* @return
*/
@Override
public boolean hasPrevious() {
return cursor>0;
}
/**
* 返回上一个元素的值
* @return
*/
@Override
public E previous() {
ConcurrentModificationException();
if(hasPrevious()){
cursor--;
lastret=cursor;
lastretnode = currentNode.pre;
currentNode = currentNode.pre;
}else {
throw new NoSuchElementException();
}
return (E) lastretnode.val;
}
/**
*
* @return 返回对 next 的下一次调用所返回元素的索引。
*/
@Override
public int nextIndex() {
return cursor;
}
/**
*
* @return 返回对 previous 的后续调用所返回元素的索引。
*/
@Override
public int previousIndex() {
return cursor-1;
}
/**
* 从列表中移除由 next 或 previous 返回的最后一个元素(可选操作)。
*/
@Override
public void remove() {
if(lastretnode==null){
throw new IllegalStateException();
}
ConcurrentModificationException();
MyLinklist.this.remove(lastret);
cursor=lastret;
lastret = -1;
lastretnode = null;
expectedModCount = modCount;
}
/**
* 修改元素的值
* @param e
*/
@Override
public void set(E e) {
if(lastretnode==null){
throw new IllegalStateException();
}
ConcurrentModificationException();
MyLinklist.this.set(lastret,e);
}
/**
* 使用迭代器增加一个元素到cursor对应索引的位置上
* @param e
*/
@Override
public void add(E e) {
if(lastretnode==null){
throw new IllegalStateException();
}
ConcurrentModificationException();
MyLinklist.this.add(cursor,e);
cursor++;
lastret=-1;
lastretnode=null;
expectedModCount = modCount;
}
}
}
测试类:不完全测试
package javahomework.day31; import java.util.ListIterator; public class Test { public static void main(String[] args) { MyLinklist<String> sml = new MyLinklist<String>(); //boolean empty = sml.isEmpty();//判空 //System.out.println(empty); sml.add("a"); sml.add("b"); sml.add("b"); sml.add(0,"c"); //boolean empty1 = sml.isEmpty();//判空 //System.out.println(empty1); //sml.clear();//清空 //boolean c = sml.contains("a"); //System.out.println(c); //int size = sml.size(); //System.out.println(size);//元素个数 //sml.set(0,"a");//修改元素值 //String s = sml.get(1); //System.out.println(s); //int b = sml.indexOf("b"); //System.out.println(b); //int b = sml.lastIndexOf("b"); //System.out.println(b); //sml.remove(1); //sml.remove("c"); ListIterator<String> si = sml.listIterator(); for(;si.hasNext();){ String next = si.next(); if(next.equals("c")){ sml.add(2,"vvvvvv"); //si.remove(); //si.add("casdas"); } //System.out.println(next); } System.out.println("----------"); for(;si.hasPrevious();){ String next = si.previous(); System.out.println(next); } System.out.println(sml); } }