MyStack的2个应用

复习栈的两个应用:

单词逆序:

package com.test.data_struct;

import java.io.*;
import java.util.*;
public class MyReverserTest {

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

		
			
		InputStreamReader isr=new InputStreamReader(System.in);
		BufferedReader br=new BufferedReader(isr);
		String input=null;
		String output=null;
		
		
		
		while(true){
			try {
				System.out.println("请输入单词,回车结束。若无输入直接回车,则程序终止退出。");
				input=br.readLine();
			
				if(input.equals("")){
					System.exit(0);
				}
				MyReverser mr=new MyReverser();
				output=mr.doReverse(input);
				System.out.println("倒置后的单词是:"+output);
				
			
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

}

class MyReverser{
	private String output="";
	private Stack s=new Stack();
	
	public String doReverse(String input){
		for(int i=0;i<input.length();i++)
			s.push(input.charAt(i));
		
		while(!s.isEmpty()){
			
		output=output+s.pop();
		}
		
		return output;
		
	}
	
}

 匹配分隔符:

package com.test.data_struct;

import java.io.*;
import java.util.*;

public class MyCheckerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		while (true) {
			System.out.println("please input a String,no input means exit:");
			String input = getString();
			if (input.equals("")){
				System.out.println("no input ,exit.");
				break;
			}
				
			MyChecker mc = new MyChecker();
			mc.checkSymbol(input);
		}

	}

	public static String getString() {
		String input = "";
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		try {
			input = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return input;
	}

}

class MyChecker {
	private Stack stk;

	public MyChecker() {
		stk = new Stack();
	}

	public void checkSymbol(String input) {
		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			switch (c) {
			case '(':
			case '[':
			case '{':
				stk.push(c);
				break;
			case ')':
			case ']':
			case '}':
				if (!stk.isEmpty()) {
					char cx = stk.pop().toString().charAt(0);
					System.out.println(cx);
					if ((c == ')' && cx != '(') || (c == ']' && cx != '[')
							|| (c == '}' && cx != '{')) {
						System.out.println("not match at no." + (i + 1) + " \""
								+ c + "\".");
					}
				} else {
					System.out.println("error at no." + (i + 1) + " \"" + c
							+ "\".");
				}
				break;
			default:
				break;
			}

		}
		if (!stk.isEmpty()) {
			System.out.println("missing symbol.");
		}else{
			System.out.println("good matched.");
		}

	}

}

 通过这2个小应用,对栈的使用有了点小感觉了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值