Stacks Flashcards
What type of data structure is a stack?
Its a LIFO, which means last in first out.
What are the use cases for a stack?
1 - Managing function calls
2 - Undo / Re Do Functionalities
3 - Routing
4 - Used with other algorithms
What are two ways to implement a stack?
Using a array or a singly linked list.
Using an array, how would we implement a stack?
To implement a stack using arrays in JavaScript, you can use the push
and pop
methods.
Why would we not use shift and unshift when when implementing a stack with an array?
These methods will result in worse Big O notation. This is because every time we add or remove from the beginning, we have to re-index everything.
How would you implement a stack with a singly linked list?
Implement it.
Add and remove from the beginning of the list instead of the end.
When should you use stacks?
When you need to do inserting and removal operations.
When should you not use stacks?
When you need to do searching and accessing.
What is the Big O of insertion and removal for stacks?
Both are O(1)
.