自定义堆栈,实现括号匹配

  算法分析:判断算术表达式中 小括号,中括号,大括号的顺序是否正确匹配。

  如:{ a + [ b +( c * a) / (d * e)] }  匹配正确           

         { a + [ b +( c * a) / (d * e) }  匹配错误

     使用栈结构来完成,如果遇见右括号则入栈(push),遇到左边括号则出栈(pop),进行从左到右的遍历,扫描完成后,如果栈为空则,匹配成功,如果不为空则匹配失败!

   代码实现如下

/**
 * 定义栈操作接口
 * Created by xinyang on 2016-12-13.
 */
public interface Stack {
    //入栈
    public void push(Object obj) throws Exception;
    //出栈
    public Object pop() throws Exception;
    //获得栈顶元素
    public Object getTop() throws Exception;
    //判断栈是否为空
    public boolean isEmpty();
}


/**
 * StackImpl
 * 栈操作实现
 * @author xinyang
 * @date 2016-12-13
 */
public class StackImpl implements Stack {
   Object[] stack;//对象数
    final int deafaultSize = 10;//默认长度
    int top;//栈顶位置
    int maxSize;//最大长度
    public StackImpl(){
        //默认方法初始化
        init(deafaultSize);
    }
    private void init(int size){
        this.maxSize = size;
        top = 0 ;
        stack = new Object[size];
    }

    /**
     * 入栈
     * @param obj
     * @throws Exception
     */
    public void push(Object obj) throws Exception {
        //判断栈是否已满
        if(top == maxSize){
            throw new  Exception("堆栈已满");
        }
        //入栈
        stack[top] = obj;
        top++;
    }
   //出栈
    public Object pop() throws Exception {
        //判断栈是否为空
        if(isEmpty()){
            throw new Exception("堆栈为空!");
        }
        //因为在入栈之后默认将top值进行了++ 所以导致不提示当前位置
        top--;
        return stack[top];
    }
    //获取栈顶元素
    public Object getTop() throws Exception {
        if(isEmpty()){
            throw new Exception("堆栈为空");
        }
        //单纯获取栈顶元素

        return stack[top - 1];
    }

    public boolean isEmpty() {
      return top == 0;
    }
}

/**
 * MainTest
 * 测试类
 * @author xinyang
 * @date 2016-12-13
 */
public class MainTest {
    public static void main(String[] args)throws Exception{
        String str = "{ a + [ b +( c * a) / (d * e)] }";
        String str2 = "{ a + ( a * B)+ [a -1]+ }";
        signCheck(str);
    }
    public static void signCheck(String str) throws Exception{
        StackImpl stack = new StackImpl() ;
        String[] arr = expToStringArray(str);
        for(int i = 0 ; i < arr.length; i++){
            //如果数组中有这三种左括号元素那么直接进行入栈操作;
            if(arr[i].equals("(") || arr[i].equals("[") || arr[i].equals("{") ){
                stack.push(arr[i]);
            }else if(arr[i].equals(")") && !stack.isEmpty() && stack.getTop().equals("(")){
               //当我们遇到右括号时,发现当前位置栈顶的是左括号,那么此时可以出栈了
                stack.pop();
            }else if(arr[i].equals(")") && !stack.isEmpty() && !stack.getTop().equals("(")){
                 System.out.println("左右括号匹配次序不成功!");
                return;
            }
            //遇到中口号时
            else if(arr[i].equals("]") && !stack.isEmpty() && stack.getTop().equals("[")){
                //当我们遇到右括号时,发现当前位于栈顶的是左括号则可以出栈了
                stack.pop();
            }else if(arr[i].equals("]") && !stack.isEmpty() && !stack.getTop().equals("[")){
                System.out.println("左右口号匹配次序不成功!");
                return;
            }
            //大括号匹配
            else if(arr[i].equals("}") && !stack.isEmpty() && stack.getTop().equals("{")){
                stack.pop();
            }else if (arr[i].equals("}") && !stack.isEmpty() && !stack.getTop().equals("{")){
                System.out.println("左右括号匹配次序不成功!");
                return;
            }
            //右边括号多于左括号
            else if(arr[i].equals(")")|| arr[i].equals("]") || arr[i].equals("}") && stack.isEmpty()){
              System.out.println("右括号多于左边括号");
                return;
            }
        }
        //经历完一趟循环后,如果堆栈不为空,那么左括号就多了
        if(!stack.isEmpty()){
            System.out.println("左括号多于右边括号");
        }else {
            System.out.println("匹配正确");
        }

    }
    //将字符串转化为字符串数组
    public static String[] expToStringArray(String exp){
        //字符串数组长度
        int n = exp.length();
        String[] arr = new String[n];
        for(int i = 0; i < n ; i++){
            arr[i] = exp.substring(i,i + 1);
        }
        return arr;
    }
}



      平时对算法的积累很少,觉得一听算法,小编脑子就开始转蒙圈了,但是静下心来实际的写一遍,其实这里的逻辑很简单。遇到问题不要复杂化,而是一步步的拆分成多个小问题,小到你能理解,并可以实现。

  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值