zoukankan      html  css  js  c++  java
  • 实现简单版的LinkedList

    相比ArrayList,双链表的数据结构就复杂多了,想要弄清代码的意思还是要搞清数据结构层面的变化。

      1 package cn.sp.chapter03;
      2 
      3 import java.util.ConcurrentModificationException;
      4 import java.util.Iterator;
      5 import java.util.NoSuchElementException;
      6 
      7 /**
      8  * Created by 2YSP on 2017/10/9.
      9  * 实现自己的双链表
     10  */
     11 public class MyLinkedList<AnyType> implements Iterable<AnyType> {
     12 
     13     private static class Node<AnyType> {
     14 
     15         public AnyType data;
     16         public Node<AnyType> prev;//指向的前一个节点
     17         public Node<AnyType> next;//指向的后一个节点
     18 
     19         public Node(AnyType d, Node<AnyType> p, Node<AnyType> n) {
     20             this.data = d;
     21             this.prev = p;
     22             this.next = n;
     23         }
     24     }
     25 
     26     public MyLinkedList() {
     27         doClear();
     28     }
     29 
     30     public void clear() {
     31         doClear();
     32     }
     33 
     34     private void doClear() {
     35         beginMarker = new Node<AnyType>(null, null, null);
     36         endMarker = new Node<AnyType>(null, beginMarker, null);
     37         beginMarker.next = endMarker;
     38 
     39         theSize = 0;
     40         modCount++;
     41     }
     42 
     43     public int size() {
     44         return theSize;
     45     }
     46 
     47     public boolean isEmpty() {
     48         return size() == 0;
     49     }
     50 
     51     public boolean add(AnyType x) {
     52         add(size(), x);
     53         return true;
     54     }
     55 
     56     public void add(int idx, AnyType x) {
     57         addBefore(getNode(idx, 0, size()), x);
     58 
     59     }
     60 
     61     public AnyType get(int idx) {
     62         return getNode(idx).data;
     63     }
     64 
     65 
     66     public AnyType set(int idx, AnyType newVal) {
     67         Node<AnyType> p = getNode(idx);
     68         AnyType oldVal = p.data;
     69         p.data = newVal;
     70         return oldVal;
     71     }
     72 
     73     public AnyType remove(int idx) {
     74         return remove(getNode(idx));
     75     }
     76 
     77 
     78     /**
     79      * @param p 添加在该节点前
     80      * @param x 要添加的数据
     81      */
     82     private void addBefore(Node<AnyType> p, AnyType x) {
     83         Node<AnyType> newNode = new Node<>(x, p.prev, p);
     84         newNode.prev.next = newNode;
     85         p.prev = newNode;
     86         theSize++;
     87         modCount++;
     88     }
     89 
     90     private AnyType remove(Node<AnyType> p) {
     91         p.prev.next = p.next;
     92         p.next.prev = p.prev;
     93         theSize--;
     94         modCount++;
     95         return p.data;
     96     }
     97 
     98     private Node<AnyType> getNode(int idx) {
     99         return getNode(idx, 0, size() - 1);
    100     }
    101 
    102     private Node<AnyType> getNode(int idx, int lower, int upper) {
    103         Node<AnyType> p;
    104 
    105         if (idx < lower || idx > upper) {
    106             throw new IndexOutOfBoundsException();
    107         }
    108 
    109         if (idx < size() / 2) {
    110             p = beginMarker.next;
    111             for (int i = 0; i < idx; i++) {
    112                 p = p.next;
    113             }
    114 
    115         } else {
    116 
    117             p = endMarker;
    118             for (int i = size(); i > idx; i--) {
    119                 p = p.prev;
    120             }
    121         }
    122 
    123         return p;
    124     }
    125 
    126     @Override
    127     public Iterator<AnyType> iterator() {
    128         return new LinkedListIterator();
    129     }
    130 
    131     private class LinkedListIterator implements java.util.Iterator<AnyType> {
    132 
    133         private Node<AnyType> current = beginMarker.next;
    134         private int expectedModCount = modCount;
    135         private boolean okToRemove = false;
    136 
    137 
    138         @Override
    139         public boolean hasNext() {
    140             return current != endMarker;
    141         }
    142 
    143         @Override
    144         public AnyType next() {
    145 
    146             if (modCount != expectedModCount) {
    147                 throw new ConcurrentModificationException();
    148             }
    149 
    150             if (!hasNext()) {
    151                 throw new NoSuchElementException();
    152             }
    153 
    154             AnyType nextItem = current.data;
    155             current = current.next;
    156             okToRemove = true;
    157 
    158             return nextItem;
    159         }
    160 
    161         @Override
    162         public void remove() {
    163             if (modCount != expectedModCount) {
    164                 throw new ConcurrentModificationException();
    165             }
    166 
    167             if (!okToRemove) {
    168                 throw new IllegalStateException();
    169             }
    170 
    171             MyLinkedList.this.remove(current.prev);
    172             expectedModCount++;
    173             okToRemove = false;
    174         }
    175     }
    176 
    177     private int theSize;
    178     private int modCount = 0;
    179     private Node<AnyType> beginMarker;
    180     private Node<AnyType> endMarker;
    181 }
  • 相关阅读:
    JavaScript如何获取一个元素的样式信息
    Linux服务器命令行操作(小白专用)
    Linux云服务器搭建node环境
    C++ new和delete运算符简介
    C++中free()与delete的区别
    VS2017+Qt开发时打开命令调试窗口
    opencv4.2版本遇到CV_MINMAX未声明标识符
    CUDA 数据传输
    uchar 存为8位/16位图像(QImage)
    Qt Creator删除toolbar中多余的“分隔符”
  • 原文地址:https://www.cnblogs.com/2YSP/p/7695877.html
Copyright © 2011-2022 走看看