[Data structure and algorithm] 3. Brackets Matching Test

Source: Internet
Author: User
I. Question

◆ 3.19 ④ assume that an arithmetic expression can contain three parentheses: parentheses ("and
")", Square brackets "[" and "]" And curly brackets "{" and "}", and the brackets can be any
Use of order nesting (for example :... [... {...}... […]… […]… (...)...). Compile a discriminant expression
Matching Algorithm (known expressions have been stored in data elements)
Is a sequential table of characters ).

Implement the following functions:
Status MatchCheck (SqList exp );
/* Exp indicates an expression in the sequence table ;*/
/* If the parentheses in exp are paired, TRUE is returned; otherwise, FALSE is returned */

Sequence Table types are defined as follows:
Typedef struct {
ElemType * elem;
Int length;
Int listsize;
} SqList; // sequence table

Stack is an implemented Stack.
Available types and functions:
Typedef char SElemType; // element type of Stack
Status InitStack (Stack & s );
Status Push (Stack & s, SElemType e );
Status Pop (Stack & s, SElemType & e );
Status StackEmpty (Stack s );
Status GetTop (Stack s, SElemType & e );

Bytes -------------------------------------------------------------------------------------------------

Ii. Ideas

You can use the"Expected urgency.

For example, consider the following sequence of parentheses:

[([] [])]

1 2 3 4 5 6 7 8

When the computer accepts the first bracket, it looks forward to the appearance of the eighth matching bracket. However, it waits for the second bracket. At this time, the first bracket "[" can only be used to draw a side temporarily, and the emergence of the ")" matching the second parenthesis is urgently waiting. Similarly, the third parenthesis "[", the degree of expectation matching is more urgent than that of the second bracket. Then, the second bracket can only be positioned by the side to the third bracket. Obviously, the expectation of the second bracket is more urgent than that of the first bracket; after accepting the fourth bracket, the expectation of the third bracket is satisfied. After elimination, the expectation matching of the second bracket becomes the most urgent task at present ,...... And so on.

Obviously, such a processing process is very consistent with the features of the stack. Therefore, this problem can be solved using the stack.

  Solution:

1. Set a stack in the algorithm and read a bracket each time;

2. if it is a right brace, or the most urgent expectation placed on the top of the stack can be eliminated, the left brace at the top of the stack will pop up; or it is invalid, right brace is pushed;

3. if it is left parentheses, it will be pushed into the stack as a new and more urgent expectation, which naturally lowers the urgency of all the undispelled expectations in the stack;

4. The stack should be empty at the beginning and end of the algorithm.

Bytes -------------------------------------------------------------------------------------------------

Iii. Code (C/C ++) View Code

1 Status MatchCheck (SqList exp) 2/* sequence table exp represents the expression; */3/* returns TRUE if the parentheses in exp are paired, otherwise, FALSE */4 {5 Stack s; 6 char e; 7 8 // read the brackets 9 for (int I = 0; I <exp. length; I ++) 10 {11 if (exp. elem [I] = '(' | exp. elem [I] = '{' | exp. elem [I] = '[') // In case of left brackets, the stack is directly pushed (s, exp. elem [I]); 14} 15 else if (exp. elem [I] = ') // In case of right parentheses, try matching the top parentheses of the stack 16 {17 if (GetTop (s, e )) 18 {19 if (e = '(') // The matching is successful, and the left parentheses output the stack 20 {21 Pop (s, e ); 22} 23 else // The matching fails. right parentheses are inserted into the stack 24 {25 Push (s, exp. elem [I]); 26} 27} 28 else // If the stack is empty, Push (s, exp. elem [I]); 31} 32} 33 else if (exp. elem [I] = '}') // In case of right curly braces, try matching the Top Bracket 34 {35 if (GetTop (s, e )) 36 {37 if (e = '{') // The matching is successful. The left curly braces output the stack 38 {39 Pop (s, e ); 40} 41 else // The matching fails. Right curly braces are inserted into the stack 42 {43 Push (s, exp. elem [I]); 44} 45} 46 else47 {48 Push (s, exp. elem [I]); 49} 50} 51 else if (exp. elem [I] = ']') // if there is a right brace, try matching the top brace 52 {53 if (GetTop (s, e )) 54 {55 if (e = '[') // The matching is successful. The left square brackets show the stack 56 {57 Pop (s, e ); 58} 59 else // The matching fails. the right side of the bracket is pushed to the stack 60 {61 (s, exp. elem [I]); 62} 63} 64 else65 {66 Push (s, exp. elem [I]); 67} 68} 69} 70 if (StackEmpty (s) // when all parentheses match successfully, the stack should be empty 71 {72 return TRUE; 73} 74 else75 {76 return FALSE; 77} 78}

Bytes -------------------------------------------------------------------------------------------------

Iv. Summary

This algorithm is seen in Professor Wu Weimin's "Data Structure". The book only provides ideas. The specific code is written by myself. I feel that this question is necessary to mark it, so I wrote this blog post.

This algorithm is not very difficult. It will be less simple to use the stack to implement non-recursive traversal of Binary trees in the future.

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.