LinkedList.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* LinkedList.java -- Linked list implementation of the List interface
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import java.io.Serializable;
  33. import java.io.ObjectOutputStream;
  34. import java.io.ObjectInputStream;
  35. import java.io.IOException;
  36. import java.lang.reflect.Array;
  37. /**
  38. * Linked list implementation of the List interface. In addition to the
  39. * methods of the List interface, this class provides access to the first
  40. * and last list elements in O(1) time for easy stack, queue, or double-ended
  41. * queue (deque) creation. The list is doubly-linked, with traversal to a
  42. * given index starting from the end closest to the element.<p>
  43. *
  44. * LinkedList is not synchronized, so if you need multi-threaded access,
  45. * consider using:<br>
  46. * <code>List l = Collections.synchronizedList(new LinkedList(...));</code>
  47. * <p>
  48. *
  49. * The iterators are <i>fail-fast</i>, meaning that any structural
  50. * modification, except for <code>remove()</code> called on the iterator
  51. * itself, cause the iterator to throw a
  52. * {@link ConcurrentModificationException} rather than exhibit
  53. * non-deterministic behavior.
  54. *
  55. * @author Original author unknown
  56. * @author Bryce McKinlay
  57. * @author Eric Blake <ebb9@email.byu.edu>
  58. * @see List
  59. * @see ArrayList
  60. * @see Vector
  61. * @see Collections#synchronizedList(List)
  62. * @since 1.2
  63. * @status missing javadoc, but complete to 1.4
  64. */
  65. public class LinkedList extends AbstractSequentialList
  66. implements List, Cloneable, Serializable
  67. {
  68. /**
  69. * Compatible with JDK 1.2.
  70. */
  71. private static final long serialVersionUID = 876323262645176354L;
  72. /**
  73. * The first element in the list.
  74. */
  75. transient Entry first;
  76. /**
  77. * The last element in the list.
  78. */
  79. transient Entry last;
  80. /**
  81. * The current length of the list.
  82. */
  83. transient int size = 0;
  84. /**
  85. * Class to represent an entry in the list. Holds a single element.
  86. */
  87. private static final class Entry
  88. {
  89. /** The element in the list. */
  90. Object data;
  91. /** The next list entry, null if this is last. */
  92. Entry next;
  93. /** The previous list entry, null if this is first. */
  94. Entry previous;
  95. /**
  96. * Construct an entry.
  97. * @param data the list element
  98. */
  99. Entry(Object data)
  100. {
  101. this.data = data;
  102. }
  103. } // class Entry
  104. /**
  105. * Obtain the Entry at a given position in a list. This method of course
  106. * takes linear time, but it is intelligent enough to take the shorter of the
  107. * paths to get to the Entry required. This implies that the first or last
  108. * entry in the list is obtained in constant time, which is a very desirable
  109. * property.
  110. * For speed and flexibility, range checking is not done in this method:
  111. * Incorrect values will be returned if (n &lt; 0) or (n &gt;= size).
  112. *
  113. * @param n the number of the entry to get
  114. * @return the entry at position n
  115. */
  116. // Package visible for use in nested classes.
  117. Entry getEntry(int n)
  118. {
  119. Entry e;
  120. if (n < size / 2)
  121. {
  122. e = first;
  123. // n less than size/2, iterate from start
  124. while (n-- > 0)
  125. e = e.next;
  126. }
  127. else
  128. {
  129. e = last;
  130. // n greater than size/2, iterate from end
  131. while (++n < size)
  132. e = e.previous;
  133. }
  134. return e;
  135. }
  136. /**
  137. * Remove an entry from the list. This will adjust size and deal with
  138. * `first' and `last' appropriatly.
  139. *
  140. * @param e the entry to remove
  141. */
  142. // Package visible for use in nested classes.
  143. void removeEntry(Entry e)
  144. {
  145. modCount++;
  146. size--;
  147. if (size == 0)
  148. first = last = null;
  149. else
  150. {
  151. if (e == first)
  152. {
  153. first = e.next;
  154. e.next.previous = null;
  155. }
  156. else if (e == last)
  157. {
  158. last = e.previous;
  159. e.previous.next = null;
  160. }
  161. else
  162. {
  163. e.next.previous = e.previous;
  164. e.previous.next = e.next;
  165. }
  166. }
  167. }
  168. /**
  169. * Checks that the index is in the range of possible elements (inclusive).
  170. *
  171. * @param index the index to check
  172. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size
  173. */
  174. private void checkBoundsInclusive(int index)
  175. {
  176. if (index < 0 || index > size)
  177. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  178. + size);
  179. }
  180. /**
  181. * Checks that the index is in the range of existing elements (exclusive).
  182. *
  183. * @param index the index to check
  184. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size
  185. */
  186. private void checkBoundsExclusive(int index)
  187. {
  188. if (index < 0 || index >= size)
  189. throw new IndexOutOfBoundsException("Index: " + index + ", Size:"
  190. + size);
  191. }
  192. /**
  193. * Create an empty linked list.
  194. */
  195. public LinkedList()
  196. {
  197. }
  198. /**
  199. * Create a linked list containing the elements, in order, of a given
  200. * collection.
  201. *
  202. * @param c the collection to populate this list from
  203. * @throws NullPointerException if c is null
  204. */
  205. public LinkedList(Collection c)
  206. {
  207. addAll(c);
  208. }
  209. /**
  210. * Returns the first element in the list.
  211. *
  212. * @return the first list element
  213. * @throws NoSuchElementException if the list is empty
  214. */
  215. public Object getFirst()
  216. {
  217. if (size == 0)
  218. throw new NoSuchElementException();
  219. return first.data;
  220. }
  221. /**
  222. * Returns the last element in the list.
  223. *
  224. * @return the last list element
  225. * @throws NoSuchElementException if the list is empty
  226. */
  227. public Object getLast()
  228. {
  229. if (size == 0)
  230. throw new NoSuchElementException();
  231. return last.data;
  232. }
  233. /**
  234. * Remove and return the first element in the list.
  235. *
  236. * @return the former first element in the list
  237. * @throws NoSuchElementException if the list is empty
  238. */
  239. public Object removeFirst()
  240. {
  241. if (size == 0)
  242. throw new NoSuchElementException();
  243. modCount++;
  244. size--;
  245. Object r = first.data;
  246. if (first.next != null)
  247. first.next.previous = null;
  248. else
  249. last = null;
  250. first = first.next;
  251. return r;
  252. }
  253. /**
  254. * Remove and return the last element in the list.
  255. *
  256. * @return the former last element in the list
  257. * @throws NoSuchElementException if the list is empty
  258. */
  259. public Object removeLast()
  260. {
  261. if (size == 0)
  262. throw new NoSuchElementException();
  263. modCount++;
  264. size--;
  265. Object r = last.data;
  266. if (last.previous != null)
  267. last.previous.next = null;
  268. else
  269. first = null;
  270. last = last.previous;
  271. return r;
  272. }
  273. /**
  274. * Insert an element at the first of the list.
  275. *
  276. * @param o the element to insert
  277. */
  278. public void addFirst(Object o)
  279. {
  280. Entry e = new Entry(o);
  281. modCount++;
  282. if (size == 0)
  283. first = last = e;
  284. else
  285. {
  286. e.next = first;
  287. first.previous = e;
  288. first = e;
  289. }
  290. size++;
  291. }
  292. /**
  293. * Insert an element at the last of the list.
  294. *
  295. * @param o the element to insert
  296. */
  297. public void addLast(Object o)
  298. {
  299. addLastEntry(new Entry(o));
  300. }
  301. /**
  302. * Inserts an element at the end of the list.
  303. *
  304. * @param e the entry to add
  305. */
  306. private void addLastEntry(Entry e)
  307. {
  308. modCount++;
  309. if (size == 0)
  310. first = last = e;
  311. else
  312. {
  313. e.previous = last;
  314. last.next = e;
  315. last = e;
  316. }
  317. size++;
  318. }
  319. /**
  320. * Returns true if the list contains the given object. Comparison is done by
  321. * <code>o == null ? e = null : o.equals(e)</code>.
  322. *
  323. * @param o the element to look for
  324. * @return true if it is found
  325. */
  326. public boolean contains(Object o)
  327. {
  328. Entry e = first;
  329. while (e != null)
  330. {
  331. if (equals(o, e.data))
  332. return true;
  333. e = e.next;
  334. }
  335. return false;
  336. }
  337. /**
  338. * Returns the size of the list.
  339. *
  340. * @return the list size
  341. */
  342. public int size()
  343. {
  344. return size;
  345. }
  346. /**
  347. * Adds an element to the end of the list.
  348. *
  349. * @param e the entry to add
  350. * @return true, as it always succeeds
  351. */
  352. public boolean add(Object o)
  353. {
  354. addLastEntry(new Entry(o));
  355. return true;
  356. }
  357. /**
  358. * Removes the entry at the lowest index in the list that matches the given
  359. * object, comparing by <code>o == null ? e = null : o.equals(e)</code>.
  360. *
  361. * @param o the object to remove
  362. * @return true if an instance of the object was removed
  363. */
  364. public boolean remove(Object o)
  365. {
  366. Entry e = first;
  367. while (e != null)
  368. {
  369. if (equals(o, e.data))
  370. {
  371. removeEntry(e);
  372. return true;
  373. }
  374. e = e.next;
  375. }
  376. return false;
  377. }
  378. /**
  379. * Append the elements of the collection in iteration order to the end of
  380. * this list. If this list is modified externally (for example, if this
  381. * list is the collection), behavior is unspecified.
  382. *
  383. * @param c the collection to append
  384. * @return true if the list was modified
  385. * @throws NullPointerException if c is null
  386. */
  387. public boolean addAll(Collection c)
  388. {
  389. return addAll(size, c);
  390. }
  391. /**
  392. * Insert the elements of the collection in iteration order at the given
  393. * index of this list. If this list is modified externally (for example,
  394. * if this list is the collection), behavior is unspecified.
  395. *
  396. * @param c the collection to append
  397. * @return true if the list was modified
  398. * @throws NullPointerException if c is null
  399. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  400. */
  401. public boolean addAll(int index, Collection c)
  402. {
  403. checkBoundsInclusive(index);
  404. int csize = c.size();
  405. if (csize == 0)
  406. return false;
  407. Iterator itr = c.iterator();
  408. // Get the entries just before and after index. If index is at the start
  409. // of the list, BEFORE is null. If index is at the end of the list, AFTER
  410. // is null. If the list is empty, both are null.
  411. Entry after = null;
  412. Entry before = null;
  413. if (index != size)
  414. {
  415. after = getEntry(index);
  416. before = after.previous;
  417. }
  418. else
  419. before = last;
  420. // Create the first new entry. We do not yet set the link from `before'
  421. // to the first entry, in order to deal with the case where (c == this).
  422. // [Actually, we don't have to handle this case to fufill the
  423. // contract for addAll(), but Sun's implementation appears to.]
  424. Entry e = new Entry(itr.next());
  425. e.previous = before;
  426. Entry prev = e;
  427. Entry firstNew = e;
  428. // Create and link all the remaining entries.
  429. for (int pos = 1; pos < csize; pos++)
  430. {
  431. e = new Entry(itr.next());
  432. e.previous = prev;
  433. prev.next = e;
  434. prev = e;
  435. }
  436. // Link the new chain of entries into the list.
  437. modCount++;
  438. size += csize;
  439. prev.next = after;
  440. if (after != null)
  441. after.previous = e;
  442. else
  443. last = e;
  444. if (before != null)
  445. before.next = firstNew;
  446. else
  447. first = firstNew;
  448. return true;
  449. }
  450. /**
  451. * Remove all elements from this list.
  452. */
  453. public void clear()
  454. {
  455. if (size > 0)
  456. {
  457. modCount++;
  458. first = null;
  459. last = null;
  460. size = 0;
  461. }
  462. }
  463. /**
  464. * Return the element at index.
  465. *
  466. * @param index the place to look
  467. * @return the element at index
  468. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  469. */
  470. public Object get(int index)
  471. {
  472. checkBoundsExclusive(index);
  473. return getEntry(index).data;
  474. }
  475. /**
  476. * Replace the element at the given location in the list.
  477. *
  478. * @param index which index to change
  479. * @param o the new element
  480. * @return the prior element
  481. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt;= size()
  482. */
  483. public Object set(int index, Object o)
  484. {
  485. checkBoundsExclusive(index);
  486. Entry e = getEntry(index);
  487. Object old = e.data;
  488. e.data = o;
  489. return old;
  490. }
  491. /**
  492. * Inserts an element in the given position in the list.
  493. *
  494. * @param index where to insert the element
  495. * @param o the element to insert
  496. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  497. */
  498. public void add(int index, Object o)
  499. {
  500. checkBoundsInclusive(index);
  501. Entry e = new Entry(o);
  502. if (index < size)
  503. {
  504. modCount++;
  505. Entry after = getEntry(index);
  506. e.next = after;
  507. e.previous = after.previous;
  508. if (after.previous == null)
  509. first = e;
  510. else
  511. after.previous.next = e;
  512. after.previous = e;
  513. size++;
  514. }
  515. else
  516. addLastEntry(e);
  517. }
  518. /**
  519. * Removes the element at the given position from the list.
  520. *
  521. * @param index the location of the element to remove
  522. * @return the removed element
  523. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  524. */
  525. public Object remove(int index)
  526. {
  527. checkBoundsExclusive(index);
  528. Entry e = getEntry(index);
  529. removeEntry(e);
  530. return e.data;
  531. }
  532. /**
  533. * Returns the first index where the element is located in the list, or -1.
  534. *
  535. * @param o the element to look for
  536. * @return its position, or -1 if not found
  537. */
  538. public int indexOf(Object o)
  539. {
  540. int index = 0;
  541. Entry e = first;
  542. while (e != null)
  543. {
  544. if (equals(o, e.data))
  545. return index;
  546. index++;
  547. e = e.next;
  548. }
  549. return -1;
  550. }
  551. /**
  552. * Returns the last index where the element is located in the list, or -1.
  553. *
  554. * @param o the element to look for
  555. * @return its position, or -1 if not found
  556. */
  557. public int lastIndexOf(Object o)
  558. {
  559. int index = size - 1;
  560. Entry e = last;
  561. while (e != null)
  562. {
  563. if (equals(o, e.data))
  564. return index;
  565. index--;
  566. e = e.previous;
  567. }
  568. return -1;
  569. }
  570. /**
  571. * Obtain a ListIterator over this list, starting at a given index. The
  572. * ListIterator returned by this method supports the add, remove and set
  573. * methods.
  574. *
  575. * @param index the index of the element to be returned by the first call to
  576. * next(), or size() to be initially positioned at the end of the list
  577. * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
  578. */
  579. public ListIterator listIterator(int index)
  580. {
  581. checkBoundsInclusive(index);
  582. return new LinkedListItr(index);
  583. }
  584. /**
  585. * Create a shallow copy of this LinkedList (the elements are not cloned).
  586. *
  587. * @return an object of the same class as this object, containing the
  588. * same elements in the same order
  589. */
  590. public Object clone()
  591. {
  592. LinkedList copy = null;
  593. try
  594. {
  595. copy = (LinkedList) super.clone();
  596. }
  597. catch (CloneNotSupportedException ex)
  598. {
  599. }
  600. copy.clear();
  601. copy.addAll(this);
  602. return copy;
  603. }
  604. /**
  605. * Returns an array which contains the elements of the list in order.
  606. *
  607. * @return an array containing the list elements
  608. */
  609. public Object[] toArray()
  610. {
  611. Object[] array = new Object[size];
  612. Entry e = first;
  613. for (int i = 0; i < size; i++)
  614. {
  615. array[i] = e.data;
  616. e = e.next;
  617. }
  618. return array;
  619. }
  620. /**
  621. * Returns an Array whose component type is the runtime component type of
  622. * the passed-in Array. The returned Array is populated with all of the
  623. * elements in this LinkedList. If the passed-in Array is not large enough
  624. * to store all of the elements in this List, a new Array will be created
  625. * and returned; if the passed-in Array is <i>larger</i> than the size
  626. * of this List, then size() index will be set to null.
  627. *
  628. * @param a the passed-in Array
  629. * @return an array representation of this list
  630. * @throws ArrayStoreException if the runtime type of a does not allow
  631. * an element in this list
  632. * @throws NullPointerException if a is null
  633. */
  634. public Object[] toArray(Object[] a)
  635. {
  636. if (a.length < size)
  637. a = (Object[]) Array.newInstance(a.getClass().getComponentType(), size);
  638. else if (a.length > size)
  639. a[size] = null;
  640. Entry e = first;
  641. for (int i = 0; i < size; i++)
  642. {
  643. a[i] = e.data;
  644. e = e.next;
  645. }
  646. return a;
  647. }
  648. /**
  649. * Serializes this object to the given stream.
  650. *
  651. * @param s the stream to write to
  652. * @throws IOException if the underlying stream fails
  653. * @serialData the size of the list (int), followed by all the elements
  654. * (Object) in proper order
  655. */
  656. private void writeObject(ObjectOutputStream s) throws IOException
  657. {
  658. s.defaultWriteObject();
  659. s.writeInt(size);
  660. Entry e = first;
  661. while (e != null)
  662. {
  663. s.writeObject(e.data);
  664. e = e.next;
  665. }
  666. }
  667. /**
  668. * Deserializes this object from the given stream.
  669. *
  670. * @param s the stream to read from
  671. * @throws ClassNotFoundException if the underlying stream fails
  672. * @throws IOException if the underlying stream fails
  673. * @serialData the size of the list (int), followed by all the elements
  674. * (Object) in proper order
  675. */
  676. private void readObject(ObjectInputStream s)
  677. throws IOException, ClassNotFoundException
  678. {
  679. s.defaultReadObject();
  680. int i = s.readInt();
  681. while (--i >= 0)
  682. addLastEntry(new Entry(s.readObject()));
  683. }
  684. /**
  685. * A ListIterator over the list. This class keeps track of its
  686. * position in the list and the two list entries it is between.
  687. *
  688. * @author Original author unknown
  689. * @author Eric Blake <ebb9@email.byu.edu>
  690. */
  691. private final class LinkedListItr implements ListIterator
  692. {
  693. /** Number of modifications we know about. */
  694. private int knownMod = modCount;
  695. /** Entry that will be returned by next(). */
  696. private Entry next;
  697. /** Entry that will be returned by previous(). */
  698. private Entry previous;
  699. /** Entry that will be affected by remove() or set(). */
  700. private Entry lastReturned;
  701. /** Index of `next'. */
  702. private int position;
  703. /**
  704. * Initialize the iterator.
  705. *
  706. * @param index the initial index
  707. */
  708. LinkedListItr(int index)
  709. {
  710. if (index == size)
  711. {
  712. next = null;
  713. previous = last;
  714. }
  715. else
  716. {
  717. next = getEntry(index);
  718. previous = next.previous;
  719. }
  720. position = index;
  721. }
  722. /**
  723. * Checks for iterator consistency.
  724. *
  725. * @throws ConcurrentModificationException if the list was modified
  726. */
  727. private void checkMod()
  728. {
  729. if (knownMod != modCount)
  730. throw new ConcurrentModificationException();
  731. }
  732. /**
  733. * Returns the index of the next element.
  734. *
  735. * @return the next index
  736. * @throws ConcurrentModificationException if the list was modified
  737. */
  738. public int nextIndex()
  739. {
  740. checkMod();
  741. return position;
  742. }
  743. /**
  744. * Returns the index of the previous element.
  745. *
  746. * @return the previous index
  747. * @throws ConcurrentModificationException if the list was modified
  748. */
  749. public int previousIndex()
  750. {
  751. checkMod();
  752. return position - 1;
  753. }
  754. /**
  755. * Returns true if more elements exist via next.
  756. *
  757. * @return true if next will succeed
  758. * @throws ConcurrentModificationException if the list was modified
  759. */
  760. public boolean hasNext()
  761. {
  762. checkMod();
  763. return (next != null);
  764. }
  765. /**
  766. * Returns true if more elements exist via previous.
  767. *
  768. * @return true if previous will succeed
  769. * @throws ConcurrentModificationException if the list was modified
  770. */
  771. public boolean hasPrevious()
  772. {
  773. checkMod();
  774. return (previous != null);
  775. }
  776. /**
  777. * Returns the next element.
  778. *
  779. * @return the next element
  780. * @throws ConcurrentModificationException if the list was modified
  781. * @throws NoSuchElementException if there is no next
  782. */
  783. public Object next()
  784. {
  785. checkMod();
  786. if (next == null)
  787. throw new NoSuchElementException();
  788. position++;
  789. lastReturned = previous = next;
  790. next = lastReturned.next;
  791. return lastReturned.data;
  792. }
  793. /**
  794. * Returns the previous element.
  795. *
  796. * @return the previous element
  797. * @throws ConcurrentModificationException if the list was modified
  798. * @throws NoSuchElementException if there is no previous
  799. */
  800. public Object previous()
  801. {
  802. checkMod();
  803. if (previous == null)
  804. throw new NoSuchElementException();
  805. position--;
  806. lastReturned = next = previous;
  807. previous = lastReturned.previous;
  808. return lastReturned.data;
  809. }
  810. /**
  811. * Remove the most recently returned element from the list.
  812. *
  813. * @throws ConcurrentModificationException if the list was modified
  814. * @throws IllegalStateException if there was no last element
  815. */
  816. public void remove()
  817. {
  818. checkMod();
  819. if (lastReturned == null)
  820. throw new IllegalStateException();
  821. // Adjust the position to before the removed element, if the element
  822. // being removed is behind the cursor.
  823. if (lastReturned == previous)
  824. position--;
  825. next = lastReturned.next;
  826. previous = lastReturned.previous;
  827. removeEntry(lastReturned);
  828. knownMod++;
  829. lastReturned = null;
  830. }
  831. /**
  832. * Adds an element between the previous and next, and advance to the next.
  833. *
  834. * @param o the element to add
  835. * @throws ConcurrentModificationException if the list was modified
  836. */
  837. public void add(Object o)
  838. {
  839. checkMod();
  840. modCount++;
  841. knownMod++;
  842. size++;
  843. position++;
  844. Entry e = new Entry(o);
  845. e.previous = previous;
  846. e.next = next;
  847. if (previous != null)
  848. previous.next = e;
  849. else
  850. first = e;
  851. if (next != null)
  852. next.previous = e;
  853. else
  854. last = e;
  855. previous = e;
  856. lastReturned = null;
  857. }
  858. /**
  859. * Changes the contents of the element most recently returned.
  860. *
  861. * @param o the new element
  862. * @throws ConcurrentModificationException if the list was modified
  863. * @throws IllegalStateException if there was no last element
  864. */
  865. public void set(Object o)
  866. {
  867. checkMod();
  868. if (lastReturned == null)
  869. throw new IllegalStateException();
  870. lastReturned.data = o;
  871. }
  872. } // class LinkedListItr
  873. }