Best Data Structure to Check Balanced Parentheses in Expression
Stack is the best data structure to check balance of parentheses in Expression.
Last opened , first closed
Last closed, first opened
1. Scan from left to right.
2. If opening bracket, then add it to stack
3. If closing bracket, then remove last opening symbol from stack.
4. At end, stack should be empty.
In whole process, bracket is added or removed from same end of the list, so stack can be used here.
Implementation in C:
#include<stdio.h>
#include<stdlib.h>
#define bool int
struct node
{
char data;
struct node *next;
};
void push(struct node**,int);
int pop(struct node**);
bool isMatching(char character1, char character2)
{
if(character1 == '(' && character2 == ')')
return 1;
else if(character1 == '{' && character2 == '}')
return 1;
else if(character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
/*Return 1 if expression has balanced Parenthesis */
bool areBalanced(char exp[])
{
int i=0;
struct node *stack = NULL;
while(exp[i])
{
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
{
push(&stack, exp[i]);
}
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(stack == NULL)
return 0;
else if(!isMatching(pop(&stack), exp[i]))
return 0;
}
i++;
}
if(stack == NULL)
return 1;
else
return 0;
}
void main()
{
char exp[] = "{({}[])}";
if(areBalanced(exp))
printf("Balanced");
else
printf("Not Balanced");
}
void push(struct node** top, int new_data)
{
struct node *new = (struct node*) malloc(sizeof(struct node));
if(new == NULL)
{
printf("Stack is overflow");
exit(0);
}
new->data = new_data;
new->next = (*top);
(*top) = new;
}
int pop(struct node** top)
{
char res;
struct node *new;
if(*top == NULL)
{
printf("Stack is underflow");
exit(0);
}
else
{
new = *top;
res = new->data;
*top = new->next;
free(new);
return res;
}
}
Last opened , first closed
Last closed, first opened
1. Scan from left to right.
2. If opening bracket, then add it to stack
3. If closing bracket, then remove last opening symbol from stack.
4. At end, stack should be empty.
In whole process, bracket is added or removed from same end of the list, so stack can be used here.
Implementation in C:
#include<stdio.h>
#include<stdlib.h>
#define bool int
struct node
{
char data;
struct node *next;
};
void push(struct node**,int);
int pop(struct node**);
bool isMatching(char character1, char character2)
{
if(character1 == '(' && character2 == ')')
return 1;
else if(character1 == '{' && character2 == '}')
return 1;
else if(character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
/*Return 1 if expression has balanced Parenthesis */
bool areBalanced(char exp[])
{
int i=0;
struct node *stack = NULL;
while(exp[i])
{
if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
{
push(&stack, exp[i]);
}
else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
{
if(stack == NULL)
return 0;
else if(!isMatching(pop(&stack), exp[i]))
return 0;
}
i++;
}
if(stack == NULL)
return 1;
else
return 0;
}
void main()
{
char exp[] = "{({}[])}";
if(areBalanced(exp))
printf("Balanced");
else
printf("Not Balanced");
}
void push(struct node** top, int new_data)
{
struct node *new = (struct node*) malloc(sizeof(struct node));
if(new == NULL)
{
printf("Stack is overflow");
exit(0);
}
new->data = new_data;
new->next = (*top);
(*top) = new;
}
int pop(struct node** top)
{
char res;
struct node *new;
if(*top == NULL)
{
printf("Stack is underflow");
exit(0);
}
else
{
new = *top;
res = new->data;
*top = new->next;
free(new);
return res;
}
}
Comments
Post a Comment