2017-01-10 24 views
2

進出口工作在LinkedListStack並具有打印出如「大小」。 這裏是我的LinkedListStackLinkedListStack的System.out.println

public class LinkedListStack { 

    private class Element { 
     public Object value; 
     public Element next; 
    } 

    private Element top; 
    private int size=0; 

    public void push(Object o) { 
     Element e=new Element(); 
     e.value=o; 
     e.next=top; 
     top=e; 
     size++; 
    } 

    public Object pop() { 
     if (top!=null) { 
      Object v=top.value; 
      top=top.next; 
      size--; 
      return v; 
     } else { 
      return null; 
     } 
    } 

    public boolean isEmpty(){ 
     return top==null; 
    } 

    public int size() { 
     return size; 
    } 

    public Object get(int n) { 
     Element current=top; 
     int i=0; 
     while (i<n && current!=null) { 
      current=current.next; 
      i++; 
     } 
     if (current==null) 
      return null; 
     else 
      return current.value; 
    } 

} 

我知道我必須使用

System.out.println ("..."); 

,而我需要一個新的類,我們稱之爲Stacki。必須包含main方法,我可以使用這些方法並將它們打印出來。因此,這將是:

public class Stacki { 
    public static void main(String[] args) { 
    } 
} 

我怎麼把

public void size() { 
    System.out.println ("size is"+size()); 
} 

在這門課嗎?因爲我不能像這樣使用塊,所以會發生錯誤。

謝謝:)

+1

您需要首先定義** LinkedListStack **的「對象」 – Null

回答

3

您實例內Stacki你的其他類,如:

public class Stacki { 

public static void main(String[]) { 
    LinkedListStack stack = new LinkedListStack(); 
    System.out.println("size is: " + stack.size()); 

......那麼你很可能添加一些元素,去掉一些,每當你想:

System.out.println("size is: " + stack.size()); 

而且提示:Stacki是一個相當沒什麼,說的名字。最好調用該類LinkedListStackTester或類似的東西。名字總是說出他們表示的東西是關於什麼的!

最後:這實在是基本東西。當你不知道如何使用它時,創建你自己的堆棧類沒什麼意義。從這個意義上說:你可能要花上幾個小時here,並通過這些教程努力工作!