Advertisement
alansam

Linesh Lanjewar's code

Oct 8th, 2021
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct stack
  4. {
  5.     int size;
  6.     int top;
  7.     char* arr;
  8. };
  9. int isEmpty(struct stack* sp)
  10. {
  11.     if(sp->top == -1){
  12.         return 1;
  13.     }
  14.     else{
  15.         return 0;
  16.     }
  17. }
  18. int isFull(struct stack* sp)
  19. {
  20.     if(sp->top == sp->size-1){
  21.         return 1;
  22.     }
  23.     else{
  24.         return 0;
  25.     }
  26. }
  27. void push(struct stack* sp, char val)
  28. {
  29.     if(isFull(sp)){
  30.         printf("STACK OVERFLOW\n");
  31.     }
  32.     else{
  33.         sp->top++;
  34.         sp->arr[sp->top] = val;
  35.     }
  36. }
  37. void pop(struct stack* sp)
  38. {
  39.     if(isEmpty(sp)){
  40.         printf("stack Underflow\n");
  41.         //return -1;
  42.     }
  43.     else{
  44.         char val = sp->arr[sp->top];
  45.         sp->top--;
  46.     }
  47. }
  48. int ParenthesisCheck(char* exp)
  49. {
  50.     struct stack* sp = (struct stack*)malloc(sizeof(struct stack));
  51.     sp->size = 100;
  52.     sp->top = -1;
  53.     char* arr = (char*)malloc(sp->size*sizeof(char));
  54.     for(int i=0;exp[i]!='\0';i++){
  55.         if(exp[i]=='{' || exp[i]=='[' || exp[i]=='('){
  56.             if(isFull(sp)){
  57.                 return 0;
  58.             }
  59.             else{
  60.                 push(sp,exp[i]);
  61.             }
  62.         }
  63.         else if(exp[i]=='}' || exp[i]==']' || exp[i]==')'){
  64.             if(isEmpty(sp)){
  65.                 return 0;
  66.             }
  67.             else{
  68.                 if(exp[i] == (sp->arr[sp->top])){
  69.                     pop(sp);
  70.                 }
  71.                 else{
  72.                     return 0;
  73.                 }
  74.             }
  75.         }
  76.     }
  77.     if(isEmpty(sp)){
  78.           return 1;
  79.      }
  80.      else{
  81.            return 0;
  82.       }
  83. }
  84. int main()
  85. {
  86.     char* exp = "{1+(1*5)}";
  87.     if(ParenthesisCheck(exp)){
  88.         printf("Parenthesis Are Matching.\n");
  89.     }
  90.     else{
  91.         printf("Parenthesis are not Matching\n");
  92.     }
  93.     return 0;
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement