0% found this document useful (0 votes)
35 views3 pages

Program-7: AIM: Program To Check For Balance Parenthesis. Description

This C program takes an expression as input and checks if the parentheses are balanced using a stack. It pushes opening parentheses onto the stack and pops elements to check for matching closing parentheses. It returns 1 if balanced or 0 if not balanced. The main function tests the input "{()}[]" which is correctly balanced, so it prints "Balanced".

Uploaded by

Ajay Kumar
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)
35 views3 pages

Program-7: AIM: Program To Check For Balance Parenthesis. Description

This C program takes an expression as input and checks if the parentheses are balanced using a stack. It pushes opening parentheses onto the stack and pops elements to check for matching closing parentheses. It returns 1 if balanced or 0 if not balanced. The main function tests the input "{()}[]" which is correctly balanced, so it prints "Balanced".

Uploaded by

Ajay Kumar
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/ 3

PROGRAM-7

AIM: Program to check for Balance Parenthesis.


DESCRIPTION:
This program takes an expression as input and checks if the expression is correctly
parenthesized.
CODE:
#include<stdio.h>

#include<stdlib.h>

#define bool int

struct sNode
{
char data;
struct sNode *next;
};

void push(struct sNode** top_ref, int new_data);

int pop(struct sNode** top_ref);

bool isMatchingPair(char character1, char character2)


{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}

bool areParenthesisBalanced(char exp[])


{
int i = 0;

struct sNode *stack = NULL;

1|2k18/SE/014
while (exp[i])
{
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);

if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')


{

if (stack == NULL)
return 0;

else if ( !isMatchingPair(pop(&stack), exp[i]) )


return 0;
}
i++;
}

if (stack == NULL)
return 1;
else
return 0;
}

int main()
{
char exp[100] = "{()}[]"; //input
if (areParenthesisBalanced(exp))
printf("Balanced \n");
else
printf("Not Balanced \n");
return 0;
}

void push(struct sNode** top_ref, int new_data)


{
struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));

if (new_node == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);

2|2k18/SE/014
}

new_node->data = new_data;

new_node->next = (*top_ref);

(*top_ref) = new_node;
}

int pop(struct sNode** top_ref)


{
char res;
struct sNode *top;

if (*top_ref == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}

OUTPUT:

3|2k18/SE/014

You might also like