Post

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

  1. main stack enque 12345
1
main stack: 12345
  1. pop main stack, enqueue in substack
1
2
main stack:
sub stack: 54321
  1. Deque: pop from substack
1
2
3
4
5
main stack:
sub stack: 5432

pop: 1
⭐️ like queue FIFO, first number 1 is popped
  1. 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
  • and thus, share resource

⭐️ Homogeneity

  • queue and stack are consisted of same data type
  • linear data type, keeps order

πŸ’‘ Reference

https://soheeparklee.github.io/posts/JAVA_stack_queue/

This post is licensed under CC BY 4.0 by the author.