为什么我的程序无法检测回文?

| 我的任务是在程序中使用ADT List的基于引用的实现和ADT Stack的基于数组的实现,在该程序中,用户输入了一个小写字母字符串。我要遍历字符串并将每个字母存储在列表和堆栈中,然后使用堆栈和列表的内容来确定字符串是否是回文。我要显示原始字母顺序,以相反的顺序显示字母顺序,最后显示是否是回文式的说明。由于某种原因,当我输入回文时,例如。 madamimadam,它输出的不是回文。我不知道为什么,请帮忙!这是我的方法代码:
import javax.swing.JOptionPane;

public class PalindromeTester
{    
    public static void main (String [] args)
    {    
        Character ch;
        boolean isPalindrome = true;
        LinkedList myList = new LinkedList();
        StackArrayBased myStack = new StackArrayBased();
        String response = JOptionPane.showInputDialog (\"Please enter a string of lower-case letters\" ) ;

        for ( int i = 0 ; i < response.length ( ) ; i++ )
        {
            ch = new Character ( response.charAt ( i ) ) ;
            myStack.push ( ch ) ;
            myList.add ( i + 1 , ch ) ;
        }

        System.out.println ( \"The original sequence of characters is: \" + response ) ;
        System.out.print ( \"The sequence of letters backwards is: \" ) ;

        int j = 1 ;
        while ( ! myStack.isEmpty ( ) )
        {
            System.out.print ( myStack.peek ( ) ) ;
            if ( ! myList.get ( j ).equals( myStack.pop (  ) ) ) ;
            isPalindrome = false ;
        }

        if ( isPalindrome )
            System.out.println ( \"\\nThe string is a palindrome.\" ) ;
        else
            System.out.println ( \"\\nThe string is not a palindrome.\" ) ;
    }
}
这是ADT Stack类:
public class StackArrayBased
{
    private static final int MAX_STACK = 15 ;
    private Object items [ ] ;
    private int top ;    

    public StackArrayBased ( )
    {
        items = new Object [ MAX_STACK ] ;
        top = -1 ;
    }

    public boolean isEmpty ( )
    {
        return top < 0 ;
    } 

    public boolean isFull ( )
    {
        return top == MAX_STACK - 1 ;
    }

    public void push ( Object newItem ) throws StackException
    {
        if ( ! isFull ( ) )
            items [ ++ top ] = newItem ;
        else
            throw new StackException ( \"StackException on push: stack is full\" ) ;
    }

    public void popAll ( )
    {
        items = new Object [ MAX_STACK ] ;
        top = -1 ;
    }

    public Object pop ( ) throws StackException
    {
        if ( ! isEmpty ( ) )
            return items [ top -- ] ;
        else
            throw new StackException ( \"StackException on pop: stack is empty\" ) ;
    }

    public Object peek ( ) throws StackException
    {
        if ( ! isEmpty ( ) )
            return items [ top ] ;
        else
            throw new StackException ( \"StackException on peek: stack is empty\" ) ;
    }
}
这是ADT清单:
public class LinkedList
{
    private Node head;
    private int numItems;    

    public LinkedList ( )
    {
        head = null ;
        numItems = 0 ;
    }

    public boolean isEmpty ( )
    {
        return numItems == 0 ;
    }

    public int size ( )
    {
        return numItems ;
    }

    private Node find ( int position )
    {
        Node curr = head ;
        for ( int i = 1 ; i < position ; i ++ )
            curr = curr.getNext ( ) ;

        return curr ;
    }

    public Object get ( int position )
    {
        if ( position >= 0 && position <= numItems )
        {
            Node curr = find ( position ) ;
            Object dataItem = curr.getItem ( ) ;
            return dataItem ;
        }
        else
        {
            System.out.println ( \"Error in position value during get attempt.\" ) ;
            return null ;
        }
    }

    public void add ( int position, Object item )
    {
        if ( position >= 1 && position <= numItems + 1 )
        {
            if ( position == 1 )
            {
                Node newNode = new Node ( item, head ) ;
                head = newNode ;
            }
            else
            {
                Node prev = find ( position - 1 ) ;
                Node newNode = new Node ( item, prev.getNext ( ) ) ;
                prev.setNext ( newNode ) ;
            }

            numItems ++ ;
        }
        else
            System.out.println ( \"Position is invalid on attempted add.\" ) ;
    }

    public void remove ( int position )
    {
        if ( position >= 1 && position <= numItems )
        {
            if ( position == 1 )
                head = head.getNext ( ) ;
            else
            {
                Node prev = find ( position - 1 ) ;
                Node curr = prev.getNext ( ) ;
                prev.setNext ( curr.getNext ( ) ) ;
            }

            numItems -- ;
        }
        else
            System.out.println ( \"Position is invalid on attempted remove.\" ) ;
    }

    public void removeAll ( )
    {
        head = null ;
        numItems = 0 ;
    }
}
已邀请:
如果要正确设置
isPalindrome
,是否不应该在此循环中对
j
进行操作...?:
[...]
int j = 1 ;
while ( ! myStack.isEmpty ( ) )
{
  System.out.print ( myStack.peek ( ) ) ;
  if ( ! myList.get ( j ).equals( myStack.pop (  ) ) ) ;
        isPalindrome = false ;
}
[...]
在第二个循环中,您应该递增j。由于链表的索引可以为0,因此添加(在第一个循环中)时,您不应该做i + 1索引。如果将其设为基于0的索引,则应在第二个循环之前将j初始化为0。
任务似乎很奇怪。如果您可以访问列表的最后一个元素(抽象列表允许使用大多数语言),则只需执行ѭ6 而且,如果您只有堆栈可玩,则可以克隆并反转堆栈(例如,推入两个堆栈,然后将其中一个弹出到新堆栈中,从而将其反转),然后执行与for循环相同的操作(逐个元素地检查两个堆栈,看它们是否相同)。 在以上两种线性时间算法中(仅使用列表的算法或仅使用堆栈的算法),函数的长度应仅为3行左右。

要回复问题请先登录注册