-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackArray.c
More file actions
150 lines (147 loc) · 2.67 KB
/
StackArray.c
File metadata and controls
150 lines (147 loc) · 2.67 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define max 20
static char e[max];
static int top=-1;
void push(char x);
char toppop();
void display();
int isempty();
void makempty();
void pop();
void balpara();
void push(char x)
{
if(top==max-1)
printf("Stack overflow");
else
{
top++;
e[top]=x;
}
}
char toppop()
{
if(top==-1)
printf("Underflow");
else
{
return e[top];
top--;
}
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
void makempty()
{
while(top!=-1)
{
pop();
}
}
void display()
{
int i;
printf("Elements in a stack are\n");
for(i=0;i<=top;i++)
printf("%c\t",e[i]);
}
void pop()
{
if(top==-1)
printf("Underflow");
else
top--;
}
void balpara()
{
char exp[max], temp;
printf("Enter the expression: ");
scanf(" %s", exp);
int i = 0, len, flag = 0;
len = strlen(exp);
while (i < len)
{
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
push(exp[i]);
else
{
if (exp[i] == ')')
temp = '(';
else if (exp[i] == ']')
temp = '[';
else if (exp[i] == '}')
temp = '{';
else
{
i++;
continue;
}
char ch = toppop();
if (temp != ch)
{
flag = 1;
break;
}
}
i++;
}
if (flag == 1 && top!=-1)
printf("Not Balanced\n");
else
printf("Balanced\n");
}
int main()
{
int check,choice;
char c;
printf("MENU\n1.Push\n2.Pop\n3.Top pop\n4.Display\n5.Isempty\n6.Makempty\n7.Balancing Paranthesis\n8.Exit");
do
{
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the element to be push:");
scanf(" %c",&c);
push(c);
break;
case 2:
pop();
break;
case 3:
printf("Top element is %c",toppop());
break;
case 4:
display();
break;
case 5:
check=isempty();
if(check==0)
printf("Stack is not empty");
else
printf("Stack is empty");
break;
case 6:
makempty();
printf("Stack is emptied");
break;
case 7:
balpara();
break;
case 8:
break;
default:
printf("Invalid choice");
}
}while(choice!=8);
return 0;
}