TRAIN TICKET RESERVATION SYSTEM
INTERNAL PROJECT
for the subject
DATA STRUCTURES
Submitted by
Karan Singh
Roll no. 90
Team Members:
Rushikesh Ahirrao Roll no. 94
Karan Singh Roll no. 90
M.C.A (2020-2022)
Department of M.C.A
SIES COLLEGE OF MANAGEMENT STUDIES
NERUL, NAVI MUMBAI
Karan Singh Roll no. 90
Parentheses checking using stack operation
Problem Statement:
The following C++ code checks whether the parentheses, square
brackets, and curly braces in a given expression are balanced. Here's how
it works:
The code defines a function areParenthesesBalanced() which takes a
string expr as input and returns a boolean value indicating whether the
parentheses are balanced or not.
Inside the function, it iterates through each character of the input
expression.
If an opening parenthesis, square bracket, or curly brace is encountered, it
pushes it onto a stack.
If a closing parenthesis, square bracket, or curly brace is encountered, it
checks if the stack is empty. If it is empty, it means there is a closing
bracket without a corresponding opening bracket, so the function returns
false.
If the stack is not empty, it pops the top element from the stack and
checks if it matches the current closing bracket. If it doesn't match, it
means the brackets are not balanced, so the function returns false.
After traversing the entire expression, if the stack is empty, it means all
brackets are balanced, and the function returns true. Otherwise, it returns
false.
In the main() function, the program prompts the user to input an
expression, and then it calls the areParenthesesBalanced() function to
check if the parentheses are balanced in the input expression.
Finally, it prints a message indicating whether the parentheses are
balanced or not.
2|Page
Karan Singh Roll no. 90
Code:
#include <iostream>
#include <stack>
#include <string>
bool areParenthesesBalanced(std::string expr) {
std::stack<char> s;
char x;
// Traversing the Expression
for (int i = 0; i < expr.length(); i++) {
if (expr[i] == '(' || expr[i] == '[' || expr[i] == '{') {
// Push the element in the stack
s.push(expr[i]);
continue;
}
// If current character is not opening bracket, then it must be a
closing one
if (s.empty())
return false;
switch (expr[i]) {
case ')':
3|Page
Karan Singh Roll no. 90
// Store the top element in a
x = s.top();
s.pop();
if (x == '{' || x == '[')
return false;
break;
case '}':
// Store the top element in b
x = s.top();
s.pop();
if (x == '(' || x == '[')
return false;
break;
case ']':
// Store the top element in c
x = s.top();
s.pop();
if (x == '(' || x == '{')
return false;
break;
}
4|Page
Karan Singh Roll no. 90
// Check Empty Stack
return (s.empty());
}
// Main function for interactive input
int main() {
std::string expr;
std::cout << "Enter an expression: ";
std::cin >> expr;
if (areParenthesesBalanced(expr))
std::cout << "Parentheses are balanced.\n";
else
std::cout << "Parentheses are balanced.\n";
return 0;
}
Implementation:
1)
5|Page
Karan Singh Roll no. 90
2) Giving wrong input-
6|Page
Karan Singh Roll no. 90
3) Output-
7|Page
Karan Singh Roll no. 90
4) Giving right input-
8) Output-
8|Page