Data Structure-Parentheses matching in C Language (stack implementation)

Source: Internet
Author: User

Data Structure-Parentheses matching in C Language (stack implementation)

# Include
 
  
# Include
  
   
# Define STACKINCREAMENT 10 # define STACK_INIT_SIZE 100 # define OVERFLOW-2 # define OK 1 # define ERROR 0 typedef int status; typedef char SElemtype; typedef struct {SElemtype * base; SElemtype * top; status stacksize;} sqstack; // initialize the 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 ;} // obtain the top stack element status Gettop (sqstack * s, SElemtype e) {if (s-> top = s-> base) return ERROR; e = * (s-> top-1); return OK;} // press the stack status push (sqstack * s, SElemtype e) {if (s-> top-s-> base> = s-> stacksize) {s-> base = (SElemtype *) 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;} // output stack status pop (sqstack * s, SElemtype * e) {if (s-> top = s-> base) return ERROR; * e = * -- s-> top; return OK ;} // determine whether the stack status is empty stackempty (sqstack * s) {if (s-> top = s-> base) return OK; return ERROR ;} // clear the stack status clearstack (sqstack * s) {if (s-> top = s-> base) return ERROR; s-> top = s-> base; return OK;} // brackets Matching Algorithm status Parenthesis_match (sqstack * s, char * str) {int I = 0, flag = 0; SElemtype e; while (str [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 (matching brackets successful !); Else printf (matching parentheses failed !); Return OK;} int main () {char str [100], enter; sqstack s; Init (& s); printf (enter the string :); scanf (% s, str); scanf (% c, & enter); Parenthesis_match (& 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.