Stacks Flashcards
Learn about what a stack is and what stack overflows are
What is a stack overflow?
A stack overflow is an error that occurs when a program exceeds the maximum amount of memory that is allocated for its execution.
What is a stack?
A stack is a data structure that can hold many elements.
How are elements organised in a stack?
LIFO - Last in first out
What are the operations you can do on a stack?
- push()
- pop()
- peek()
- isEmpty()
- size()
What are the two ways you can implement stacks?
Arrays and linked lists.
What can stacks be used for?
Implementing undo mechanisms, revert to previous states, create algorithms, performing depth-first searches in graphs and backtracking.
What does pop() do?
Removes and returns the top element from the stack.
What does push() do?
Adds a new element to the stack.
What does peek() do?
Returns the top element from the stack.
What does isEmpty() do?
Checks if the stack is empty.
What does size() do?
Finds the number of elements in the stack.
What are some reasons to implement stacks using arrays?
Memory Efficient - Array elements do not hold the next elements address like linked lists and nodes do.
Easier to implement and understand - Using arrays to implement stacks requires less code than using linked lists, and for this reason it is typically easier to understand as well.