1. 栈的实现

/*
*路人假helloworld
*/
package com.cjj.sort;

import java.util.Iterator;

public class Stack<T> implements Iterable<T>{
    private Node head;
    private int N;

    //结点内部类
    public 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);
    }

    //判断当前栈中元素是否为0
    public boolean isEmpty(){
        return N == 0;
    }
    //获取栈中元素的个数
    public int size(){
        return N;
    }
    //把元素压入栈
    public void push(T t){
        Node newNode = new Node(t,null);
        Node n = head.next;
        if (n == null){
            head.next = newNode;
            return;
        }
        newNode.next = n;
        head.next = newNode;
        N++;
    }
    //弹出栈顶元素
    public T pop(){
        Node first = head.next;
        if (first == null){
            return null;
        }
        head.next = first.next;
        N--;
        return first.item;
    }

    @Override
    public Iterator<T> iterator() {
        return new SIterable();
    }

    private class SIterable implements Iterator{
        private Node n;

        public SIterable(){
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public Object next() {
            n = n.next;
            return n.item;
        }
    }
}

测试

package com.cjj.test;

import com.cjj.sort.Stack;

public class StackTest {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        //测试压栈
        stack.push("a");
        stack.push("b");
        stack.push("c");
        stack.push("d");
        for (String item : stack){
            System.out.println(item);
        }

        System.out.println("---------------------------------");
        //测试弹栈
        String result = stack.pop();
        System.out.println("弹出的元素是:" + result);
        System.out.println("栈中还剩" + stack.size() + "个元素");
    }
}

测试结果

2.括号匹配问题

package com.cjj.sort;

public class BracketsMatchTest {
    public static void main(String[] args) {
        String str = "(上海(长安)())";
        boolean match = isMatch(str);
        System.out.println(str+"中的括号是否匹配:"+match);

    }

    //判断字符串中的括号是否匹配,并返回结果
    public static boolean isMatch(String str){
        //创建存储左括号的栈
        Stack<String> chars = new Stack<>();
        //遍历字符串
        for (int i = 0; i < str.length(); i++){
            String c = str.charAt(i) + "";
            if (c.equals("(")){
                chars.push(c);
            }else if (c.equals(")")){
                String pop = chars.pop();
                if (pop == null){
                    return false;
                }
            }
        }
        //判断栈中是否还有左括号,如果有则返回false,否则返回true
        if (chars.size() == 0){
            return true;
        }else{
            return false;
        }
    }
}
posted @ 2022-03-19 16:33  路人假helloWorld  阅读(48)  评论(0)    收藏  举报