Stacks Flashcards
Hint of Stack
Ins and outs
is it better than array?
It is favorable to store data in a stack than array
What does stack use?
LIFO- Last in First out
Operations-
empty() size() top() push(a) pop()
empty()
empty() – Returns whether the stack is empty – Time Complexity: O(1)
size()
size() – Returns the size of the stack – Time Complexity: O(1)
top()
top() – Returns the topmost element of the stack – Time Complexity: O(1)
pop()
pop() – Deletes the topmost element of the stack – Time Complexity: O(1)
append()
append(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity: O(1)
Does stack offer constant time access to the ith item?
Stack does not offer constant time access to the ith item.
Constant time access-
only append() and pop() has the constant time access because it doesn’t shift elements around
Is stack linked list?
Stack can be implemented as linked list if the items are added and removed from the same side.
Stack in Recursive algorithms
Append temporary data onto a stack as we recurse, but remove as we bcktrack
Can stack help recursive algorithm to iterate?
It can be use to implement iteratively.