开发者

Why does my program not detect palindromes?

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 ( "StackExcep开发者_开发知识库tion 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 ;
    }
}


If you want to set isPalindrome correctly, shouldn't you be doing something with j in this loop...?:

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜