2014-02-22 34 views
-2

你好,我正試圖扭轉我的句子。例如,hello = olleh。我已經寫了代碼,但有一些錯誤,我試圖弄清楚。你能幫我解決這個問題嗎? 這是我的代碼。需要幫助反向使用堆棧的句子

import java.util.*;  
    public class ArrayStack<T> implements StackADT<T>  
    {  
     private final static int DEFAULT_CAPACITY = 100;  
     private int top; 
     private T[] stack; 

     /** 
     * Creates an empty stack using the default capacity.  
     */ 

     public ArrayStack() 
     { 
      this(DEFAULT_CAPACITY); 
     }  

     /** 
     * Creates an empty stack using the specified capacity.  
     * @param initialCapacity the initial size of the array 
     */ 

     public ArrayStack(int initialCapacity) 
     { 
      top = 0; 
      stack = (T[])(new Object[initialCapacity]);  
     }  

     /** 
     * Adds the specified element to the top of this stack, expanding 
     * the capacity of the array if necessary. 
     * @param element generic element to be pushed onto stack 
     */ 

     public void push(T element) 
     { 
      if (size() == stack.length) 
      expandCapacity();   
      stack[top] = element; 
      top++; 
     }  

     /** 
     * Creates a new array to store the contents of this stack with 
     * twice the capacity of the old one. 
     */ 

     private void expandCapacity() 
     { 
      stack = Arrays.copyOf(stack, stack.length * 2); 
     }  

     /** 
     * Removes the element at the top of this stack and returns a 
     * reference to it. 
     * @return element removed from top of stack  
     * @throws EmptyCollectionException if stack is empty 
     */ 

     public T pop() throws EmptyCollectionException 
     { 
      if (isEmpty()) 
       throw new EmptyCollectionException("stack");   
      top--; 
      result = stack[top]; 
      stack[top] = null; 
      return result; 
     } 


     /** 
     * Returns a reference to the element at the top of this stack. 
     * The element is not removed from the stack. 
     * @return element on top of stack 
     * @throws EmptyCollectionException if stack is empty 
     */ 

     public T peek() throws EmptyCollectionException 
     { 
      if (isEmpty()) 
       throw new EmptyCollectionException("stack");   
      return stack[top-1]; 
     } 

     /**  
     * Returns true if this stack is empty and false otherwise. 
     * @return true if this stack is empty 
     */ 

     public boolean isEmpty() 
     { 
      return stack.length == 0; 
     } 

     /** 
     * Returns the number of elements in this stack. 
     * @return the number of elements in the stack 
     */ 

     public int size() 
     { 
      return top; 
     } 

     public static void main(String[] args) 
     {  
      ArrayStack<Character> stack = new ArrayStack<Character>(); 
      String sentence = " ", word; 
      Scanner in = new Scanner(System.in); 
      System.out.println("Enter a sentence:"); 
      sentence= in.nextLine(); 
      System.out.println("Reversing each word:"); 
      Scanner sentenceScanner = new Scanner(sentence); 

      while(sentenceScanner.hasNext()) 
      { 
       word = sentenceScanner.next();  
       for(int i= 0; i<word.length(); i++)   
       { 
        System.out.print(word.charAt(i)); 
       } 

       for(int i = word.length()-1; i>=0; i--) 
       { 
        stack.pop(); 
       } 
      }  
     } 
    } 
+0

你爲什麼不使用'堆棧'開始? –

+0

它有什麼問題?你得到什麼錯誤或什麼行爲不正確? – Hugo

+0

_something wrong_是什麼意思? – lakshman

回答

1

您從不向堆棧添加任何內容,這就是爲什麼它不以相反順序打印的原因;相反,你可以明確地打印字符,以便在這裏:

 for(int i= 0; i<word.length(); i++) 

     { 

      System.out.print(word.charAt(i)); 

     } 

您僅僅是字循環,才能,並打印每個字符。我相信你所要做的就是將字符添加到堆棧中,然後在下一個循環中打印出字符,然後從堆棧中彈出它們。

所以,你的代碼應該看起來更像是這樣的:

for(int i= 0; i<word.length(); i++)   
    { 
    stack.push(word.charAt(i)); 
    } 

    while (!stack.isEmpty()) //instead of for(int i = word.length()-1; i>=0; i--) 
    { 
    System.out.print(stack.pop()); 
    } 
+0

那我該怎麼辦? – user3339580

+0

我編輯了一個應用程序的例子,該應用程序可以將字符添加到堆棧,然後在彈出它們時將其打印出來。 – Jsdodgers

+0

劇透警報! :) – WonderWorld