Application of stack
So recently came across this question where an expression is given with some parenthesis and we are told to check whether the expression is balanced or not.
This is what i have done till now, but i cant seem to pop the elements from the stack. When i pop it in the else part of the function i can seem to properly compare it.
How can i make my code work, and what is the reason for the simple comparison condition not to work?
#include <stdio.h>
#include <stdbool.h>
char stack[30];
int top = 0;
bool spec = 0;
void push(char x){
if (top == 30)
printf("Full\n");
else{
stack[top] = x;
top++;
}
}
void pop(char x){
if (top == 0)
spec = 1;
else{
if (x == stack[top-1])
top--;
}
}
int main()
{
char temp[30];
scanf("%s", temp);
for (int i = 0; temp[i] != '\0'; i++)
{
if (temp[i] == '(' || temp[i] == '{' || temp[i] == '[')
push(temp[i]);
else if (temp[i] == ')' || temp[i] == '}' || temp[i] == ']')
pop(temp[i]);
}
for (int i = 0; i < top; i++){
printf("%c", stack[i]);
}
if (top != 0)
printf("Unbalanced");
else
printf("Balanced");
return 0;
}