Data structure---C language implementation bracket matching (stack implementation)

Source: Internet
Author: User
#include <stdio.h> #include <stdlib.h> #define stackincreament #define STACK_INIT_SIZE #define Overflo
W-2 #define OK 1 #define ERROR 0 typedef int status;
typedef char Selemtype;
	typedef struct {Selemtype *base;
	Selemtype *top;
Status StackSize;

}sqstack;
	Initialize stack status Init (Sqstack *s) {s->base = (Selemtype *) malloc (stack_init_size * sizeof (Selemtype));
	if (!s->base) exit (OVERFLOW);
	S->top = s->base;
	S->stacksize = stack_init_size;
return OK;
	//Get stack top element status Gettop (Sqstack *s, Selemtype e) {if (s->top = s->base) return ERROR;
	E = * (s->top-1);
return OK; }//Press stack status push (Sqstack *s, Selemtype e) {if (s->top-s->base >= s->stacksize) {s->base = (SE
	  Lemtype *) realloc (s->base, (s->stacksize + stackincreament) * sizeof (Selemtype));
      if (!s->base) exit (OVERFLOW);
	  S->top = S->base + s->stacksize;
	S->stacksize + = stackincreament;
	} *s->top++ = e; return OK;
}//Out stack status POPs (Sqstack *s, Selemtype *e) {if (s->top = s->base) return ERROR;
		*e=*--s->top;
return OK;
		}//Determine if empty stack status Stackempty (Sqstack *s) {if (s->top==s->base) return OK;
return ERROR;
		}//Empty stack status Clearstack (Sqstack *s) {if (s->top = = s->base) return ERROR;
		S->top = s->base;
return OK;
		}//Bracket matching algorithm status Parenthesis_match (Sqstack *s,char *str) {int i=0, flag=0;
		Selemtype e;
						while (Str[i]!= ' i ') {switch (str[i)) {case ' (': Push (S,str[i]);
				Break
						Case ' [': Push (S,str[i]);
				Break
						  Case ') ': {pop (s,&e);
						if (e!= ' (') flag=1;
				} break;
						  Case '] ': {pop (s,&e);
						if (e!= ' [') flag=1;
				} break;
			Default:break;
			} if (flag) break;
	i++;
   } if (!flag && stackempty (s)) printf ("Bracket matching succeeded!\n");
   else printf ("Bracket match failed!\n");
return OK; } intMain () {char str[100], enter;
	Sqstack s;
	Init (&s);
	printf ("Please enter a string: \ n");
	scanf ("%s", str);
	scanf ("%c", &enter);
    Parenthesis_match (&AMP;S,STR);
return 0;

 }





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.