0% found this document useful (0 votes)
36 views6 pages

DSA Micro Project Report Format

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

DSA Micro Project Report Format

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Affiliated to Gujarat Technological University

Micro Project Report

Parenthesis Matching Using Stack Data


Structure
Branch Computer Engineering
Semester 3RD (Div-B)
Subject Name Data Structures and Algorithms
Subject Code 4330704

Group Member
Sr. No. Enrollment Number Name
1 236020307150 PARMAR MANTHANRAJSINH NIKUNJ
2 236020307147 PARMAR DHAVAL HASMUKHBHAI
3 236020307117 MARADIYA DHYEY ALPESHKUMAR
4 236020307138 PANDYA LAKSHANK NAIMISHKUMAR
This code implements a stack-based approach to check if parentheses in an expression are
balanced. It defines a `Stack` class with methods to push and pop elements, and a
`Parenthesis_check` function that uses this stack to verify if each opening parenthesis `(`
has a corresponding closing parenthesis `)`. The program takes an expression as input and
determines whether the parentheses are balanced or not.

Code:
#include <iostream>
#include <string>
using namespace std;

class Stack
{
private:
char *arr;
int size;
int top;

public:
Stack(int size);

int push(char value);


int pop();
void print_stack();
friend int Parenthesis_check(string str);
};

Stack::Stack(int size)
{
this->size = size;
top = -1;
arr = new char[size];
}

int Stack::push(char value)


{
if (top < size - 1)
{
arr[++top] = value;
return 1;
}
else
{
return 0;
}
}

int Stack::pop()
{
if (top >= 0)
{
top--;
return 1;
}
else
{
return 0;
}
}

void Stack::print_stack()
{
for (int i = top; i != -1; i--)
{
cout << arr[i] << " ";
}
cout << endl;
}

int Parenthesis_check(string str)


{
Stack s(str.length());
for (int i = 0; i < str.length(); i++)
{
if (str[i] == '(')
{
s.push(str[i]);
}
else if (str[i] == ')')
{
if (s.pop() == 0)
{
cout << "Parenthesis is not balanced" << endl;
return 0;
}
}
}

if (s.top == -1)
{
cout << "Parenthesis is balanced" << endl;
return 1;
}
else
{
cout << "Parenthesis is not balanced" << endl;
return 0;
}
}

int main()
{
string str;
cout << "Enter a expression : ";
cin >> str;
Parenthesis_check(str);

return 0;
}

Output:

Enter a expression : ((())()(()))

Parenthesis is balanced

You might also like