Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<ctype.h>
- #include<stdlib.h>
- struct node
- {
- char data;
- struct node *next;
- } *start=NULL;
- void push(char);
- char pop(void);
- void print();
- void main()
- {
- char in='\0';
- while(in!='\n')
- {
- in=getchar();
- if(in =='(' || in=='[' || in=='{')
- {
- push(in);
- // print();
- continue;
- }
- else if (in ==')')
- {
- if((pop())+1==in)
- {
- // print();
- continue;
- }
- else
- {
- push(in-1);
- continue;
- }
- }
- else if (in ==']')
- {
- if((pop())+2==in)
- {
- // print();
- continue;
- }
- else
- {
- push(in-2);
- continue;
- }
- }
- else if (in =='}')
- {
- if((pop())+2==in)
- {
- // print();
- continue;
- }
- else
- {
- push(in-2);
- continue;
- }
- }
- else if(isalnum(in) || in=='+' || in=='-' || in=='*' || in=='/')
- {
- // print();
- continue;
- }
- else if(in!='\n')
- {
- printf("\nInvalid Expression\n");
- exit(1);
- }
- }
- if(start!=NULL)
- {
- printf("\nExpression is invalid\n");
- exit(1);
- }
- printf("\nExpression is valid\n");
- }
- void push(char element)
- {
- struct node *new;
- new=(struct node *) (malloc(sizeof(struct node)));
- new->data=element;
- new->next=NULL;
- if(start==NULL)
- {
- start=new;
- return;
- }
- new->next=start;
- start=new;
- }
- char pop()
- {
- struct node *p;
- if(start==NULL) //if empty
- {
- return('!');
- }
- else
- {
- p=start;
- start=start->next;
- return(p->data);
- }
- }
- void print()
- {
- if(start==NULL)
- {
- printf("\nNo data in Linked List\n");
- return;
- }
- struct node *p;
- p=start;
- printf("\nLinked list: \n");
- while(p!=NULL)
- {
- printf("\n%c",p->data);
- p=p->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement