我如何使用 java 中的 printStream 参数打印堆栈?
How i print a stack using printStream argument in java?
这是我必须在另一个 class 中实现的接口方法,但我不知道如何创建它。我必须使用带 printStream 参数的 linkedList 来打印堆栈。在 class 节点(对于 linkedList)中,我有一个方法 getObject().
import java.io.PrintStream;
import java.util.NoSuchElementException;
public interface StringStack {
public boolean isEmpty();
public void push(String item);
public String pop() throws NoSuchElementException;
public String peek() throws NoSuchElementException;
/**
* print the contents of the stack, starting from the item
* on the top,
* to the stream given as argument. For example,
* to print to the standard output you need to pass System.out as
* an argument. E.g.,
* printStack(System.out);
*/
public void printStack(PrintStream stream);
public int size();
}
public class StringStackImpl implements StringStack {
private Node head;
....
public void printStack(PrintStream stream) {???}
}
不确定堆栈的结构如何,但应该这样做:
Node node = head; // top of the stack
while(node != null){
stream.println(node.value);
stream.flush();
node = node.next;
}
这是我必须在另一个 class 中实现的接口方法,但我不知道如何创建它。我必须使用带 printStream 参数的 linkedList 来打印堆栈。在 class 节点(对于 linkedList)中,我有一个方法 getObject().
import java.io.PrintStream;
import java.util.NoSuchElementException;
public interface StringStack {
public boolean isEmpty();
public void push(String item);
public String pop() throws NoSuchElementException;
public String peek() throws NoSuchElementException;
/**
* print the contents of the stack, starting from the item
* on the top,
* to the stream given as argument. For example,
* to print to the standard output you need to pass System.out as
* an argument. E.g.,
* printStack(System.out);
*/
public void printStack(PrintStream stream);
public int size();
}
public class StringStackImpl implements StringStack {
private Node head;
....
public void printStack(PrintStream stream) {???}
}
不确定堆栈的结构如何,但应该这样做:
Node node = head; // top of the stack
while(node != null){
stream.println(node.value);
stream.flush();
node = node.next;
}