• 栈结构的java实现&括号匹配问题


    栈的结构,构造器方法定义:
        //记录首结点
        private Node head;
        //栈中元素的个数
        private int N;
    
        private class Node{
            public T item;//保存结点信息
            public Node next;//保存下一个结点
    
            public Node(T item, Node next) {
                this.item = item;
                this.next = next;
            }
        }
        public Stack() {
            this.head = new Node(null,null);
            this.N=0;
        }
    
    入栈与出栈:
        //把t元素压入栈
        public void push(T t){
            //找到首结点指向的第一个结点
            Node oldFirst = head.next;
            //创建新结点
            Node newNode = new Node(t, null);
            //让首结点指向新结点
            head.next = newNode;
            //让新结点指向原来的第一个结点
            newNode.next=oldFirst;
            //元素个数+1;
            N++;
        }
    
        //弹出栈顶元素
        public T pop(){
            //找到首结点指向的第一个结点
            Node oldFirst = head.next;
            if (oldFirst==null){
                return null;
            }
            //让首结点指向原来第一个结点的下一个结点
            head.next=oldFirst.next;
            //元素个数-1;
            N--;
            return oldFirst.item;
        }
    
    括号匹配问题

    问题描述****:

    给定一个字符串,里边可能包含"()"小括号和其他字符,请编写程序检查该字符串的中的小括号是否成对出现。

    例如:

    "(上海)(长安)":正确匹配

    "上海((长安))":正确匹配

    "上海(长安(北京)(深圳)南京)":正确匹配

    "上海(长安))":错误匹配

    "((上海)长安":错误匹配

    public class BracketsMatchTest {
        public static void main(String[] args) {
            String str = "上海(长安)())";
            boolean match = isMatch(str);
            System.out.println(str+"中的括号是否匹配:"+match);
        }
    
        /**
         * 判断str中的括号是否匹配
         * @param str 括号组成的字符串
         * @return 如果匹配,返回true,如果不匹配,返回false
         */
        public static boolean isMatch(String str){
            //1.创建栈对象,用来存储左括号
            Stack<String> chars = new Stack<>();
            //2.从左往右遍历字符串
            for (int i = 0; i < str.length(); i++) {
                String currChar = str.charAt(i)+ "";
    
                //3.判断当前字符是否为左括号,如果是,则把字符放入到栈中
                if (currChar.equals("(")){
                    chars.push(currChar);
                }else if(currChar.equals(")")){
                    //4.继续判断当前字符是否是有括号,如果是,则从栈中弹出一个左括号,并判断弹出的结果是否为null,如果为null证明没有匹配的左括号,如果不为null,则证明有匹配的左括号
                    String pop = chars.pop();
                    if (pop==null){
                        return false;
                    }
                }
    
            }
            //5.判断栈中还有没有剩余的左括号,如果有,则证明括号不匹配
            if (chars.size()==0){
                return true;
            }else{
                return false;
            }
    
        }
    }
    
    
    温故而知新
  • 相关阅读:
    关于 CS1595 MS的知识库还是不全面,反正它给我的解决方法不能用,偶只有这样做了....
    被一贴,一个以前写的邮件发送的小类库。记住了,不是内裤
    [下载]活学活用DataGrid控件与ADO.NET
    不同浏览器透明度的写法
    通过文件读取oep值
    菜单
    CWnd::OnContextMenu函数(右键单击弹出快捷菜单)
    poj 3349(hash)
    poj 3009(dfs+回溯 模拟)
    poj 3026 (bfs+prim)
  • 原文地址:https://www.cnblogs.com/littleTiger-bukeai/p/14144423.html
Copyright © 2020-2023  润新知