使用堆栈反转句子需要帮助

时间:2014-02-22 02:37:07

标签: java

您好我试图扭转我的判决。例如,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();
                }   
             }    
         }   
    }

1 个答案:

答案 0 :(得分: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());
  }