1

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?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

5 Answers5

4

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.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
3

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?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

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.

Narendra Yadala
  • 9,554
  • 1
  • 28
  • 43
  • But again, why there is no concrete implementatition like C# has? what if i want to use arrays internally instead of a linked list. and you are doing a cast which is a computationally expensive operations. – DarthVader Oct 05 '11 at 05:40
  • There are lots of concrete implementations of Queue interface backed by Array data store such as PriorityQueue, ArrayDeque etc. You use linked list as a queue when you want best of both the worlds (list and queue) – Narendra Yadala Oct 05 '11 at 06:08
1

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?"

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
0

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).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302