Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I get you right, you look for a data structure that offers several iterators for manipulating the list. This is technically difficult for the original java.util.LinkedList because it does housekeeping for the current index and this is only possible in an efficient way if there are no parallel changes at unknown positions in the list by other iterators. But, you can easily implement a simple LinkedList that does not do this housekeeping and supports adding/removing through several iterators. Then, an iterator does not know its position in the list, but I bet you do not care. Just use something like this:</p> <pre><code>public class MyList&lt;T&gt; { private MyNode&lt;T&gt; first = null, last = null; public MyNode&lt;T&gt; getFirst() { return first; } public MyNode&lt;T&gt; getLast() { return last; } public boolean contains(MyNode&lt;T&gt; n) { return n.list == this; } /** * If beforeMe is null, toInsert is inserted at the end of the list. * @return inserted node */ public void insertBefore(MyNode&lt;T&gt; beforeMe, MyNode&lt;T&gt; newNode) { if (newNode == null) { throw new IllegalArgumentException("toInsert must not be null!"); } if (newNode.list != null) { throw new IllegalArgumentException("This node is already in the list " + newNode.list); } if (beforeMe == null) { if (last == null) { newNode.prev = newNode.next = null; first = last = newNode; } else { last.next = newNode; newNode.prev = last; newNode.next = null; last = newNode; } } else { newNode.prev = beforeMe.prev; newNode.next = beforeMe; if (beforeMe.prev != null) { beforeMe.prev.next = newNode; } else { first = newNode; } beforeMe.prev = newNode; } newNode.list = this; } /** * If beforeMe is null, t is inserted at the end of the list. * @return inserted node */ public MyNode&lt;T&gt; insertBefore(MyNode&lt;T&gt; beforeMe, T t) { MyNode&lt;T&gt; newNode = new MyNode&lt;T&gt;(t); insertBefore(beforeMe, newNode); return newNode; } public void remove(MyNode&lt;T&gt; n) { if (n == null || n.list != this) { throw new IllegalArgumentException("Node is not in the list!"); } if (n.prev != null) { n.prev.next = n.next; } else { first = n.next; } if (n.next != null) { n.next.prev = n.prev; } else { last = n.prev; } n.prev = n.next = null; n.list = null; }} public class MyNode&lt;T&gt; { private T t; /** * written only by MyList */ MyNode&lt;T&gt; prev = null; /** * written only by MyList */ MyNode&lt;T&gt; next = null; /** * written only by MyList */ MyList&lt;T&gt; list = null; public T get() { return t; } public void set(T t) { this.t = t; } public MyNode&lt;T&gt; previous() { return prev; } public MyNode&lt;T&gt; next() { return next; } public MyList&lt;T&gt; list() { return list; } /** * called only by MyList. * @param t */ MyNode(T t) { this.t = t; }} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload