为什么我的程序无法检测回文?
我的任务是在一个程序中使用 ADT 列表的基于引用的实现和 ADT 堆栈的基于数组的实现,该程序让用户输入一串小写字母。我要遍历字符串并将每个字母存储在列表和堆栈中,然后使用堆栈和列表内容来确定字符串是否是回文。我要显示原来的字母序列,逆序的字母序列,最后声明是否是回文。由于某种原因,当我输入回文时,例如。女士,它输出它不是回文。我不明白为什么,请帮忙!这是我的方法代码:
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 ;
}
}
My assignment was to use the reference-based implementation of the ADT List and the array-based implementation of the ADT Stack in a program that has a user enter a string of lower-case letters. I was to go through the string and store each letter in both the list and the stack and then use the stack and list contents to determine if the string is a palindrome or not. I am to display the original sequence of letters, the sequence of letters in reverse order, and finally, a statement whether or not it is a palindrome or not. For some reason, when I input a palindrome, ex. madamimadam, it outputs that it is not a palindrome. I cannot figure out why, please help! Here is my code for the method:
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." ) ;
}
}
Here is the ADT Stack class:
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" ) ;
}
}
and here is the ADT list:
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 ;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想正确设置
isPalindrome
,你不应该在这个循环中对j
做一些事情吗...?:If you want to set
isPalindrome
correctly, shouldn't you be doing something withj
in this loop...?:在第二个循环中,您应该递增 j。由于链表索引可以为 0,因此在添加(在第一个循环中)时不应该执行 i+1 索引。如果将其设为基于 0 的索引,则应在第二个循环之前将 j 初始化为 0。
In the second loop, you should be incrementing j. Since linkedlist index can be 0, you shouldn't be doing i+1 index while adding ( in the first loop). If you make it a 0 based index you should be initalizing j to be 0 before the second loop.
这个任务看起来很奇怪;如果您可以访问列表的最后一个元素(大多数语言都允许抽象列表),那么您只需执行
for i=[0,length) {if input[i]!=input[length- 1-i], return false} return true
如果您只有堆栈可供使用,您可以克隆并反转堆栈(例如,通过压入两个堆栈,然后将其中一个弹出到新堆栈中,从而反转它),并执行与 for 循环相同的操作(逐个元素地遍历两个堆栈以查看它们是否相同)。
在上述两种线性时间算法(无论是仅使用列表的算法,还是仅使用堆栈的算法)中,函数应该只有 3 行左右。
The assignment seems odd; if you can access the last element of the list (as an abstract list allows in most language), then you can just do the
for i=[0,length) {if input[i]!=input[length-1-i], return false} return true
And if you just had stacks to play with, you could just clone and reverse the stack (e.g. by pushing onto two stacks, and popping one of those into a new stack, thereby reversing it), and do the same thing as the for loop (go through the two stacks element-by-element to see if they're the same).
In both of the above linear-time algorithms (either the one that uses just lists, or the one that uses just stacks), the function should just be 3 lines or so.