When using Java LinkedList how do you find out the element's next or previous relationships?
I mean, in a regular linked list I would do something like this:
Node node1 = new Node();
Node node2 = new Node();
LinkedList list = new LinkedList();
list.add(node1);
list.add(node2);
//then my node1 will know who it's next is:
assertEquals(node2, node1.next());
where Node is my own container for data/object.
But in Java's LinkedList, the data does not seem to be modified. So how do I actually find out who the "next" (or "previous" in the case of doubly-linked lists) element is?
6 Answers 6
You can't. LinkedList is just an implementation of List and offers about nothing more. You'd need to make your own.
For node1.next()
you'd need a reference from node1
to the List. Actually, you'd need multiple references, since node1
may be there multiple times. Moreover, it may be contained in multiple Lists.
Maybe you can use ListIterator
for this.
-
4That is rather sad, as I was hoping for a "linked list" implementation. Oh well, I'm going to roll my own.Andriy Drozdyuk– Andriy Drozdyuk2011年02月07日 23:37:25 +00:00Commented Feb 7, 2011 at 23:37
-
1@drozzy - alternatively, take a second look at what you are doing and see if you can implement it better if you don't think in terms of
next
andprev
pointers.Stephen C– Stephen C2011年02月08日 01:01:56 +00:00Commented Feb 8, 2011 at 1:01 -
I wish, but I am implementing a Range Tree, which requires a doubly-linked list of it's leaves.Andriy Drozdyuk– Andriy Drozdyuk2011年02月08日 01:43:57 +00:00Commented Feb 8, 2011 at 1:43
-
@drozzy Wouldn't Guava's
RangeMap
do?maaartinus– maaartinus2014年09月09日 01:02:09 +00:00Commented Sep 9, 2014 at 1:02
I don't know what Node
class you're using, but LinkedList<T>
has its own internal node class, which you don't get access to. Calling add
will add a value to the list - you can't explicitly insert a node holding a value, or access the nodes themselves in any other way. Yes, that can be a pain sometimes.
If you need a linked list with a public encapsulation of the node as well, you'll need to find a different implementation or roll your own.
-
1Sorry, Node was my own imaginary container for the data. I would say java's LinkedList is not suitable for me in this case.Andriy Drozdyuk– Andriy Drozdyuk2011年02月07日 23:38:01 +00:00Commented Feb 7, 2011 at 23:38
Best Solution: Make your own next and last Links when constructing an new List Item Object:
Just have a lastInserted Object somewhere more globally
public MyLinkedListItem(){
if(lastInserted != null){
lastInserted.next = this;
this.last = lastInserted;
}
lastInserted = this;
}
You can use an iterator to move through the nodes such as:
Node node1 = new Node();
Node node2 = new Node();
LinkedList list = new LinkedList();
list.add(node1);
list.add(node2);
Iterator <Node> m_iterator=list.iterator();
//set iterator to first node
m_iterator.next();
//then my node1 will know who it's next is:
assertEquals(node2, m_iterator.next());
-
with iterator of this kind one would be able to move only in one direction with devalues the very idea of using some kind of linked list.shabunc– shabunc2022年07月01日 10:29:53 +00:00Commented Jul 1, 2022 at 10:29
-
@shabunc
ListIterator
also hashasPrevious()
andprevious()
methods that you can use to traverse the list backwards.MikaelF– MikaelF2022年10月02日 14:31:00 +00:00Commented Oct 2, 2022 at 14:31
The "linked" part of the LinkedList class name only refers to its implementation. The interface does not expose explicit methods to do what you want.
LinkedList implements the Collection (and List) interface, so given an index i
of an element in the list list
, you can get previous and next elements with list.get(i-1)
and list.get(i+1)
, respectively. For a LinkedList, the implementation of these methods are quite slow. If you do a lot of prev/next operation, consider to implement your list or to use an ArrayList instead.
-
2Yeah, so much for the "linked" list.Andriy Drozdyuk– Andriy Drozdyuk2011年02月07日 23:36:51 +00:00Commented Feb 7, 2011 at 23:36
I have to disagree with the accepted answer. You can as long as you have the head element. Once you lose reference to the first element by deleting and not returning the next element or insert an element before the first element and not return it, you will not be able to do your searches.
-
This makes no sense to me. Let the first element be
"f"
, find what comes next after the element"e"
.maaartinus– maaartinus2014年05月18日 16:39:04 +00:00Commented May 18, 2014 at 16:39
list.add(node1); list.add(node1);
?