A simple C++ DSA project that checks whether the brackets in a given expression are balanced using the Stack data structure.
- Checks
(),{}, and[] - Supports nested brackets
- Supports mathematical expressions containing variables and operators
- Detects mismatched brackets
- Uses Stack for efficient bracket matching
Input: {[()]}
Output: Expression is BALANCED
Input: ([)]
Output: Expression is NOT BALANCED
Input: a+(b*c)
Output: Expression is BALANCED
Stack
Opening brackets are pushed into the stack.
When a closing bracket is encountered, it is compared with the bracket at the top of the stack.
If they match, the opening bracket is removed using pop().
At the end, if the stack is empty, the expression is balanced.
- Read the expression.
- Traverse each character.
- Push opening brackets into the stack.
- For a closing bracket, check the top of the stack.
- If the brackets match, pop the stack.
- If they do not match, the expression is not balanced.
- If the stack is empty after traversal, the expression is balanced.
- Time Complexity: O(n)
- Space Complexity: O(n)
- C++
- STL Stack
- String
Compile:
g++ main.cpp -o checker
Run:
./checker