我的任务是在一个程序中使用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 ;
}
}
答案 0 :(得分:0)
如果您想正确设置isPalindrome
,那么您不应该在此循环中使用j
做某事......?:
[...]
int j = 1 ;
while ( ! myStack.isEmpty ( ) )
{
System.out.print ( myStack.peek ( ) ) ;
if ( ! myList.get ( j ).equals( myStack.pop ( ) ) ) ;
isPalindrome = false ;
}
[...]
答案 1 :(得分:0)
在第二个循环中,您应该递增j。由于linkedlist索引可以为0,因此在添加时(第一个循环中)不应该执行i + 1索引。如果你使它成为一个基于0的索引,你应该在第二个循环之前将j初始化为0。
答案 2 :(得分:0)
任务似乎很奇怪;如果你可以访问列表的最后一个元素(如大多数语言中的抽象列表所允许的那样),那么你可以只执行for i=[0,length) {if input[i]!=input[length-1-i], return false} return true
如果你只是要使用堆栈,你可以克隆并反转堆栈(例如,通过推入两个堆栈,然后将其中一个弹出一个新的堆栈,从而将其反转),并执行相同的操作for循环(逐个元素地遍历两个堆栈以查看它们是否相同)。
在上述两种线性时间算法(使用仅列表的算法或仅使用堆栈的算法)中,函数应该只有3行左右。