Arnab_Manna

application of stack parenthisis infix to postfix

Jun 3rd, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. typedef unsigned char BOOLEAN;
  10.  
  11. typedef struct NODE_tag
  12. {
  13.     char data;
  14.     struct NODE_tag *llink;
  15.     struct NODE_tag *rlink;
  16. }NODE;
  17.  
  18. NODE *getnode()
  19. {
  20.     NODE *NEW;
  21.     NEW=(NODE *)malloc(sizeof(NODE));
  22.     NEW->llink=NULL;
  23.     NEW->rlink=NULL;
  24.     return NEW;
  25. }
  26.  
  27. typedef struct stack
  28. {
  29.     NODE *TOP;
  30. }STACK;
  31.  
  32. STACK initialise(STACK S)
  33. {
  34.     S.TOP=NULL;
  35.     return S;
  36. }
  37.  
  38. BOOLEAN isFull(STACK S)
  39. {
  40.     NODE *NEW;
  41.     NEW=getnode();
  42.     if(NEW==NULL)
  43.     {
  44.         return TRUE;
  45.     }
  46.     else
  47.     {
  48.     return FALSE;
  49.     free(NEW);
  50.     }
  51. }
  52.    
  53. BOOLEAN isEmpty(STACK S)
  54. {
  55.     if(S.TOP==NULL)
  56.     {
  57.         return TRUE;
  58.     }
  59.     else
  60.     {
  61.         return FALSE;
  62.     }
  63. }
  64.  
  65.  
  66. STACK Push(STACK S, char x)
  67. {
  68.     NODE *New;
  69.     New = getnode();
  70.     if(New==NULL)
  71.         {
  72.             printf("\n STACK overflow error");
  73.             return S;
  74.         }
  75.     New->data = x;
  76.     if (S.TOP== NULL)
  77.     {
  78.         S.TOP = New;
  79.     }
  80.     else
  81.     {
  82.         New->rlink=S.TOP;
  83.         S.TOP->llink = New;
  84.         S.TOP=New;
  85.     }
  86.     return S;
  87. }
  88.  
  89. STACK Pop(STACK S, char *x)
  90. {
  91.     NODE *Del;
  92.     if (S.TOP == NULL)
  93.     {
  94.         printf("\n STACK underflow error");
  95.         return S;
  96.     }
  97.     Del = S.TOP;
  98.     S.TOP = Del->rlink;
  99.     if (S.TOP!=NULL)
  100.     {
  101.         S.TOP->llink = NULL;
  102.         Del->rlink = NULL;
  103.     }
  104.     *x = Del->data;
  105.     free(Del);
  106.     return S;
  107. }
  108.  
  109. void display(STACK S)
  110. {
  111.     NODE *Ptr=S.TOP;
  112.     if (S.TOP == NULL)
  113.     {
  114.         printf("\nEmpty STACK");
  115.         return;
  116.     }
  117.     printf("\nCurrent content of the STACK:");
  118.     while(1)
  119.     {
  120.         if (Ptr==NULL) return;
  121.         printf("%c ", Ptr->data);
  122.         Ptr=Ptr->rlink;
  123.     }
  124.     printf("\n");
  125. }
  126.  
  127. BOOLEAN brackets(char c)
  128. {
  129.     if(c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']')
  130.     return TRUE;
  131.     else
  132.     return FALSE;
  133. }
  134. void arrange(char *ch)
  135. {
  136.     STACK S;
  137.     int i=0;
  138.     char f;
  139.     S = initialise(S);
  140.     while(ch[i]!='\0')
  141.     {
  142.         display(S);
  143.         if(brackets(ch[i])==TRUE)
  144.         {
  145.             switch (ch[i])
  146.             {
  147.                 case '(':
  148.                 S=Push(S,ch[i]);
  149.                 break;
  150.                 case '{':
  151.                 S=Push(S,ch[i]);
  152.                 break;
  153.                 case '[':
  154.                 S=Push(S,ch[i]);
  155.                 break;
  156.                 case ']':
  157.                 S=Pop(S,&f);
  158.                 break;
  159.                 case '}':
  160.                 S=Pop(S,&f);
  161.                 break;
  162.                 case ')':
  163.                 S=Pop(S,&f);
  164.                 break;
  165.             }
  166.         }
  167.         else
  168.         {
  169.             S=Pop(S,&f);
  170.         }
  171.         i++;
  172.     }
  173. if(S.TOP->rlink==NULL)
  174. {
  175.  return;
  176. }
  177. }
  178. int main()
  179. {
  180.     char st[250];
  181.     int i;
  182.     printf("enter a suquence ");
  183.     gets(st);
  184.     for(i=0;;i++)
  185.     {
  186.         if(st[i]=='\0')
  187.         {
  188.             st[i]=' ';
  189.             st[i+1]='\0';
  190.             break;
  191.         }
  192.     }
  193.     arrange(st);
  194.     return 0;
  195. }
Add Comment
Please, Sign In to add comment