我正在尝试使用堆栈和链接列表构建回文检查器。我使用泛型是为了重用节点和结构来完成这个任务的两个部分(在下一部分完成一些不同的事情)。
该程序没有将字母压入堆栈 - 它返回空值。我认为问题在于 push 方法的构造,无论是在 LinkedStack 构造中,还是在 StackDriver 实现中,或两者兼而有之。我只是不确定我做错了什么;我尝试了多种替代方法,并查找并尝试了其他方法来构建推送方法,但随后出现错误并且根本无法运行程序。(我意识到我在这里使用的 2 种推送方法是不同的——这是我尝试过的 2 个版本)。我应该为 char c 寻找某种类型的装箱来使用包装器类吗?
该程序已被设置回它运行的最后一点。“反转”的弹出元素似乎获得了正确数量的字符 - 为什么?
我意识到这个程序还有其他问题,但我觉得在我克服这个绊脚石之前我无法解决这些问题。任何帮助将不胜感激 - 谢谢!
麦克风
给定的接口:
public interface Stack<E> {
void push(E data);
E pop();
boolean isEmpty();
int size();
E stackTop();
void clear();
}
节点和方法:
public class Node<E> {
// create the node structure
private E data;
private Node<E> next;
// getters and setters
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
堆栈:
import java.util.EmptyStackException;
public class LinkedStack<E> implements Stack<E>{
// Create the head and nodeCount variables
private Node<E> head;
private int nodeCount;
// also need to be able to convert letters to capitals.
// constructor for the LinkedStack
public LinkedStack()
{
clear();
}
// A method to push the data onto a stack and increment the node count
public void push(E data) {
head = new Node<E>();
nodeCount++;
}
// pop the head off of the stack and decrement the node count
public E pop() {
E item;
if (head == null)
throw new EmptyStackException();
item = head.getData();
head = head.getNext();
nodeCount--;
return item;
}
// Check if the stack is empty
public boolean isEmpty() {
if (head == null);
return true;
}
// check the size of the node
public int size() {
return nodeCount;
}
// this is the peek method
public E stackTop()
{
if (head == null)
throw new EmptyStackException();
return head.getData();
}
// clear the Linked Stack
public void clear() {
head = null;
nodeCount = 0;
}
// convert to text
public String toString() {
String rtn = "";
if (nodeCount == 0) {
rtn += "<empty>";
}
else {
Node<E> t = head;
while (t != null){
/* return the node data on a line with the head node data
at the beginning of the line and the arrow pointing to
each successive node*/
rtn += t.getData() + "->";
// go on to the next
t = t.getNext();
}
rtn += "null";
}
return rtn;
}
}
还有司机:
import java.util.Iterator;
import java.util.Scanner;
public class StackDriver<E> implements Iterator<E>{
/**
* @param args
*/
public static void main(String[] args) {
//Initialize the driver
StackDriver run = new StackDriver();
run.doIt();
}
public void doIt() {
// gather the input
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a phrase. This program will verify" +
" if the phrase is a palindrome.");
// holder for the phrase
String phrase;
// holder for the reversed phrase
String reversed = "";
phrase = keyboard.nextLine().toUpperCase();
System.out.println("You entered: "+ phrase);
// create the two stacks for the characters
LinkedStack<E> alpha = new LinkedStack<E>();
LinkedStack<E> punctuation = new LinkedStack<E>();
//------------------------------------------
for(int i=0; i<phrase.length(); i++)
{
// if the character is a letter, push it onto the letters stack
char c = phrase.charAt(i);
if (true == Character.isLetter(c))
{
// (testing purposes only- remove next line)
System.out.println("LETTER");
String A = Character.toString(c);
// push the letter onto the stack
alpha.push((E) new Node<E>());
}
// else push it onto the characters stack
else
{
// (testing purposes only- remove next line)
System.out.println("NOT LETTER");
String B = Character.toString(c);
// push the character onto the stack
punctuation.push((E) new String(B));
}
// then pop the letters stack
while (!alpha.isEmpty());
{
reversed += alpha.pop();
}
}
//------------------------------------------
// if it equals the String phrase
if (reversed == phrase)
// it is a palindrome
System.out.println("The phrase you entered is a palindrome");
else
System.out.println("The phrase you entered is NOT a palindrome");
System.out.println("phrase: " + phrase);
System.out.println("alpha: " + alpha);
System.out.println("reversed: " + reversed);
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return true;
}
@Override
public E next() {
// TODO Auto-generated method stub
return null;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
}
结果:
Please enter a phrase. This program will verify if the phrase is a palindrome.
mom
You entered: MOM
LETTER
LETTER
LETTER
The phrase you entered is NOT a palindrome
phrase: MOM
alpha: <empty>
reversed: nullnullnull