栈的应用-判断括号匹配

    栈的一个典型应用就是可以用来协助分析表达式的括号是否匹配。括号可以延伸到任何成对出现的界定符,例如引号,书名号等。

    接下来给出程序实现:

    第一部分给出的是堆栈的插入,删除等操作对应的实现:

 1 public class StackChar
 2 {
 3   private int maxSize;//堆栈数组大小
 4   private char [] stackArray;
 5   private int top;//堆栈顶
 6  public StackChar(int maxSize)
 7    {
 8       this.maxSize=maxSize;//设置初始化数组大小
 9       stackArray=new char[maxSize];
10       top=-1;
11     }
12   public void push(char ch)
13   {
14     //入栈
15     if(isFull())
16     System.out.println("Cannot insert item "+ch +"! the stack is already full.");
17     else
18    {
19     top++;
20     stackArray[top]=ch;
21     }
22    }
23 public char pop(char ch)
24   {
25     //入栈
26     if(isEmpty())
27    {
28      System.out.println("Cannot delect item "+ch +"! the stack is empty.");
29      return 0;
30     }
31     else
32    {
33         char ch=stackArray[top];
34         stackArray[top]=null;
35         top--;
36         return  ch;//出栈并且返回值 
37     }
38   }
39    public int size()
40   {  
41    return top+1;
42    }
43    public int peek()
44   {  
45    return stackArray[top];
46    }
47    public char num(int n)
48   {
49     return stackArray[n];//返回index为n的数值
50    }
51    public boolean isEmpty()
52   {
53     return (-1==top);
54   }
55  public boolean isFull()
56   {
57     return (maxSize-1==top);
58   }
59   public void print ()
60   {
61   System.out.println(" Stack :"):
62   for (int i=0;i<maxSize;i++)
63    {
64      System.out.println(num(i));
65    }
66   }
67 }

第二部分是给出了相应的括号判断:

 1 public class BrackCheck{
 2   private String inString;
 3   public  BrackCheck(String in){
 4    inString=in;
 5   }
 6   public void check(){//检查括号匹配与否
 7   {
 8    int len=inString.length();
 9    StackChar chStack=new StackChar(len);
10    char ch;
11    char chPop;
12    boolean flag= false;
13    for( int index=0; index<len;index++){
14     ch=inString.charAt(index);
15     switch(ch){
16       case '{':
17       case '[':
18       case '(':
19        chStack.push(ch);//如果检测到是括号的前半部分,就执行入栈操作
20        break;
21        case '}':
22        case ']':
23        case ')':
24        if(!chStack.isEmpty()){//检测到反括号
25           chPop=chStack.peek();
26           if(('{'==chPop && '}'!=ch)//判断堆栈顶的括号与现在读入的这个反括号是不是匹配      
27              ||('['==chPop && ']'!=ch)
28              || ('('==chPop && ')'!=ch))
29             {//不匹配就报错
30                  System.out.println("Error : " + ch+" at" +index);
31                  flag=true;
32              }
33            else if(('{'==chPop && '}'==ch)
34              ||('['==chPop && ']'==ch)
35              || ('('==chPop && ')'==ch))
36               //匹配,出栈,进行下一个字符
37             chStack.pop();
38             }
39             else//遇到反括号,但是栈为空,则出错
40              {
41                  System.out.println("Error :"+ch+" at"+index);
42                  flag=true;
43               }
44            default: 
45                  break;
46            }
47     }
48  if(!flag)
49      System.out.println("The delimiters of the string is matching!");
50    }
51   }
原文地址:https://www.cnblogs.com/lujun1949/p/5571213.html