Stack Flashcards
When would you use a Stack?
To reverse a list/array because LIFO (can get the last elements off the top of the stack first)
How do you add and remove from a stack?
You add and remove from the back
How do you build a queue with a Stack?
You need 2 stacks because:
Q: [10, 20, 30]
S1:[10, 20, 30]
You can only remove from back of stack and we need to remove 10 so we need to move everything from S1 to s2
S2:[30,20,10]
Describe a Stack?
A stack is a data structure that allows elements to be added and removed in a specific order (LIFO), and is commonly used in programming for function calls, recursion, and undo/redo functionality.
What are the two main stack operations?
push an element onto a stack, it is added to the top of the stack
pop an element from a stack, the element at the top of the stack is removed and returned
What 2 ways can we implement a stack?
A stack can be implemented using an array or a linked list.
When using an array, the stack has a fixed size, and the size cannot be changed dynamically.
When using a linked list, the size of the stack can grow or shrink dynamically as elements are added or removed.
Can you explain the difference between a Stack and a Queue and when you would use each?
Stack - LIFO
Queue - FIFO
Stack - 1 end
Queue - 2 ends
Stack - push, pop
Queue - enqueue dequeue
Stack - reverse, recursion problems
Queue - sequential processing based problems