這是一個接口的方法,我必須在另一個類中實現,我不知道如何創建它。我必須使用帶有printStream參數的linkedList來打印堆棧。在類Node中(對於linkedList),我有一個方法getObject()。如何在java中使用printStream參數打印堆棧?
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) {???}
}
你怎麼想打印的堆棧,每行或單行的所有對象1個對象? – Zpetkov
@Zpetkov每行1個 –
您面臨的問題究竟是什麼?首先從頭節點打印數據,然後轉到下一個節點並打印。重複,直到沒有剩下的節點。 – Pshemo