

The LinkedList class shares many features with the ArrayList. That allows traversal in both the forward and backward direction: The nodes of a doubly linked list also contain a “prev” link field that points to the previous node in the sequence. The Java LinkedList class uses a Doubly linked list to store the elements. There are other kinds of linked lists, such as the Circular linked list, which is a singly linked list in which the last node and next field points back to the first node in the sequence. Types of Linked Lists in JavaĪt it’s most basic, a linked list is one whose nodes contain a data field as well as a “next” reference (link) to the next node in the list:
#Instantiating a linked list stack java how to
By the end of this Java programming tutorial, you will know about the different kinds of linked lists (yes, there are more than one type!), how they are implemented in Java, and how to work with them in your programs. In terms of the Java API, and any decent library, it should be safe to assume that every class implementing an interface will conform to what that interface claims to do, but just keep in mind that there isn't something stopping it from not conforming when it comes to more shady libraries or less experienced programmers.While you may have heard of linked lists, many developers have very little idea how they work, much less how to use them in their programs.

There's nothing in the language itself that prevents this - since, as far as the compiler is concerned, implementing Deque just requires that you define a bunch of methods - there's no enforcement of what these methods are supposed to do. Now for an important albeit somewhat confusing note - LinkedList can, for example, implement Deque and have an addFirst that doesn't do what it's supposed to - it can just, for example, print some random text.

Based on the docs, the addFirst function needs to add to the front of the Deque, and indeed, with a LinkedList, it will add to the front of the LinkedList (while the front of the LinkedList doesn't need to be the front of the Deque, by looking at the Deque methods implemented in LinkedList, we see that the front of the Deque is the front of the LinkedList, and all the methods add to / remove from one of the sides, do so from the correct side). Queue doesn't actually need to be FIFO (see the docs) (if it needed to be, a LinkedList would also need to be), so LinkedList would have quite a bit of freedom in this regard, so let's continue this explanation using Deque - it has methods to support addition and removal from either the front or the back.īecause LinkedList implements Deque, it needs to implement the addFirst function. The whole piece of paper is still there - you could simply remove the covering, which would be synonymous to casting it back to a LinkedList, which would then allow you to call any LinkedList method on it again.īy the way a LinkedList "normally would" do things I mentioned above, I mean a LinkedList is always a linked-list, even if you assign it to a Queue - it doesn't suddenly start using an array as the underlying implementation, as an ArrayDeque would (which also implements Queue), or something else. Given that most of it is covered, you can't see it's a LinkedList, all you can see is that it's a Queue, so you can only call the Queue methods on it. If you call a Queue method on it, it will still do things like a LinkedList normally would, since that that's what the piece of paper is. Think of the assignment to a Queue as covering all but a little part of this piece of paper which reveals that it's a Queue.

To explain with a (perhaps somewhat flawed) metaphor - think of a LinkedList as a piece of paper.
