首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

用自定义栈往实现字符串反转

2012-11-06 
用自定义栈去实现字符串反转package date0610import java.io.BufferedReaderimport java.io.IOException

用自定义栈去实现字符串反转

package date0610;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** *@author TonyJ *@time 2011-6-10 下午03:46:08 */public class Test02 {private char arr[];private int maxSize;private int top;public Test02(int s) {arr = new char[s];top = -1;maxSize = s;}public void push(char c) {//进栈arr[++top] = c;}public char pull() {//出栈return arr[top--];}public char peek() {//取得栈头数据return arr[top];}public boolean isEmpty() {//判断是否空return top == -1;}public boolean isFull() {//判断栈是否已满return top == maxSize - 1;}public static String getString() {//获得输入的字符串String str = null;BufferedReader br = null;br = new BufferedReader(new InputStreamReader(System.in));try {System.out.println("请输入字符串:");str = br.readLine();} catch (IOException e) {e.printStackTrace();}return str;}public static String reverse() {String input = getString();String output="";Test02 t = new Test02(input.length());for (int i = 0; i < input.length(); i++) {t.push(input.charAt(i));}while (!t.isEmpty()) {output =output+ t.pull();}return output;}public static void main(String[] args) {System.out.println(reverse());}}

热点排行