Stack/ Queue
β Stack
input, output same direction
LIFO
π‘ usage
- DFS
- λ¬Έμμ΄ μμ
- μ°μ°μ νμνκΈ°λ²
π‘ methods
push
pop
isEmpty
isFull
: requires MAX_SIZE
β Queue
input, output different direction
front: deQueue
rear: enQueue
FIFO
π‘ usage
- linked list
- buffer
- BFS
βοΈ Can you make a queue with stack?
YES, with two stacks
I can make a queue
- main stack enque 12345
1
main stack: 12345
- pop main stack, enqueue in substack
1
2
main stack:
sub stack: 54321
- Deque: pop from substack
1
2
3
4
5
main stack:
sub stack: 5432
pop: 1
βοΈ like queue FIFO, first number 1 is popped
- Enqueue: pop substack, enqueue in mainstack then enqueue new number
6
1
2
3
main stack: 23456
substack:
βοΈ like queue FIFO, 6 is added to the end
βοΈ Can you share stack resource to another stack?
- cannot directly
- but can share
data structure that can store stack
- such as
array
- such as
- and thus, share resource
βοΈ Homogeneity
- queue and stack are consisted of same data type
- linear data type, keeps order
π‘ Reference
This post is licensed under CC BY 4.0 by the author.