Just started to learn the data structure, with buyers wrote a classic application, bracket matching.
Algorithm ideas:
Input string, will ' (', ' [' press into buyers, encounter ') '] ', and then stack the top out buyers, brace matching, if the successful match, then continue, otherwise, the program ends, enter the mismatch information,
If ') ' is matched, then see if the buyers is empty, if it is empty, the input matches, otherwise, the output symbol number does not match.
#include <iostream>using namespace std; #define Max Stack{int top;int Maxsize;char *element;}; typedef struct STACK stack;void Init (stack *s,int N) {if (n>0) {s->top=-1; s->maxsize=n; S->element=new Char [s->maxsize];}} BOOL Isfull (Stack *s) {return (s->top==s->maxsize-1);} BOOL IsEmpty (Stack *s) {return (s->top==-1);} void push (Stack *s,char x) {if (! Isfull (S)) s->element[++s->top]=x;else{cout<< "Buyers full" <<ENDL;} Char pop (Stack *s) {if (! IsEmpty (S)) return s->element[s->top--];else{cout<< "Buyers Empty" <<endl;return-1;} int main () {Stack *s=new stack;init (S,max); bool Flag=true;bool flag_1=true;bool flag_2=true;cout<< "Please enter string:" < <endl;char ch;while ((ch = getchar ()) = ' \ n ') {switch (ch) {case ' (': Push (S,CH); Case ' [': Push (S,CH); Case ') ': {if (pop (S)! = ' (' && flag_2) {cout<< "') '" << "does not match" <<endl; Flag_1=false; Flag=false; Break } else {continue; }} Case ': {if (pop (S)! = ' [' && flag_1) {cout<< "'] '" << "symbol does not match" <<endl; Flag_2=false; Flag=false; Break } else {continue; }}}}if (! IsEmpty (S) && flag) {cout<< "Quantity mismatch" <<ENDL;} else if (flag) {cout<< "Match" <<ENDL;} System ("pause"); return 0;}
Are simple to define, simple to function.