Data structure review-chain stack, sequence stack, and matching algorithm of parentheses

Source: Internet
Author: User
Sequential Stack

# Include <stdio. h> // initialize the stack [sequential Stack] void initstack (seqstack * s) {S-> Top =-1 ;}// stack-in-stack int push (seqstack * s, stackelementtype X) {If (S-> Top = Stack_size-1) {return false;} s-> top +; s-> ELEM [S-> top] = X; return true;} // output stack int POP (seqstack * s, stackelementtype * X) {If (S-> Top =-1) {return false ;} else {* x = s-> ELEM [S-> top]; S-> top --; return true ;}// get the top element of the stack int gettop (seqstack * s, stackelementtype * X) {If (S-> Top =-1) {return false;} else {* x = s-> ELEM [S-> top]; return true ;}} int main (void) {return 0 ;}

Chain Stack

# Include <stdio. h> typedef struct node {stackelementtype data; struct node * Next;} linkstacknode; typedef linkstacknode * linkstack; // stack-in-stack [Chain Stack] int push (seqstack top, stackelementtype X) {linkstacknode * temp; temp = (linkstacknode) malloc (sizeof (linkstacknode); If (temp = NULL) // return false if the application fails; temp-> DATA = X; temp-> next = Top-> next; top-> next = temp; return true;} // output stack [Chain Stack] int POP (seqstack top, stackelementtype * X) {linkstacknode * temp; temp = Top-> next; If (temp = NULL) // the stack is empty, return false; top-> next = temp-> next; * x = temp-> data; free (temp); Return true;} int main (void) {return 0 ;}

Bracket Matching Algorithm

# Include <stdio. h> // bracket matching algorithm void bracketmatch (char * Str) // The input string {stack S, int I, char ch; initstack (& S ); for (I = 0; STR [I]! = '\ 0'; I ++) {Switch (STR [I]) {Case' (': Case' [': Case' {': Push (& S, STR [I]); break; Case ')': Case ']': Case '}': If (isempty (& S )) {printf ("unnecessary parentheses"); return;} else {gettop (& S, & Ch); If (MATCH (CH, STR [I]) pop (& S, & Ch); elseprintf ("corresponding to different brackets") ;}}if (isempty (& S) printf ("matching brackets "); elseprintf ("Parentheses do not match");} int main (void) {return 0 ;}

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: 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.