[英]How i print a stack using printStream argument in java?
This is a method from an interface which i have to implement in another class and i don't know how to create it. 这是来自接口的一种方法,我必须在另一个类中实现,但我不知道如何创建它。 I have to print the stack using linkedList with printStream argument.
我必须使用带有printStream参数的linkedList打印堆栈。 Inside the class Node(for linkedList) i have a method getObject().
在类Node(forlinkedList)中,我有一个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) {???}
}
Not sure how is the structure of your stack but this should do: 不确定堆栈的结构如何,但是应该这样做:
Node node = head; // top of the stack
while(node != null){
stream.println(node.value);
stream.flush();
node = node.next;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.