JAVA类库LinkList的基本实现
来源:互联网 发布:海湾消防主机编程视频 编辑:程序博客网 时间:2024/05/22 04:56
写完调试了好久,边界不好处理,详细的请看JDK类库,以下只是基本实现:
import java.util.Iterator;/** * 类名:MyLinkedList 说明:LinkedList的基本实现 */public class MyLinkedList<AnyType> implements Iterable {private int theSize = 0;private int modCount = 0;private Node<AnyType> beginMarker;private Node<AnyType> endMarker;private static class Node<AnyType> {public Node(AnyType data, Node<AnyType> prev, Node<AnyType> next) {this.data = data;this.prev = prev;this.next = next;}public AnyType data;public Node<AnyType> prev;public Node<AnyType> next;}public MyLinkedList() {clear();}public void clear() {beginMarker = new Node<AnyType>(null, null, null);endMarker = new Node<AnyType>(null, beginMarker, null);beginMarker.next = endMarker;theSize = 0;modCount++;}public boolean isEmpty() {return theSize == 0;}public int size() {return theSize;}public boolean add(AnyType x) {add(theSize, x);return true;}public void add(int index, AnyType x) {addBefore(getNode(index), x);}public AnyType get(int index) {return getNode(index).data;}private Node<AnyType> getNode(int index) {if (index < 0 || index > theSize)throw new IndexOutOfBoundsException();Node<AnyType> p;if (index < size() / 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;}private void addBefore(Node<AnyType> p, AnyType x) {Node<AnyType> newNode = new Node(x, p.prev, p);newNode.prev.next = newNode;p.prev = newNode;theSize++;modCount++;}private AnyType remove(Node<AnyType> p) {AnyType removed = p.data;p.prev.next = p.next;p.next.prev = p.prev;theSize--;modCount++;return removed;}public AnyType set(int index, AnyType x) {AnyType old = getNode(index).data;getNode(index).data = x;return old;}public AnyType remove(int index) {return remove(getNode(index));}@Overridepublic Iterator iterator() {// TODO Auto-generated method stubreturn new Iterator() {//匿名内部类实现,jdk里是返回重新写的一个private类。private Node<AnyType> current = beginMarker.next;private int expectedModCount = modCount;private boolean okToRemove = false;public boolean hasNext() {return current != endMarker;}public AnyType next() {if (modCount != expectedModCount) {throw new java.util.ConcurrentModificationException();}if (!hasNext())throw new java.util.NoSuchElementException();AnyType d = current.data;current = current.next;okToRemove = true;return d;}public void remove() {if (modCount != expectedModCount) {throw new java.util.ConcurrentModificationException();}if (!okToRemove)throw new IllegalStateException();MyLinkedList.this.remove(current.prev);okToRemove = false;expectedModCount++;}};}public String toString() {String s = new String();for (int i = 0; i < theSize; i++)s += get(i) + " ";return s;}/** * 方法名:MyLinkedList.java 说明:测试 */public static void main(String[] args) {// TODO Auto-generated method stubMyLinkedList<Integer> list = new MyLinkedList();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);System.out.println(list);list.remove(1);System.out.println(list);list.set(1, 2);System.out.println(list);Iterator ite = list.iterator();while (ite.hasNext()) {System.out.println(ite.next() + " ");}}}
0 0
- JAVA类库LinkList的基本实现
- LinkList模板的实现
- LinkList 单链表的实现
- linklist 链表的实现
- LinkList的一个简单实现
- 自己用java实现LinkList和arrayList
- C++实现的链表,LinkList
- java中ArrayList 、LinkList的区别分析
- java中ArrayList和LinkList的区别
- java中ArrayList和LinkList的区别
- java集合——linklist的用法
- java中ArrayList 、LinkList的区别分析
- Java中LinkList类的使用
- Java中ArrayList与LinkList的区别
- Lab1 线性表的基本操作及其应用 sqlist linklist
- 链表的链性表的c语言实现方式 linklist.h 和 linklist.c
- 链表LinkList.c实现
- LinkList的新建,插入,删除等的实现
- Android如何隐藏桌面图标
- utf-8和Unicode转换
- 原始套接字SOCK_RAW
- stl : map 插入相同key组成的make_pair, 结果是插入不进去, 不是覆盖
- Json读取与应用
- JAVA类库LinkList的基本实现
- 缓冲运动-1-[解决1].html
- cocos2dx游戏优化方向
- 工作感悟
- THIS_MODULE ?
- andorid SDK Message 代理
- ActivityGroup实现tab功能
- 内部类相关
- lua进阶5-C++调用lua函数