2015-11-07 47 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