利用stack实现 字串倒序输入()

public class Demo
{
	public void vConvertMsg(String sInputStr, StringBuffer OutputStr)
	{
		if(null == OutputStr)return;
		//Code Here
		System.out.println("sInputStr:"+sInputStr);
		
		if(sInputStr.equals("")){
			OutputStr.append("");
			return;
		}
		
		char[] words = sInputStr.toCharArray();
		Stack stack = new Stack(words.length+1);
		stack.push('\0');
		for(Character word:words){
			if(Character.isLetter(word)|| word == '-'){
//				if(!Character.isWhitespace(word)&&!Character.isDigit(word)){
				if(!stack.isFull()){
					stack.push(word);
				}
			}else{
				if(stack.getElementCount()>0){
					char[] tmp =new char[stack.getElementCount()];
					for(int i=0;i<tmp.length;i++){
						if(!stack.isEmpty()){
							try {
								tmp[i] = stack.pop();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
					OutputStr.append(tmp);
					if(Character.isWhitespace(word)){
						OutputStr.append(" ");
					}else{
						OutputStr.append(word);
					}
			    }else{
			    	OutputStr.append(word);
			    }
			}
		}
	}
	
}

java实现的stack


/**
* Implement a stack with array.
* function:
* 
*/ 
public class Stack {
	
	  char[] data; 
	   
	  int maxSize;   
	  //top of the stack
	  int top;       
	   
	  public Stack(int maxSize) {       
	      this.maxSize = maxSize;       
	      data = new char[maxSize];       
	      top = -1;       
	  }       
	    
	  /**
	   * get the Size of the stack
	   * @return maxSize
	   */ 
	  public int getSize() 
	  { 
	    return maxSize; 
	  } 
	   
	  /**
	   * return the number of the elements of the stack,
	   * 
	   * BE CAREFUL:
	   * it is not correct when you first calculate, because top = -1;
	   * @return top
	   */ 
	  public int getElementCount() 
	  { 
	    return top; 
	  } 
	   
	  /**
	   * Determine the stack is empty
	   * @return 
	   */ 
	  public boolean isEmpty() 
	  { 
	    return top == -1; 
	  } 
	   
	  
	  public void setEmpty(){
		  top = -1;
	  }
	  
	  /**
	   * Determine the stack is full
	   * @return 
	   */ 
	  public boolean isFull() 
	  { 
	    return top+1 == maxSize; 
	  } 
	   
	  /**   
	   * push the data
	   * @param data 
	   * @return true when push successful,otherwise if false   
	   */       
	  public boolean push(char data) {       
	    if(isFull())  
	    {       
	        System.out.println("The stack is full.");       
	        return false;       
	    }       
	    this.data[++top] = data;       
	    return true;       
	  }       
	         
	  /**   
	   * get data from the stack  
	   * @return get data
	   */       
	  public char pop() throws Exception{       
	    if(isEmpty())  
	    {       
	        throw new Exception("The stack is empty.");       
	    }       
	    return this.data[top--];       
	  }       
	   
	  /**
	   * Back to the top of the stack elements
	   * @return
	   */ 
	  public char peek() 
	  { 
	    return this.data[getElementCount()];   
	  } 

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值