括号配对是最基本的栈的问题,它是栈入门的经典题目,思路是,如果是左括号直接进栈,如果是右括号,这时就要比较栈顶的元素与他是否匹配,如果匹配则出栈,否则进栈,下面是代码的实现:
1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef struct stack{//定义栈来存储括号 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();//初始化top指针 19 char ch[10001]; 20 scanf("%s", ch); int i = 0; 21 while(ch[i] != '