Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #include<string.h>
- #define TRUE 1
- #define FALSE 0
- typedef unsigned char BOOLEAN;
- typedef struct NODE_tag
- {
- char data;
- struct NODE_tag *llink;
- struct NODE_tag *rlink;
- }NODE;
- NODE *getnode()
- {
- NODE *NEW;
- NEW=(NODE *)malloc(sizeof(NODE));
- NEW->llink=NULL;
- NEW->rlink=NULL;
- return NEW;
- }
- typedef struct stack
- {
- NODE *TOP;
- }STACK;
- STACK initialise(STACK S)
- {
- S.TOP=NULL;
- return S;
- }
- BOOLEAN isFull(STACK S)
- {
- NODE *NEW;
- NEW=getnode();
- if(NEW==NULL)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- free(NEW);
- }
- }
- BOOLEAN isEmpty(STACK S)
- {
- if(S.TOP==NULL)
- {
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- STACK Push(STACK S, char x)
- {
- NODE *New;
- New = getnode();
- if(New==NULL)
- {
- printf("\n STACK overflow error");
- return S;
- }
- New->data = x;
- if (S.TOP== NULL)
- {
- S.TOP = New;
- }
- else
- {
- New->rlink=S.TOP;
- S.TOP->llink = New;
- S.TOP=New;
- }
- return S;
- }
- STACK Pop(STACK S, char *x)
- {
- NODE *Del;
- if (S.TOP == NULL)
- {
- printf("\n STACK underflow error");
- return S;
- }
- Del = S.TOP;
- S.TOP = Del->rlink;
- if (S.TOP!=NULL)
- {
- S.TOP->llink = NULL;
- Del->rlink = NULL;
- }
- *x = Del->data;
- free(Del);
- return S;
- }
- void display(STACK S)
- {
- NODE *Ptr=S.TOP;
- if (S.TOP == NULL)
- {
- printf("\nEmpty STACK");
- return;
- }
- printf("\nCurrent content of the STACK:");
- while(1)
- {
- if (Ptr==NULL) return;
- printf("%c ", Ptr->data);
- Ptr=Ptr->rlink;
- }
- printf("\n");
- }
- BOOLEAN brackets(char c)
- {
- if(c=='(' || c==')' || c=='{' || c=='}' || c=='[' || c==']')
- return TRUE;
- else
- return FALSE;
- }
- void arrange(char *ch)
- {
- STACK S;
- int i=0;
- char f;
- S = initialise(S);
- while(ch[i]!='\0')
- {
- display(S);
- if(brackets(ch[i])==TRUE)
- {
- switch (ch[i])
- {
- case '(':
- S=Push(S,ch[i]);
- break;
- case '{':
- S=Push(S,ch[i]);
- break;
- case '[':
- S=Push(S,ch[i]);
- break;
- case ']':
- S=Pop(S,&f);
- break;
- case '}':
- S=Pop(S,&f);
- break;
- case ')':
- S=Pop(S,&f);
- break;
- }
- }
- else
- {
- S=Pop(S,&f);
- }
- i++;
- }
- if(S.TOP->rlink==NULL)
- {
- return;
- }
- }
- int main()
- {
- char st[250];
- int i;
- printf("enter a suquence ");
- gets(st);
- for(i=0;;i++)
- {
- if(st[i]=='\0')
- {
- st[i]=' ';
- st[i+1]='\0';
- break;
- }
- }
- arrange(st);
- return 0;
- }
Add Comment
Please, Sign In to add comment