Java 链表堆栈/泛型实现(家庭作业)

Java 链表堆栈/泛型实现(家庭作业),java,Java,我正在尝试使用堆栈和链表构建回文检查器。我使用泛型来重用节点和结构,以完成本任务的两部分(在下一部分中完成不同的任务) 程序没有将字母推送到堆栈上,而是返回空值。我认为问题在于push方法的构造,无论是LinkedStack构造还是StackDriver实现,或者两者兼而有之。我只是不确定我做错了什么;我尝试了许多替代方法,并且查找并尝试了其他方法来构造push方法,但是随后我出现了错误,根本无法让程序运行。(我意识到我这里的两种推送方法是不同的——这是我尝试过的两种版本)。我应该看看char

我正在尝试使用堆栈和链表构建回文检查器。我使用泛型来重用节点和结构,以完成本任务的两部分(在下一部分中完成不同的任务)

程序没有将字母推送到堆栈上,而是返回空值。我认为问题在于push方法的构造,无论是LinkedStack构造还是StackDriver实现,或者两者兼而有之。我只是不确定我做错了什么;我尝试了许多替代方法,并且查找并尝试了其他方法来构造push方法,但是随后我出现了错误,根本无法让程序运行。(我意识到我这里的两种推送方法是不同的——这是我尝试过的两种版本)。我应该看看char c使用包装器类的一些装箱类型吗

程序已被设置回其运行的最后一点。“反向”弹出的元素似乎得到了正确的字符数量——为什么

我意识到这个项目还有其他问题,但我觉得在我克服这个障碍之前,我无法解决这些问题。任何帮助都将不胜感激-谢谢

迈克

给定接口:

public interface Stack<E> {
   void push(E data);
   E pop();
   boolean isEmpty();
   int size();
   E stackTop();
   void clear();
}
公共接口堆栈{
无效推送(E数据);
E pop();
布尔isEmpty();
int size();
E stackTop();
无效清除();
}
节点和方法:

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;
    }
公共类节点{
//创建节点结构
私人电子数据;
私有节点下一步;
//接球手和接球手
公共E getData(){
返回数据;
}
公共无效设置数据(E数据){
这个数据=数据;
}
公共节点getNext(){
下一步返回;
}
公共void setNext(节点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.EmptyStackException;
公共类LinkedStack实现堆栈{
//创建head和nodeCount变量
专用节点头;
私有int节点计数;
//还需要能够将字母转换为大写字母。
//LinkedStack的构造函数
公共LinkedStack()
{
清除();
}
//将数据推送到堆栈上并增加节点计数的方法
公共无效推送(E数据){
头=新节点();
nodeCount++;
}
//将磁头从堆栈中弹出并减少节点计数
公共E-pop(){
E项目;
if(head==null)
抛出新的EmptyStackException();
item=head.getData();
head=head.getNext();
节点计数--;
退货项目;
}
//检查堆栈是否为空
公共布尔值为空(){
如果(head==null);
返回true;
}
//检查节点的大小
公共整数大小(){
返回nodeCount;
}
//这是peek方法
公共E stackTop()
{
if(head==null)
抛出新的EmptyStackException();
返回head.getData();
}
//清除链接堆栈
公共空间清除(){
head=null;
nodeCount=0;
}
//转换为文本
公共字符串toString(){
字符串rtn=“”;
如果(nodeCount==0){
rtn+=”;
}
否则{
节点t=头部;
while(t!=null){
/*返回带有头部节点数据的行上的节点数据
在行首和指向的箭头处
每个连续节点*/
rtn+=t.getData()+“->”;
//继续下一步
t=t.getNext();
}
rtn+=“空”;
}
返回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

}


}
import java.util.Iterator;
导入java.util.Scanner;
公共类StackDriver实现迭代器{
/**
*@param args
*/
公共静态void main(字符串[]args){
//初始化驱动程序
StackDriver run=新的StackDriver();
run.doIt();
}
公共无效doIt(){
//收集输入
扫描仪键盘=新扫描仪(System.in);
System.out.println(“请输入短语。此程序将验证”+
“如果这个短语是回文。”);
//短语的持有者
字符串短语;
//反向短语的持有者
字符串反转=”;
短语=键盘.nextLine().toUpperCase();
System.out.println(“您输入:“+短语”);
//为角色创建两个堆栈
LinkedStack alpha=新LinkedStack();
LinkedStack标点=新建LinkedStack();
//------------------------------------------

对于(int i=0;i如果我正确地回答了您的问题,我认为问题确实在于
LinkedStack
类中的
push
方法。请看一看

public void push(E data) {
    head = new Node<E>();
    nodeCount++;
}
公共无效推送(E数据){
头=新节点();
nodeCount++;
}

创建一个新的
节点
,将其分配给
头部
,并增加堆栈中的节点数,但您从未实际链接旧头部或填充新的当前头部,您只需将头部替换为没有上一个或下一个元素的新节点。

如果我正确理解了您的问题,我认为问题确实是您的问题<在
LinkedStack
类中的code>push
方法。看一看

public void push(E data) {
    head = new Node<E>();
    nodeCount++;
}
// A method to push the data onto a stack and increment the node count
public void push(E data) {
    head = new Node<E>();
    nodeCount++;
    }
公共无效推送(E数据){
头=新节点();
nodeCount++;
}
创建一个新的
节点
,将其分配给
,并增加堆栈中的节点数,但实际上从未链接旧头或填充新的当前头,只需将头替换为没有上一个或下一个元素的新节点。

//将数据推送到堆栈上并增加节点的方法计数
// A method to push the data onto a stack and increment the node count
public void push(E data) {
    head = new Node<E>();
    nodeCount++;
    }
公共无效推送(E数据){ 头=新节点(); nodeCount++; }
此方法错误。当推送新节点时,需要将其设置为当前标头的下一个。。还需要使用数据填充该节点。

//将数据推送到堆栈并递增