Unit 3 | Lec 4 | Applications of Stacks | Checking Valid or Invalid Parentheses | Data Structures

‪@VimalKaushik-g3t‬ Please Like, Share, and Subscribe. If any doubts or feedback, please comment below. In this video, we discussed about 1. Introduction to stacks 2. Applications of stacks 2.2 Checking whether parentheses are valid or invalid. Here is the C program for implementing, #include(less than symbol)stdio.h(greater than symbol) #include(less than symbol)stdlib.h(greater than symbol) #include(less than symbol)stdbool.h(greater than symbol) #define max 20 int top = -1; char stack[max]; void push(char ch) { stack[++top] = ch; } char pop() { return stack[top--]; } bool isEmpty() { if(top == -1) { return true; } return false; } int main() { char input[max]; printf("Enter the string: "); scanf("%s",input); for(int i=0 ; input[i]!='\0' ; i++) { if(input[i] == '(' || input[i] == '[' || input[i] == '{') { push(input[i]); } else if(input[i] == ')' || input[i] == ']' || input[i] == '}') { if(isEmpty()) { printf("Invalid parantheses\n"); return 0; } char ch = pop(); if((input[i] == ')' && ch != '(') || (input[i] == ']' && ch != '[') || (input[i] == '}' && ch != '{')) { printf("Invalid parantheses\n"); return 0; } } } if(isEmpty()) { printf("Valid parantheses\n"); } else { printf("Invalid parantheses\n"); } return 0; }