`

堆栈(Stack)示例

    博客分类:
  • Java
 
阅读更多

//Demonstrate the Stack class.
import java.util.*;
class StackDemo{
 static void showPush(Stack st,int a){
  st.push(new Integer(a));
  System.out.println("push("  + a + ")");
  System.out.println("stack: " + st);
 }
 static void showPop(Stack st){
  System.out.print("pop --> ");
  Integer a = (Integer)st.pop();
  System.out.println(a);
  System.out.println("stack: " + st);
 }
 public static void main(String[] args)
 {
  Stack st = new Stack();

  System.out.println("stack: " + st);
  showPush(st,9);
  showPush(st,19);
  showPush(st,99);
  System.out.println();

  showPop(st);
  showPop(st);
  showPop(st);
  try{
   showPop(st);
  }catch(EmptyStackException e){
   System.out.println("Empty stack");
  }
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics