-1

I realize that LinkedList in Java implements Queue interface (via: Deque), in addition to implementing List interface.

Refer below, the snippet is taken from Javadoc:

enter image description here

Why it is designed this way, that is, it is Queue as well as a List? So does it mean a LinkedList is both a Queue as well as List?

CuriousMind
  • 8,301
  • 22
  • 65
  • 134
  • 2
    Or https://stackoverflow.com/questions/15608026/difference-in-linkedlist-queue-vs-list – Sotirios Delimanolis Jun 05 '20 at 22:29
  • @SotiriosDelimanolis: Thanks for pointing those links, however , why can't `LinkedList` just use `List`, which is the intention (a List implementation). Having two interfaces give it **two** different capabilities, that is my confusion. – CuriousMind Jun 05 '20 at 22:34
  • Well, an old car could be both a 'means of transport' and a 'collectors item'. Has two different capabilities, depending if I want to use it to drive, or sell for good money. Where is the confusion? – pafau k. Jun 05 '20 at 22:43
  • 3
    The accepted answer in the first duplicate addresses that. – Sotirios Delimanolis Jun 05 '20 at 22:58
  • Yes. It's both. What's weird about that? – Louis Wasserman Jun 05 '20 at 23:17
  • 1
    Does this answer your question? [Why does java linkedlist implementation use the interface deque?](https://stackoverflow.com/questions/5896159/why-does-java-linkedlist-implementation-use-the-interface-deque) – Dmitry Pisklov Jun 06 '20 at 00:13
  • 1
    A large part of the problem here is your choice of words. It doesn't 'use' those interfaces, it *implements* them. It's the calling application that uses them, and `LinkedList` provides the application with a choice that the application can set to better express what the list is actually for. And your claim that the capabilities are different is false. The capabilities of `Queue` are a subset of the capabilities of `List`. – user207421 Jun 06 '20 at 01:07

1 Answers1

2

A linked list is naturally a queue. In fact, even prior to Java, if you want a queue with no fixed limit on the number of entries, a linked list is often the structure of choice.

A linked list has an order, and is O(1) for adding things to the tail, and O(1) for removing things from the head. You can keep on adding entries as long as you want. All in all, exactly what you need in a queue.

It would be a strange definition of a 'queue' if a linked list couldn't easily provide that interface.

user13463803
  • 281
  • 2
  • 4