-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathparanthesis_matching_problem_stack.c
More file actions
94 lines (82 loc) · 1.52 KB
/
paranthesis_matching_problem_stack.c
File metadata and controls
94 lines (82 loc) · 1.52 KB
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
86
87
88
89
90
91
92
93
94
// author-akshat khatri
// date-14-08-2023
// solving paranthesis matching problem using stacks
#include <stdio.h>
#include <stdlib.h>
struct node
{
char symbol;
struct node *next;
};
struct node *create_node(char symbol)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
if (ptr == NULL)
{
printf("memory error\n");
exit(EXIT_FAILURE);
}
else
{
ptr->symbol = symbol;
return ptr;
}
}
int is_empty(struct node *stack_node)
{
if (stack_node == NULL)
{
return 1;
}
else
{
return 0;
}
}
void push(struct node **top, char symbol)
{
struct node *temp = create_node(symbol);
temp->next = *top;
*top = temp;
}
void pop(struct node **top)
{
if (is_empty(*top))
{
printf("unbalanced parenthesis\n");
exit(EXIT_SUCCESS);
}
struct node *temp = *top;
*top = (*top)->next;
free(temp);
}
int main()
{
struct node *top = NULL;
int size;
printf("enter the size of the expression\n");
scanf("%d", &size);
char arr[size + 10];
for (int i = 0; i < size; i++)
{
printf("Enter the %dth character: ", i + 1);
scanf(" %c", &arr[i]);
if (arr[i] == '(')
{
push(&top, '(');
}
else if (arr[i] == ')')
{
pop(&top);
}
}
if (top == NULL)
{
printf("balanced paranthesis\n");
}
else
{
printf("unbalanced paranthesis\n");
}
return 0;
}