Sunday 29 September 2024

Write a Program to Checking of balanced parenthesis using stack

 Method 1


#include <stdio.h>

#include <string.h>


#define MAX 100


// Global variables for stack

char stack[MAX];

int top;


// Initialize the stack

void initStack() {

    top = -1;

}


// Push an element onto the stack

void push(char item) {

    if (top == (MAX - 1)) {

        printf("Stack is Full\n");

    } else {

        stack[++top] = item;  // Increment first, then add the item

    }

}


// Pop an element from the stack

void pop() {

    if (top == -1) {

        printf("Stack underflow! Cannot pop\n");

    } else {

        --top;  // Decrement the top

    }

}


// Check if the stack is empty

int isEmpty() {

    return top == -1;

}


// Check the top element of the stack

char peek() {

    if (!isEmpty()) {

        return stack[top];

    }

    return '\0';  // Return null character if stack is empty

}


// Function to check if parentheses in an expression are balanced

int Balanced(char expression[]) {

    initStack();  // Initialize the stack


    for (int i = 0; i < strlen(expression); i++) {

        char ch = expression[i];


        switch (ch) {

            // Push opening brackets onto the stack

            case '(':

            case '{':

            case '[':

                push(ch);

                break;


            // Check for matching opening bracket

            case ')':

                if (isEmpty() || peek() != '(') 

                return 0;

                pop();

                break;

            case '}':

                if (isEmpty() || peek() != '{') 

                  return 0;

                pop();

                break;

            case ']':

                if (isEmpty() || peek() != '[')

                        return 0;

                pop();

                break;

        }

    }


    // If the stack is empty, all brackets were matched

    return isEmpty();

}


int main() {

    char expression[MAX];


    // Input the expression

    printf("Enter an expression: ");

    scanf("%s", expression);


    // Check if the parentheses are balanced

    if (Balanced(expression)) {

        printf("Parentheses are balanced.\n");

    } else {

        printf("Parentheses are not balanced.\n");

    }


    return 0;

}


Method2

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


#define MAX 100


struct stack {

    char st[MAX];

    int top;

}s;


// Initialize the stack

void initStack() {

    s.top = -1;

}


// Push an element onto the stack

void push(char item) {

    if (s.top == (MAX - 1)) {

        printf("Stack is Full\n");

    } else {

        s.st[++s.top] = item;  // Increment first, then add the item

    }

}


// Pop an element from the stack

void pop() {

    if (s.top == -1) {

        printf("Stack underflow! Cannot pop\n");

    } else {

        --s.top;  // Decrement the top

    }

}


// Check if two characters are a matching pair of parentheses

int checkPair(char val1, char val2) {

    return ((val1 == '(' && val2 == ')') ||

            (val1 == '[' && val2 == ']') ||

            (val1 == '{' && val2 == '}'));

}


// Check if the stack is empty

int isEmpty() {

    return s.top == -1;

}


// Function to check if parentheses in an expression are balanced using switch-case

int checkBalanced(char expression[], int len) {

    initStack();  // Initialize the stack


    // Traverse the expression using a for loop

    for (int i = 0; i < len; i++) {

        char ch = expression[i];


        // Use switch-case to handle different characters

        switch (ch) {

            // If an opening bracket is found, push it onto the stack

            case '(':

            case '{':

            case '[':

                push(ch);

                break;


            // If a closing bracket is found, check for a matching opening bracket

            case ')':

            case '}':

            case ']':

                if (isEmpty() || !checkPair(s.st[s.top], ch)) {

                    return 0;  // Not balanced if no matching opening bracket

                } else {

                    pop();  // Pop the matched opening bracket

                }

                break;

        }

    }


    // If the stack is empty, the parentheses were balanced

    return isEmpty();

}


int main() {

    char expression[MAX];


    // Input the expression

    printf("Enter an expression: ");

    scanf("%s", expression);


    char len = strlen(expression);  // Get the length of the expression u can use int len


    // Check if the parentheses are balanced

    if (checkBalanced(expression, len)) {

        printf("Parentheses are balanced.\n");

    } else {

        printf("Parentheses are not balanced.\n");

    }

    return 0;

}