0

我的任务是在让用户输入小写字母字符串的程序中使用 ADT 列表的基于引用的实现和 ADT 堆栈的基于数组的实现。我将遍历字符串并将每个字母存储在列表和堆栈中,然后使用堆栈和列表内容来确定字符串是否为回文。我要显示原始的字母序列,逆序的字母序列,最后,一个语句是否是回文。出于某种原因,当我输入回文时,例如。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 堆栈类:

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 ;
    }
}
4

3 回答 3

0

如果你想isPalindrome正确设置,你不应该j 在这个循环中做些什么吗......?:

[...]
int j = 1 ;
while ( ! myStack.isEmpty ( ) )
{
  System.out.print ( myStack.peek ( ) ) ;
  if ( ! myList.get ( j ).equals( myStack.pop (  ) ) ) ;
        isPalindrome = false ;
}
[...]
于 2011-04-18T20:19:29.537 回答
0

这个任务看起来很奇怪。如果您可以访问列表的最后一个元素(作为大多数语言中允许的抽象列表),那么您可以执行for i=[0,length) {if input[i]!=input[length-1-i], return false} return true

如果你只有堆栈可以玩,你可以克隆和反转堆栈(例如,通过压入两个堆栈,并将其中一个弹出到一个新堆栈中,从而反转它),并执行与 for 循环相同的操作(逐个元素地检查两个堆栈,看看它们是否相同)。

在上述两种线性时间算法中(要么只使用列表,要么只使用堆栈),函数应该只有 3 行左右。

于 2011-04-18T20:23:58.140 回答
0

在第二个循环中,您应该递增 j。由于链表索引可以为 0,因此在添加 ( 在第一个循环中) 时不应执行 i+1 索引。如果将其设为基于 0 的索引,则应在第二个循环之前将 j 初始化为 0。

于 2011-04-18T20:20:37.673 回答