

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Check for balanced paranthesis by using Stacks
Here we will discuss how to check the balanced brackets using stacks. We not only check the opening and closing brackets but also check the ordering of brackets. For an example we can say that the expression "[{} () {()}]" it is correct, but "{[}]" it is not correct.
Input: Some expression with brackets "{()}[]" Output: They are balanced
Algorithm
Step 1: Define a stack to hold brackets Step 2: Traverse the expression from left to right Step 2.1: If the character is opening bracket (, or { or [, then push it into stack Step 2.2: If the character is closing bracket ), } or ] Then pop from stack, and if the popped character is matched with the starting bracket then it is ok. otherwise they are not balanced. Step 3: After traversal if the starting bracket is present in the stack then it is not balanced.
Example Code
#include<iostream> #include<stack> using namespace std; bool isBalanced(string expr) { stack<char> s; char ch; for (int i=0; i<expr.length(); i++) { //for each character in the expression, check conditions if (expr[i]=='('||expr[i]=='['||expr[i]=='{') { //when it is opening bracket, push into stack s.push(expr[i]); continue; } if (s.empty()) //stack cannot be empty as it is not opening bracket, there must be closing bracket return false; switch (expr[i]) { case ')': //for closing parenthesis, pop it and check for braces and square brackets ch = s.top(); s.pop(); if (ch=='{' || ch=='[') return false; break; case '}': //for closing braces, pop it and check for parenthesis and square brackets ch = s.top(); s.pop(); if (ch=='(' || ch=='[') return false; break; case ']': //for closing square bracket, pop it and check for braces and parenthesis ch = s.top(); s.pop(); if (ch =='(' || ch == '{') return false; break; } } return (s.empty()); //when stack is empty, return true } main() { string expr = "[{}(){()}]"; if (isBalanced(expr)) cout << "Balanced"; else cout << "Not Balanced"; }
Output
Balanced
- Related Questions & Answers
- Check for balanced parentheses in Python
- Python Program to Implement Queues using Stacks
- C++ Program to Evaluate an Expression using Stacks
- C++ Program to Implement Queue Using Two Stacks
- Java Program to Reverse a String Using Stacks
- Check for balanced parentheses in an expression in C++
- Program to check whether parentheses are balanced or not in Python
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- C++ Program to check if two stacks of letters can be emptied or not
- Program to check whether a tree is height balanced or not in C++
- JVM Stacks
- C program to print multiplication table by using for Loop
- Program to check maximum sum of all stacks after popping some elements from them in Python
- 8085 Program to check for palindrome
- C++ program to check for ISBN
Advertisements