Which of the following data structures can be used for parentheses matching?
Asked In: BPSC TRE 3.0Options
- A. priority queue
- B. queue
- C. n-ary tree
- D. stack
D. stack
Parentheses matching requires:
Checking opening brackets: ( { [
Matching them with corresponding closing brackets: ) } ]
A Stack follows LIFO (Last In, First Out) principle:
Push opening bracket onto stack
When closing bracket appears → pop from stack
Check if it matches
If stack is empty at end → parentheses are balanced