LinkedList实现
来源:互联网 发布:olay大红瓶有用吗 知乎 编辑:程序博客网 时间:2024/05/21 18:40
import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.NoSuchElementException;public class MyLinkedList<E> implements Iterable<E>{ private static class Node<E>{ public E data; public Node<E> prev; public Node<E> next; public Node(E e, Node<E> p,Node<E> n){ this.data=e; this.prev=p; this.next=n; } } private int size; private int count=0; private Node<E> begin; private Node<E> end; public MyLinkedList() { doClear(); } public void clear() { doClear(); } private void doClear() { this.begin=new Node<E>(null,null,null); this.end=new Node<E>(null,begin,null); begin.next=end; this.size=0; count++; } public int size() { return this.size; } public boolean isEmpty() { return size()==0; } 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; size++; count++; } public boolean add(E e) { add(size(),e); return true; } public void add(int index,E e) { addBefore(getNode(index,0,size()),e); } public E get(int index) { return getNode(index).data; } public E set(int index,E newE) { Node<E> oldNode=getNode(index); E oldE=oldNode.data; oldNode.data=newE; return oldE; } public E remove(int index) { return remove(getNode(index)); } private E remove(Node<E> p) { p.next.prev=p.prev; p.prev.next=p.next; size--; count++; return p.data; } private Node<E> getNode(int index,int low,int high){ Node<E> p; if(index<low||index>high) { throw new IndexOutOfBoundsException(); } if(index<size()/2) { p=begin.next; for(int i=0;i<index;i++) { p=p.next; } }else { p=end; for(int i=size();i>index;i--) { p=p.prev; } } return p; } private Node<E> getNode(int index){ return getNode(index,0,size()-1); } @Override public Iterator<E> iterator() { return new LinkedListIterator(); } private class LinkedListIterator implements Iterator<E>{ private Node<E> current =begin.next; private int exceptedCount=count; private boolean okToRemove=false; @Override public boolean hasNext() { return current!=end; } @Override public E next() { if(count!=exceptedCount) { throw new ConcurrentModificationException(); } if(!hasNext()) { throw new NoSuchElementException(); } E next=current.data; current=current.next; okToRemove=true; return next; } public void remove() { if(count!=exceptedCount) { throw new ConcurrentModificationException(); } if(!okToRemove) { throw new IllegalStateException(); } MyLinkedList.this.remove(current.prev); exceptedCount++; okToRemove=false; } }}
阅读全文
0 0
- LinkedList实现
- LinkedList实现
- LinkedList实现
- LinkedList实现
- 实现LinkedList
- LinkedList实现
- LinkedList实现
- LinkedList实现
- 用LinkedList实现栈
- 用LinkedList实现队列
- 使用LinkedList实现Queue
- 类实现之LinkedList
- 通过LinkedList实现队列
- LinkedList实现栈,队列
- ArrayList和LinkedList实现
- Linkedlist实现Queue
- 自定义LinkedList实现
- LinkedList实现类
- SQL 聚集函数
- UVa 1585 Score
- 剑指offer_二叉树---二叉搜索树与双向链表
- maven
- 谈谈你对闭包的理解
- LinkedList实现
- ICS bomblab总结
- Linux vi命令修改文件及保存总结
- javascript 函数表达式
- 点击改变选项背景图(或其他)
- SPI 的主模式和从模式
- [POJ 1125]Stockbroker Grapevine[dijkstra]
- Python(1)
- 菜鸟的第五篇日志