Question: Using a Linked List structure, determine whether a linked list contains a Palindrome in integers. For example, the following is a Palindrome: 0-> 1 ->

Using a Linked List structure, determine whether a linked list contains a Palindrome in integers. For example, the following is a Palindrome:

0-> 1 -> 2 -> 1 -> 0

To solve this problem, following these steps:

1.) reverse the linked list

2.) compare the reversed list to the original list

3.) if they're the same, you have a Palindrome.

Create the following methods in a helper class. From the tester/driver class, call isPalindrome(aLL) from main. Create the linked list by hard coding values in the linked list. For example:

LinkedList goodTest = new LinkedList ();

goodTest.addLast("0"); goodTest.addLast("1"); goodTest.addLast("2"); goodTest.addLast("1"); goodTest.addLast("0")

boolean isPalindrome(LinkedList aLL) ~ given a linked list, will return whether the linked list is a Palindrome.

LinkedList reverseAndClone(LinkedList aLL) ~ given a linked list, will return the reversed linked list.

boolean isEqual(LinkedList one, LinkedList two) ~ given the 2 linked lists, will return whether they are both the same.

To test the program, in main method:

1. create a linked list that is a Palindrome, and call the isPalindrome method, passing it a linkedList. Test to ensure it returns True. 2. create a linked list that is NOT a Palindrome, and call the isPalindrome method passing it a linkedList. Test to ensure it returns a False.

For extra credit:

1.) Instead of hard coding the linked list of Strings, create an input file that contains 5 words, one word per line. Make some of the words be Palindromes, like "racecar," and other words NOT be Palindromes, like "butterfly". 2.) In main, read the file, using try catch blocks for IOException, asking the user for the name of the file, and looping until the correct file name is entered. 3.) Once the file is read, take each word in the file, and create a linked list with each letter of the word. This will need a new method called parseWord(String aWord). 4.) Then, continue on with calling the other methods starting with isPalindrome(LinkedList LL), which in turn calls reverseAndClone(LinkedList LL), and then calls isEqual(LinkedList L1, LinkedList L2) passing it the original linked list, and the linked list returned from reverseAndClone method. 5.) Print out the word, and whether it is a Palindrome or not.

I would like to go towards the extra credit part

Skeleton

**********Driver**********

package palindrometester;

import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner;

public class PalindromeTester {

public static void main(String[] args) { File myFile; Scanner inFile = null; String word; LinkedList possiblePal; HelperPal myPalHelp = new HelperPal(); try { myFile = new File("candidates.txt"); inFile = new Scanner(myFile); while (inFile.hasNext()) { word = inFile.next(); possiblePal = makeLinkedList(word); if (myPalHelp.isPalindrome(possiblePal)) { System.out.println(word + " is a Palindrome."); } else { System.out.println(word + " is NOT a Palindrome."); } } } catch (IOException e) { System.out.println("Sorry, wrong file. Come back later."); } finally { if (inFile != null) inFile.close(); } } public static LinkedList makeLinkedList(String word) { return new LinkedList(); } }

***********Helper Class**********

package palindrometester;

import java.util.LinkedList;

public class HelperPal { public boolean isPalindrome(LinkedList aLL) { return true; } public LinkedList reverseAndClose(LinkedList inputLL) { return new LinkedList(); } public boolean isEqual(LinkedList aLL1, LinkedList aLL2) { return true; } }

***********Text File (candidates.txt)**********

01210 racecar mom butterfly noon madam

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!