zoukankan      html  css  js  c++  java
  • 栈应用实例单词逆序

    package com.study.dataStructure.linearList;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    //栈类
    public class StackX {
        private int maxSize;
        private char[] stackArray;
        private int top;
        
        public StackX(int max){
            stackArray = new char[max];
            top = -1;//空栈
        }
        
        public void push(char j){
            stackArray[++top] = j;
        }
        
        public char pop(){
            return stackArray[top--];
        }
        
        public char peek(){
            return stackArray[top];
        }
        
        public boolean isEmpty(){
            return top == -1;
        }
        
        public boolean isFull(){
            return top == (maxSize -1);
        }
        
    }
    
    //倒转字符串类
    class Reverse{
        private String input;
        private String output;
        
        public Reverse(String in){
            input = in;
        }
        
        //倒转字符串
        public String doRev(){
            int stackSize = input.length();
            StackX theStack = new StackX(stackSize);
            
            for(int i=0; i< stackSize; i++){
                theStack.push(input.charAt(i));
            }
            
            output = "";
            while(!theStack.isEmpty()){
                output += theStack.pop();
            }
            return output;
        }
    }
    package com.study.dataStructure.linearList;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    //字符串反转应用
    class ReverseApp{
        public static void main(String[] args) throws IOException{
            String input,output;
            
            while(true){
                System.out.println("请输入字符串");
                input = getString();
                if(input.equals("\r")) break;
                
                Reverse theReverser = new Reverse(input);
                output = theReverser.doRev();
                System.out.println("Reversed:"+output);
            }
            
            
        }
        
        public static String getString() throws IOException{
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            String str = br.readLine();
            return str;
        }
    }
  • 相关阅读:
    POJ 1003 解题报告
    POJ 1004 解题报告
    POJ-1002 解题报告
    vi--文本编辑常用快捷键之光标移动
    常用图表工具
    September 05th 2017 Week 36th Tuesday
    September 04th 2017 Week 36th Monday
    September 03rd 2017 Week 36th Sunday
    September 02nd 2017 Week 35th Saturday
    September 01st 2017 Week 35th Friday
  • 原文地址:https://www.cnblogs.com/yony/p/2875062.html
Copyright © 2011-2022 走看看