1+
2+ package practices .Chapter ;
3+
4+
5+ /*
6+ ʵÏÖMyLinkedListµÄcontainsÀý³Ì¡£
7+ */
8+
9+ class MyLinkedList <AnyType > implements Iterable <AnyType > {
10+ private int theSize ;
11+ private int modCount =0 ;
12+ private Node <AnyType > beginMarker ;
13+ private Node <AnyType > endMarker ;
14+
15+ /*Node½ÚµãÀà*/
16+ private static class Node <AnyType > {
17+ public Node (AnyType d , Node <AnyType > p , Node <AnyType > n ) {
18+ data = d ;
19+ prev = p ;
20+ next = n ;
21+ }
22+
23+ public AnyType data ;
24+ public Node <AnyType > prev ;
25+ public Node <AnyType > next ;
26+ }
27+
28+ public MyLinkedList () {
29+ doClear ();
30+ }
31+
32+ public void clear () {
33+ doClear ();
34+ }
35+
36+ private void doClear () {
37+ beginMarker = new Node <AnyType >(null , null , null );
38+ endMarker = new Node <AnyType >(null , beginMarker , null );
39+ beginMarker .next = endMarker ;
40+
41+ theSize = 0 ;
42+ modCount ++;
43+ }
44+
45+ public int size () {
46+ return theSize ;
47+ }
48+
49+ public boolean isEmpty () {
50+ return size () == 0 ;
51+ }
52+
53+ public boolean add (AnyType x ) {
54+ add (size (), x );
55+ return true ;
56+ }
57+
58+ public void add (int idx , AnyType x ) {
59+ addBefore (getNode (idx , 0 , size ()), x );
60+ }
61+
62+ public AnyType get (int idx ) {
63+ return getNode (idx ).data ;
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+ private void addBefore (Node <AnyType > p , AnyType x ) {
78+ Node <AnyType > newNode = new Node <>(x , p .prev , p );
79+ newNode .prev .next = newNode ;
80+ p .prev = newNode ;
81+ theSize ++;
82+ modCount ++;
83+ }
84+
85+ private AnyType remove (Node <AnyType > p ) {
86+ p .next .prev = p .prev ;
87+ p .prev .next = p .next ;
88+ theSize --;
89+ modCount --;
90+ return p .data ;
91+ }
92+
93+ private Node <AnyType > getNode (int idx ) {
94+ return getNode (idx , 0 , size () - 1 );
95+ }
96+
97+ private Node <AnyType > getNode (int idx , int lower , int upper ) {
98+ Node <AnyType > p ;
99+
100+ if (idx < lower || idx > upper )
101+ throw new IndexOutOfBoundsException ();
102+
103+ if (idx < size () / 2 ) {
104+ p = beginMarker .next ;
105+ for (int i =0 ; i < idx ; i ++)
106+ p = p .next ;
107+ }
108+ else {
109+ p = endMarker ;
110+ for (int i = size (); i >idx ; i --)
111+ p = p .prev ;
112+ }
113+
114+ return p ;
115+ }
116+
117+ public boolean contains (AnyType x ) {
118+ Node <AnyType > p = beginMarker .next ;
119+ while (p != endMarker && (!p .data .equals (x ))) {
120+ p = p .next ;
121+ }
122+
123+ return p != endMarker ;
124+ }
125+
126+ public java .util .Iterator <AnyType > iterator () {
127+ return new LinkedListIterator ();
128+ }
129+
130+ private class LinkedListIterator implements java .util .Iterator <AnyType > {
131+ private Node <AnyType > current = beginMarker .next ;
132+ private int expectedModCount = modCount ;
133+ private boolean okToRemove = false ;
134+
135+ public boolean hasNext () {
136+ return current != endMarker ;
137+ }
138+
139+ public AnyType next (){
140+ if (modCount != expectedModCount )
141+ throw new java .util .ConcurrentModificationException ();
142+
143+
144+ if (!hasNext ())
145+ throw new java .util .NoSuchElementException ();
146+
147+ AnyType nextItem = current .data ;
148+ current = current .next ;
149+ okToRemove = true ;
150+ return nextItem ;
151+
152+ }
153+
154+ public void remove () {
155+ if (modCount != expectedModCount )
156+ throw new java .util .ConcurrentModificationException ();
157+ if (!okToRemove )
158+ throw new IllegalStateException ();
159+
160+ MyLinkedList .this .remove (current .prev );
161+ expectedModCount ++;
162+ okToRemove = false ;
163+ }
164+ }
165+ }
166+
167+ public class Chapter_3_3 {
168+
169+ public static void main (String args []) {
170+ System .out .println ("Chapter_3_3" );
171+
172+ MyLinkedList <String > list = new MyLinkedList ();
173+ list .add ("aaa" );
174+ list .add ("bbb" );
175+ list .add ("ccc" );
176+ list .add ("ddd" );
177+ list .add ("eee" );
178+ list .add ("fff" );
179+
180+ System .out .println (list .contains ("ccc" ));
181+ System .out .println (list .contains ("aab" ));
182+ }
183+ }
0 commit comments