forked from MohmedIkram/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bracket Balancer Program.c
85 lines (84 loc) · 1.54 KB
/
Bracket Balancer Program.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Stack operations using Linked List
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct
{
struct node * top;
}STACK;
int push(STACK *s,int v)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Overflow");
return 1;
}
temp->data=v;
temp->next=s->top;
s->top=temp;
return 0;
}
int pop(STACK *s,struct node **v)
{
struct node *temp;
if(s->top==NULL)
{
printf("Underflow");
return 1;
}
temp=s->top;
s->top=temp->next;
*v=temp;
return 0;
}
int Bracket(char ex[], STACK *s)
{
struct node *m;
int i=0;
while(ex[i])
{
if (ex[i] == '{' || ex[i] == '(' || ex[i] == '[')
{
int p=push(&s,(int)ex[i]);
}
if (ex[i] == '}' || ex[i] == ')' || ex[i] == ']')
{
if(s==NULL)
{
return 0;
}
else
{
pop(&s,&m);
int a= ex[i]==')' && m->data == '(';
int b= ex[i]=='}' && m->data == '{';
int c= ex[i]==']' && m->data == '[';
if(!(a || b || c))
return 0;
}
}
i++;
}
if(s->top==NULL)
return 1;
else
return 0;
}
int main()
{
STACK s1;
s1.top = NULL;
char ex[100];
gets(ex);
int a=Bracket(ex, &s1);
if(a==1)
printf("Balanced\n");
else
printf("Unbalanced\n");
}