Java Reference
In-Depth Information
Step 2 will convert it to this:
list1
d
a
m
n
m
a
d
Step 3 will reverse this list to get the following:
list2
d
a
m
n
m
a
d
Comparing
list1
and
list2
will reveal that
Damn Mad!
is a palindrome.
We will write a program that prompts the user to type a phrase and tells her whether it is a palindrome. It then
prompts for another phrase. To stop, the user must press the Enter key. The following is a sample run:
Type a phrase. (To stop, press "Enter" only): Damn Mad!
is a palindrome
Type a phrase. (To stop, press "Enter" only): So Many Dynamos!
is a palindrome
Type a phrase. (To stop, press "Enter" only): Rise to vote, sir.
is a palindrome
Type a phrase. (To stop, press "Enter" only): Thermostat
is not a palindrome
Type a phrase. (To stop, press "Enter" only): A Toyota's a Toyota.
is a palindrome
Type a phrase. (To stop, press "Enter" only):
Previously, we worked with a linked list of integers. But, now, we need a linked list of characters. If we did things
right, we should need to make changes in the
NodeData
class only. We should not have to change
anything
in the
LinkedList
class, and we won't. Here is what
NodeData
should look like:
public class NodeData {
char ch;
public NodeData(char c) {
ch = c;
}
public char getData() {return ch;}
public int compareTo(NodeData nd) {
if (this.ch == nd.ch) return 0;
if (this.ch < nd.ch) return -1;
return 1;
}
public String toString() {
return ch + "";
}
} //end class NodeData
Search WWH ::
Custom Search