Which data structure is commonly used to check whether parentheses are balanced in an expression?

Options

  • A. Queue
  • B. Linked List
  • C. Stack
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. Stack

Detailed Explanation

Correct Answer: C. Stack

Explanation: Stack is used to check balanced parentheses.

  • Push every opening bracket onto the stack.
  • For a closing bracket, check the top opening bracket.
  • Pop when the brackets match.
  • The expression is balanced if the stack is empty at the end and no mismatch occurs.

Example: (A+B) is balanced, whereas (A+B is not balanced.