java学习第15天,栈的应用

栈的应用中情况非常多,这段代码很长,在手输入的时候,看着源代码然后再敲上去,而不是复制粘贴。这点非常重要,在手输入的时候能够体会到代码的思想理解其中意思。

package java11to20;

public class D15_applicationStack {
	public static final int Max = 25;
	int depth = 0;
	char[] data;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		D15_applicationStack tempStack = new D15_applicationStack();

		for (char ch = 'a'; ch <= 'z'; ch++) {
			tempStack.push(ch);
			System.out.println(String.format("栈中现有值:%s", tempStack));
		}
		char tempChar;
		for (int i = 0; i < 13; i++) {
			tempChar = tempStack.pop();
			System.out.println("出栈: " + tempChar);
			System.out.println("栈中现有值: " + tempStack);
		}
		boolean match;
		String tempExpression = "[2 + (1 - 3)] * 5";
		match = bracketMatching(tempExpression);
		System.out.println("是表达式 " + tempExpression + " 括号是否匹配? " + match);

		tempExpression = "( )  )(";
		match = bracketMatching(tempExpression);
		System.out.println("是表达式 " + tempExpression + " 括号是否匹配? " + match);

		tempExpression = "(()()())";
		match = bracketMatching(tempExpression);
		System.out.println("是表达式 " + tempExpression + " 括号是否匹配? " + match);

		tempExpression = "({}[])";
		match = bracketMatching(tempExpression);
		System.out.println("是表达式 " + tempExpression + " 括号是否匹配? " + match);

		tempExpression = ")(";
		match = bracketMatching(tempExpression);
		System.out.println("是表达式 " + tempExpression + " 括号是否匹配? " + match);

	}

	public static boolean bracketMatching(String paraString) {
		D15_applicationStack Stack = new D15_applicationStack();
		Stack.push('#');
		char tempChar, tempPopedChar;

		for (int i = 0; i < paraString.length(); i++) {
			tempChar = paraString.charAt(i);

			switch (tempChar) {
			case '(':
			case '[':
			case '{':
				Stack.push(tempChar);
				break;
			case ')':
				tempPopedChar = Stack.pop();
				if (tempPopedChar != '(') {
					return false;
				}
				break;
			case ']':
				tempPopedChar = Stack.pop();
				if (tempPopedChar != '[') {
					return false;
				}
				break;
			case '}':
				tempPopedChar = Stack.pop();
				if (tempPopedChar != '{') {
					return false;
				}
				break;
			default:
			}
		}
		tempPopedChar = Stack.pop();
		if (tempPopedChar != '#') {
			return false;
		}
		return true;
	}

	public D15_applicationStack() {
		depth = 0;
		data = new char[Max];
	}

	public String toString() {
		String result = "";
		for (int i = 0; i < depth; i++) {
			result += data[i];
		}
		return result;
	}

	public boolean push(char paraChar) {
		if (depth == Max) {
			System.out.println("栈满\n");
			return false;
		}
		data[depth] = paraChar;
		depth++;

		return true;
	}

	public char pop() {
		if (depth == 0) {
			System.out.println("栈空");
			return '\0';
		}
		char result = data[depth - 1];
		depth--;

		return result;
	}
}

输出结果:

栈中现有值:a
栈中现有值:ab
栈中现有值:abc
栈中现有值:abcd
栈中现有值:abcde
栈中现有值:abcdef
栈中现有值:abcdefg
栈中现有值:abcdefgh
栈中现有值:abcdefghi
栈中现有值:abcdefghij
栈中现有值:abcdefghijk
栈中现有值:abcdefghijkl
栈中现有值:abcdefghijklm
栈中现有值:abcdefghijklmn
栈中现有值:abcdefghijklmno
栈中现有值:abcdefghijklmnop
栈中现有值:abcdefghijklmnopq
栈中现有值:abcdefghijklmnopqr
栈中现有值:abcdefghijklmnopqrs
栈中现有值:abcdefghijklmnopqrst
栈中现有值:abcdefghijklmnopqrstu
栈中现有值:abcdefghijklmnopqrstuv
栈中现有值:abcdefghijklmnopqrstuvw
栈中现有值:abcdefghijklmnopqrstuvwx
栈中现有值:abcdefghijklmnopqrstuvwxy
栈满

栈中现有值:abcdefghijklmnopqrstuvwxy
出栈: y
栈中现有值: abcdefghijklmnopqrstuvwx
出栈: x
栈中现有值: abcdefghijklmnopqrstuvw
出栈: w
栈中现有值: abcdefghijklmnopqrstuv
出栈: v
栈中现有值: abcdefghijklmnopqrstu
出栈: u
栈中现有值: abcdefghijklmnopqrst
出栈: t
栈中现有值: abcdefghijklmnopqrs
出栈: s
栈中现有值: abcdefghijklmnopqr
出栈: r
栈中现有值: abcdefghijklmnopq
出栈: q
栈中现有值: abcdefghijklmnop
出栈: p
栈中现有值: abcdefghijklmno
出栈: o
栈中现有值: abcdefghijklmn
出栈: n
栈中现有值: abcdefghijklm
出栈: m
栈中现有值: abcdefghijkl
是表达式 [2 + (1 - 3)] * 5 括号是否匹配? true
是表达式 ( )  )( 括号是否匹配? false
是表达式 (()()()) 括号是否匹配? true
是表达式 ({}[]) 括号是否匹配? true
是表达式 )( 括号是否匹配? false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值