2015-11-07 66 views
-2

這是一個接口的方法,我必須在另一個類中實現,我不知道如何創建它。我必須使用帶有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) {???} 

} 
+2

你怎麼想打印的堆棧,每行或單行的所有對象1個對象? – Zpetkov

+0

@Zpetkov每行1個 –

+0

您面臨的問題究竟是什麼?首先從頭節點打印數據,然後轉到下一個節點並打印。重複,直到沒有剩下的節點。 – Pshemo

回答

0

不知道如何是你的籌碼的結構,但這應該做的:

Node node = head; // top of the stack 

    while(node != null){ 
    stream.println(node.value); 
    stream.flush(); 
    node = node.next; 
    } 
+0

我必須打印直到給定的流,所以它會(while)(head.getObject()!= stream)? –

+0

您正在將堆棧中的每個對象都打印到OutputStream,該文件可以是文件或控制檯標準輸出。我認爲你需要更熟悉流和基本I/O。 – Zpetkov