简体   繁体   中英

Java LinkedList methods for Queue and Stack

if you look at the LinkedList methods of Java, it does offer operations for Queue, Stack, Deque.

And I am aware that you can implement Queue, Stack or Deque with a LinkedList. If you look at C# implementation though, Queue and Stack uses arrays.

My Curiosity is, why they offer a push(T e) method for a linked list?

Why Queue and Stack are not separate classes, just like C#.

Below is the code for push and pop which is duhhh. But why?

public void push(Object obj)
{
    addFirst(obj);
}

public Object pop()
{
    return removeFirst();
}

If you look at HashMap, or HashSet, it uses array internally, and there is LinkedHashSet and map correspondingly to keep ordering.

This is not really confusing, but it doesnt make sense really.

Why java has such implementation?

Focus on data structure implementation:

Linked list is efficient for frequent add and remove. (as Queue and Stack usually do, and iteration operation is rare). Array is not, it needs array-copy operation which is time consuming .

A double-linked list such as Java's LinkedList is a rather flexible data structure, so it makes sense to implement several data structures using it as a base. So if you want to view it as a Queue, you'd say something like this (I'm omitting type parameters):

Queue q = new LinkedList();

If you want to use it as a stack, you would declare it like this:

Deque s = new LinkedList();

And so on. It all boils down to code reutilization, after all, why implement several different classes for similar functionality, when a single class (LinkedList in this case) suffices?

LinkedList implements Queue interface because you might want to use the linked list as a Queue in some places. What this means is that a method which takes a queue as input param can also process linked lists. The following works

List<String> linkedList = new LinkedList<String>();
linkedList.add("element1");
linkedList.add("element2");

Queue<String> q = (Queue<String>)linkedList; 
q.poll(); //removes and returns element1 from the linkedList

Java has a separate class called java.util.Stack which extends from vector which in turn is a array based implementation. But this is a thread safe version. If you don't worry about thread safety, then you can use ArrayDeque as a stack.

Basic OOD: while perhaps a fuzzy line, Queue, Stack, and Deque roughly describe operations you can perform on a collection and hence deserve to be interfaces. LinkedList describes the implementation and performance characteristics of an interface and hence deserves to be a class. That implementation could be (is) exposed as multiple interfaces. To me, the real question is, "Why is Stack a class?"

Queue is an interface, and has other implementations besides LinkedList. There's also a Stack class.

Ultimately, it just seems like an arbitrary decision, and the underlying implementation doesn't really matter that much (IMO).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM