实现简单版的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 }

 

转载于:https://www.cnblogs.com/2YSP/p/7695877.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值