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

Source: Internet
Author: User

Sequence stack [cpp] # 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 [cpp] # 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 )// Failed to apply for space return false; 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) // stack is empty return false; top-> next = temp-> next; * x = temp-> data; free (temp); return true;} int main (void) {return 0;} brackets matching algorithm [cpp] # 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); else printf ("corresponding to different bracket classes");} www.2cto.com} if (IsEmpty (& s )) printf ("Parentheses match"); else printf ("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: 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.