0

首先,代码很长,但我只关心 toString 方法。我粘贴了所有内容以防万一。

该代码应该检查输入的元素是否小于堆栈顶部的元素。我已经弄清楚了那部分。当我调用我的 toString 方法来打印保存的输入时,它不会打印添加到堆栈中的第一个节点。但是,它会打印所有剩余的输入。例如,如果用户输入以下内容:

testing
test
te
t

What its currently outputing:
t te test

What it needs to output:
t te test testing

主要的:

import java.util.*;

public class Stack {
public static void main(String args[]) {
    int loopInt = 1;
    PyramidStack<String> stringStack = new PyramidStack<String>();
    PyramidStack<Integer> intStack = new PyramidStack<Integer>();

    System.out
            .println("This program will save some of the strings you enter. ");
    System.out
            .println("Can you predict which ones will be saved?  (Enter nothing to quit.)");
    Scanner sc = new Scanner(System.in); // Opens Scanner for keyboard input

    try {
        do {
            System.out.print("Enter a String: ");
            String input = sc.nextLine();

            if (input.length() > 0) {

                if (stringStack.size() == 0) {
                    intStack.push(input.length());
                    stringStack.push(input);
                    System.out.println("String Saved");
                }

                else if (input.length() < intStack.peek()) {
                    stringStack.push(input);
                    intStack.push(input.length());
                    System.out.println("String Saved");

                }
                else {
                    System.out.println("String NOT saved. Already saved "
                            + intStack.countBefore(input.length())
                            + " strings that should come before this one.");
                }
            } else {
                System.out.println();
                System.out.println(stringStack.toString());
                System.out.println(intStack.toString());

                loopInt--;
                sc.close();
            }
        } while (loopInt > 0);

    } catch (NullPointerException e) {
        System.out.println("No strings have been entered. Ending the program.");
    }
}
}

PyramidStack 方法:

import java.util.*;
import java.lang.Comparable;

public class PyramidStack<E extends Comparable<E>> extends Stack<E> {

@Override
public void push(E item) throws IllegalArgumentException {

    if (super.size == 0) {
        super.push(item);

    }
    else if(item.compareTo(super.peek()) <= 0) {

        super.push(item);
    } 
    else {
        System.out.println("String NOT saved. " + countBefore(item) +  " strings that should come before this one.");
    }
}

@Override
public String toString() {
    Node<E> node;
    node = this.top;
    String s = "";

    while(node.getNext() != null){
        s += node.getData() + " ";
        node = node.getNext();
    }    
    return s;
}


public int countBefore(E item) {
    Node<E> node;
    node = this.top;
    int i = 0;

    while(node.getNext() != null){
        if(item.compareTo(super.peek()) <= 0) {
        node = node.getNext();
        }
        else{
        i++;
        node = node.getNext();
        }
    }
    return i;
}
}

堆栈方法和节点方法在下面的评论中。

4

1 回答 1

2

while(node.getNext() != null){ s += node.getData() + " "; node = node.getNext(); }

这是您缺少打印最后一个数据的地方..您正在检查 node.getnext() 是否为空..因此它会跳过最后一个对象并且不打印..您的条件应该是
node != null
OR
在循环结束后有这个
s += node.getData() -- 这不会给出空指针异常,因为节点不为空.. 因为它确实检查了 node.getnext()

希望能帮助到你

于 2013-02-23T08:49:23.597 回答