Nyoj2 bracket pairing

Source: Internet
Author: User

Bracket matching is the most basic stack issue. It is a classic topic for getting started with the stack. The idea is that if the left bracket is directly added to the stack, if it is the right brace, at this time, it is necessary to compare whether the element at the top of the stack matches with him. If it matches, it will go out of the stack; otherwise, it will go into the stack. The following is the implementation of the Code:

1 # include <stdio. h> 2 # include <stdlib. h> 3 typedef struct stack {// defines the stack to store brackets 4 char ch; 5 struct stack * Next; 6} link_stack; 7 link_stack * init_link_stack (); 8 link_stack * push_stack (link_stack * Top, char ch); 9 link_stack * pop_stack (link_stack * Top); 10 int main () 11 {12 int n; 13 scanf ("% d", & N); 14 getchar (); 15 while (n --) 16 {17 link_stack * Top; 18 Top = init_link_stack (); // initialize top pointer 19 char ch [100 01]; 20 scanf ("% s", CH); int I = 0; 21 While (CH [I]! = '\ 0') // Judge Read Terminator 22 {23 if (CH [I] ='] ') & (top-> CH = '[') | (CH [I] = ')') & (top-> CH = '(') // if the right bracket is to be added to the stack, determine whether the top element of the stack is left brace, if yes, 24 Top = pop_stack (top); 25 else26 Top = push_stack (top, CH [I]) will pop up; // otherwise, the stack will be pressed 27 I ++; 28} 29 If (top-> CH = '0') // judge whether the stack is empty 30 printf ("Yes \ n "); 31 else32 printf ("NO \ n"); 33} 34 return 0; 35} 36 37 link_stack * init_link_stack () // initialize the stack function 38 {39 link_stack * node; 40 node = (link_stack *) malloc (sizeof (link_stack); 41 node-> next = NULL; 42 node-> CH = '0'; 43 return node; 44} 45 link_stack * push_stack (link_stack * Top, char ch) // function 46 {47 link_stack * node; 48 node = init_link_stack (); 49 node-> CH = CH; 50 node-> next = top; 51 Top = node; 52 return top; 53} 54 link_stack * pop_stack (link_stack * top) // output stack function 55 {56 link_stack * node; 57 if (top-> next = NULL) 58 return top; 59 else60 {61 node = top; 62 Top = Top-> next; 63 free (node); 64 return top; 65 66} 67 68}

 

Nyoj2 bracket pairing

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.