Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct stack
- {
- int size;
- int top;
- char* arr;
- };
- int isEmpty(struct stack* sp)
- {
- if(sp->top == -1){
- return 1;
- }
- else{
- return 0;
- }
- }
- int isFull(struct stack* sp)
- {
- if(sp->top == sp->size-1){
- return 1;
- }
- else{
- return 0;
- }
- }
- void push(struct stack* sp, char val)
- {
- if(isFull(sp)){
- printf("STACK OVERFLOW\n");
- }
- else{
- sp->top++;
- sp->arr[sp->top] = val;
- }
- }
- void pop(struct stack* sp)
- {
- if(isEmpty(sp)){
- printf("stack Underflow\n");
- //return -1;
- }
- else{
- char val = sp->arr[sp->top];
- sp->top--;
- }
- }
- int ParenthesisCheck(char* exp)
- {
- struct stack* sp = (struct stack*)malloc(sizeof(struct stack));
- sp->size = 100;
- sp->top = -1;
- char* arr = (char*)malloc(sp->size*sizeof(char));
- for(int i=0;exp[i]!='\0';i++){
- if(exp[i]=='{' || exp[i]=='[' || exp[i]=='('){
- if(isFull(sp)){
- return 0;
- }
- else{
- push(sp,exp[i]);
- }
- }
- else if(exp[i]=='}' || exp[i]==']' || exp[i]==')'){
- if(isEmpty(sp)){
- return 0;
- }
- else{
- if(exp[i] == (sp->arr[sp->top])){
- pop(sp);
- }
- else{
- return 0;
- }
- }
- }
- }
- if(isEmpty(sp)){
- return 1;
- }
- else{
- return 0;
- }
- }
- int main()
- {
- char* exp = "{1+(1*5)}";
- if(ParenthesisCheck(exp)){
- printf("Parenthesis Are Matching.\n");
- }
- else{
- printf("Parenthesis are not Matching\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement