Advertisement
kobikirmayer

Untitled

Mar 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #define MAX_SIZE 101
  2.  
  3. int A[MAX_SIZE];
  4. int top=-1;
  5.  
  6. void push(int x){
  7.      if (top== MAX_SIZE -1){
  8.         printf("Error: stack overflow\n");
  9.         return;
  10.      }
  11.      top++;
  12.      A[top]=x;
  13. }
  14.  
  15. void pop(){
  16.      if (top== -1){
  17.         printf("Error: No element to pop\n");
  18.         return;
  19.      }
  20.      top--;
  21. }
  22. int Top(){
  23.      return A[top];
  24. }
  25.  
  26. int isEmpty(){
  27.     if (top==-1) return 1;
  28.     else return 0;
  29. }
  30.  
  31. void printStack(){
  32.      int i;
  33.      printf("Stack: ");
  34.      for (i=0;i<=top;i++)
  35.         printf("%i ",A[i]);
  36.      printf("\n");
  37. }
  38. int main()
  39. {
  40.     char st[40];
  41.     printf("Enter a string:\n");
  42.     scanf("%s", st);
  43.  
  44.     int i=0;
  45.     while (i<strlen(st)){
  46.         if (st[i]== '(')  push(1);     // ()=1
  47.         if (st[i]== '[')  push(2);                     // []=2
  48.         if (st[i]== '{')  push(3);
  49.         if (st[i]== ')')
  50.             if(Top()==1)  pop(); else  i=100;
  51.          if (st[i]== ']')
  52.             if(Top()==2)  pop(); else  i=100;
  53.          if (st[i]== '}')
  54.             if(Top()==3)  pop(); else  i=100;
  55.          i++;
  56.     }
  57.  
  58.      (isEmpty() )? printf("\nEmpty\n"):printf("\nNo Empty\n");
  59.  
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement