算法分析-栈

原理:栈是一种先进后出的数据结构,底层可以用链表和数组实现。

代码:

package com.chenqing.test.linearList.stack;

import java.util.Iterator;

public class Realization <T> implements Iterable<T>{
    private Node head;
    private int n;

    public Realization() {
        this.head = new Node(null, null);
        this.n = 0;
    }

    public boolean isEmpty(){
        return this.n == 0;
    }

    public int size(){
        return this.n;
    }

    public void push(T t){
        Node oldFirst = this.head.next;
        Node newNode = new Node(t, oldFirst);
        this.head.next = newNode;
        this.n++;
    }

    public T pop(){
        Node oldFirst = this.head.next;
        if(oldFirst==null){
            return null;
        }
        this.head.next = oldFirst.next;
        this.n--;
        return oldFirst.data;
    }

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

    private class MyselfIterator implements Iterator<T>{
        private Node n;

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


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

        @Override
        public T next() {
            n=n.next;
            return n.data;
        }
    }

    private class Node{
         T data;
         Node next;

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}

示例:

1、括号匹配问题:

public void testMatch() {
        String str = "中正因为(中中)(里))";
        boolean match = isMatch(str);
        System.out.println(match);
    }

    private boolean isMatch(String str) {
        Realization<String> strings = new Realization<>();
        for (int i = 0; i < str.length(); i++) {
            String curStr = str.charAt(i) + "";
            if (curStr.equals("(")) {
                strings.push(str.charAt(i) + "");
            } else if (curStr.equals(")")) {
                String pop = strings.pop();
                if (pop == null) {
                    return false;
                }
            }

        }
        if (strings.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

2、逆波兰表达式计算:

代码:

public void postfix() {
        String expression = "34*23+4/2-22+(45+33)";
        String[] expressionArr = {"34", "23", "*", "4", "2", "/", "+", "22", "-", "45", "33", "+", "+"};
        Realization<Integer> integers = new Realization<>();
        for (int i = 0; i < expressionArr.length; i++) {
            String curr = expressionArr[i];
            Integer pop1=0;
            Integer pop2 = 0;
            switch (curr){
                case "+":
                     pop1 = integers.pop();
                     pop2 = integers.pop();
                    int addVal = pop2 + pop1;
                    integers.push(addVal);
                    break;
                case "-":
                     pop1 = integers.pop();
                     pop2 = integers.pop();
                    int eVal = pop2 - pop1;
                    integers.push(eVal);
                    break;
                case "*":
                    pop1 = integers.pop();
                    pop2 = integers.pop();
                    int xVal = pop2 * pop1;
                    integers.push(xVal);
                    break;
                case "/":
                    pop1 = integers.pop();
                    pop2 = integers.pop();
                    int cVal = pop2 / pop1;
                    integers.push(cVal);
                    break;
                default:
                    integers.push(Integer.parseInt(curr));
            }
        }
        Integer result = integers.pop();
        System.out.println(result);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值