Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - LinkedList push() pop() implies it is a stack, not a queue?

In my data structures class, I learned that a LinkedList is a queue. Like a line in real life, the first person who gets into the line will be the first person who leaves. Makes sense. As seen below, a ListedList implements a Queue which has a FIFO (first in first out) procedure.

But if you look at the descriptions for the methods push(E) and pop(), they read as follows:

push(E)

Pushes an element onto the stack represented by this list. In other words, inserts the element at the front of this list.

pop()

Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.

That's.... not a queue. That's a stack. The first element that goes into the LinkedList via push cannot be accessed by pop until every element added after it has been pop()'ed.

Why is this? I get that LinkedLists can be both used as a stack (if you only use addFirst(E) and removeFirst()) and can be used as a queue (if you only use addFirst(E) and removeLast() or vice versa) so why is it like this? I feel like pop() should remove and return the last element, OR push(E) should add the element at the end of the LinkedList. Then it would make more sense.

TLDR: Why does LinkedList's push and pop imply it works as a stack when LinkedList actually implements Queue instead.

enter image description here

like image 845
Hatefiend Avatar asked Sep 18 '16 08:09

Hatefiend


People also ask

What is push and pop in LinkedList?

push(): Insert a new element into the stack i.e just insert a new element at the beginning of the linked list. pop(): Return the top element of the Stack i.e simply delete the first element from the linked list.

Is Java LinkedList a queue?

A LinkedList is already a queue, since it implements the Queue interface (and check the Javadoc yourself). Hence it has the following queue operations: enqueue: add() - Appends the specified element to the end of this list.

Is LinkedList a stack?

Linked list and stack are both linear data structures. The main difference is that Stack follows the LIFO(Last in, First out) principle, i.e., insertion and deletion can take place only at one end, whereas in a linked list, insertion and deletion can take place from any position.

Is LinkedList a list or a queue?

So in essence, a LinkedList is a Queue; it has all features that a Queue does and more. Keep in mind, a Queue is not a LinkedList, as a LinkedList is built and expanded upon a Queue.


2 Answers

Push() and pop() are by convention operations related to Stacks (Deque, more specifically in this context) and that's why you should expect your LinkedList to work that way when you use those method. If you want your LinkedList to work as a Queue instead (it implements the Queue interface) the methods you want to use (as stated in the Documentation) are add() and remove().

See LinkedList Documentation

like image 111
Sith Avatar answered Sep 28 '22 05:09

Sith


The methods (push() and pop()) you are mentioning are from the Deque interface, which is also implemented by LinkedList. The Javadoc for Deque states:

A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced "deck". Most Deque implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.

In other words, it is not the same as a regular queue.

If you really want to use LinkedList as a queue, you should assign the variable to that interface:

Queue<String> queue = new LinkedList<>();

Doing this, you would only be able to use the queue as a, well, queue. This interface defines, among other methods, add() and remove(), which are used to add and remove elements from the queue.

like image 42
Magnilex Avatar answered Sep 28 '22 07:09

Magnilex