算法-栈的实现

本文介绍了使用Java实现的栈数据结构,包括push、pop等基本操作,并通过两个实际案例展示了栈的应用:括号匹配问题和逆波兰表达式求值。在括号匹配中,栈用于检查括号的正确配对;在逆波兰表达式计算中,栈用于处理运算符和操作数,实现计算过程。
摘要由CSDN通过智能技术生成

 

package test.stack;

import org.apache.poi.ss.formula.functions.T;
import test.linear.LinkList;

import java.util.Iterator;


public class Stack<T> implements Iterable<T> {

    private Node head;

    private int N;



    private class  Node {
        T item;
        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;
    }


    public Boolean isEmpty(){
        return N==0;
    }

    public int size(){
        return N;
    }

    public void push(T t){

        Node oldfirst = head.next;
        head.next = new Node(t,oldfirst);

        N++;
    }

    public T pop(){
        if(head.next==null){
            return null;
        }
        Node n = head.next;
        head.next =  head.next.next;
        N--;
        return n.item;
    }


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

    private class Literator implements Iterator {
        private Node n;
        public Literator(){
            this.n = head ;
        }
        @Override
        public boolean hasNext() {
            return n.next!=null;
        }

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

        @Override
        public void remove() {

        }




    }

}

测试代码

package test.stack;

public class StackTest {
    public static void main(String[] args) {
        Stack<String> stack = new Stack();
        stack.push("aa");
        stack.push("bb");
        stack.push("cc");
        for (String s : stack) {
            System.out.println(s);
        }
        System.out.println("----------------------");
        String result = stack.pop();
        System.out.println("弹出的元素是:"+result);
        System.out.println("剩下的个数的:"+stack.size());
       result = stack.pop();
        System.out.println("弹出的元素是:"+result);
        System.out.println("剩下的个数的:"+stack.size());
         result = stack.pop();
        System.out.println("弹出的元素是:"+result);
        System.out.println("剩下的个数的:"+stack.size());
         result = stack.pop();
        System.out.println("弹出的元素是:"+result);
        System.out.println("剩下的个数的:"+stack.size());
    }
}
cc
bb
aa
----------------------
弹出的元素是:cc
剩下的个数的:2
弹出的元素是:bb
剩下的个数的:1
弹出的元素是:aa
剩下的个数的:0
弹出的元素是:null
剩下的个数的:0

案例:

括号匹配问题

解决思路

代码实现

package test.stack;

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

    public static boolean isMatch(String str){
        Stack<String> chars = new Stack<>();
        for (int i = 0; i <str.length() ; i++) {
           String temp = String.valueOf( str.charAt(i));
           //判断字符是不是左括号
            if("(".equals(temp)){
                //那么就压入栈中
                chars.push(temp);
            }else if(")".equals(temp)){
                String item = chars.pop();
                if(item==null){
                    return false;
                }
            }
        }
        if(chars.size()==0){
            return true;
        }
        return false;
    }


}

 

遍历字符,如果当前字符不是操作符就压入栈中,如果是就从栈中取出俩个进行计算,然后压入栈中

package test.stack;

public class ReversePolishNotationTest {
    public static void main(String[] args) {
        String [] notation = {"3","17","15","-","*","18","6","/","+"};
        int result = caculate(notation);
        System.out.println("逆波兰表达式的结果为:"+result);

    }

    public static int caculate(String [] notation){
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i <notation.length ; i++) {
            String item = notation[i];
            Integer o1 ;
            Integer o2 ;
            Integer result ;
            switch (item){
                case "+":
                     o1 =  stack.pop();
                     o2 =   stack.pop();
                     result =  o2 + o1;
                    stack.push(result);
                    break;
                case "-":
                     o1 =  stack.pop();
                     o2 =   stack.pop();
                     result =  o2 - o1;
                    stack.push(result);
                    break;
                case  "*":
                     o1 =  stack.pop();
                     o2 =   stack.pop();
                     result =  o2 * o1;
                    stack.push(result);
                    break;
                case "/":
                     o1 =  stack.pop();
                     o2 =   stack.pop();
                     result =  o2 / o1;
                    stack.push(result);
                    break;
                default:
                    //把操作数压入栈中
                    stack.push(Integer.valueOf(item));


            }


        }
        return stack.pop();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值