Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is linkedList a stack? what is the best implementation of stack data structure

Tags:

java

I notice that linkedList has some methods like pop and push. Typically, if I want to use the feature of stack (FILO).Would the linkedList be the best choice?

like image 876
Xuzheng Wang Avatar asked Aug 21 '15 16:08

Xuzheng Wang


People also ask

Which implementation is best for stack?

You can perform the implementation of stacks in data structures using two data structures that are an array and a linked list. Array: In array implementation, the stack is formed using an array. All the operations are performed using arrays.

Which is better stack or linked list?

Linked lists are good for inserting and removing elements at random positions. In a stack, we only ever append to or remove from the end which makes an ArrayList much more appealing to implement a stack.

What is the linked list implementation of stack?

In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said to be overflown if the space left in the memory heap is not enough to create a node.

Can LinkedList be implemented using stack?

To implement a stack using the singly linked list concept, all the singly linked list operations should be performed based on Stack operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly linked list.


1 Answers

LinkedList will work, and in fact implements the most stack-like interface in the JDK, Deque.

ArrayDeque is the other main non-threadsafe implementation, and is probably more efficient if you only need the stack operations. The above link for Deque lists the other two JDK-provided implementations, which are thread safe.

like image 145
yshavit Avatar answered Nov 15 '22 15:11

yshavit