Multiple parenthesis balancing problem using stacks

 Below is the source code of parenthesis balancing problem using stacks.

If you need any explanation then please feel free to contact me on my email i.e harshprasadaju@gmail.com.

Thankyou.

#include <stdio.h> //Ignore spellings error
#include <stdlib.h>
#include <string.h>
struct stack
{
    int top;
    int size;
    char *arr;
};
int isEmpty(struct stack *ptr)
{
    if (ptr->top == -1)
        return 1;
    return 0;
}
struct stack *createStack(int size)
{
    struct stack *s = (struct stack *)malloc(sizeof(struct stack));
    s->size = size;
    s->top = -1;
    s->arr = (char *)malloc(size * sizeof(char));
    return s;
}
void push(struct stack *ptrchar item)
{
    
        ptr->arr[++(ptr->top)] = item;
}
char pop(struct stack *ptr)
{
    char c;
    if (isEmpty(ptr))
    {
        printf("The paranthesis are not balanced .\n");
        exit(0);
    }
    else
        c = ptr->arr[ptr->top--];
    return c;
}
int isParanthesisMatched(char *expstruct stack *ptr)
{
    char ch;
    for (int i = 0i < strlen(exp); i++)
    {
        if (exp[i== '(')
            push(ptrexp[i]);
        if(exp[i]== '{')
        push(ptr , exp[i]);
        if(exp[i]== '[')
        push(ptr , exp[i]);
        if (exp[i== ')')
        {
            ch = pop(ptr);
            if (ch != '(')
            {
                printf("Paranthesis are not balanced \n");
                exit(0);
            }
        }
        if (exp[i== '}')
        {
            ch = pop(ptr);
            if (ch != '{')
            {
                printf("Paranthesis are not balanced \n");
                exit(0);
            }
        }
        if (exp[i== ']')
        {
            ch = pop(ptr);
            if (ch != '[')
            {
                printf("Paranthesis are not balanced \n");
                exit(0);
            }
        }

    }
    return 1;
}

int main()
{

    struct stack *op = createStack(10);
    char exp[100];
    puts("Enter the expresion\n");
    gets(exp);
    int n = isParanthesisMatchedexp , op);
    if(n == 1){
        printf("Paranthesis are balanced.\n");
    }
}

OUTPUT :
Enter the expression 473{39}[382{[3822}] Parenthesis are not balanced

Comments

Popular posts from this blog

Height of the Tree.

Insert and delete item in a BST