Stack(v0.1)

类别:Java 点击:0 评论:0 推荐:

未定稿

 

1 package org.spring.ds.arraystack; 2 3 /** 4 * Created by IntelliJ IDEA. 5 * User: chunyang 6 * Date: 2003-6-20 7 * Time: 15:35:22 8 * Develop with pleasure!. 9 * Array based stack class 10 */ 11 public class MyArrayStack { 12 //Array holding stack Objects 13 protected Object[] array; 14 //Index for top Object 15 protected int top; 16 17 public MyArrayStack(int size) { 18 array = new Object[size]; 19 top = -1; 20 } 21 22 public boolean isEmpty() { 23 return top == -1; 24 } 25 /** 26 * Push object onto stack 27 * @param o 28 */ 29 public void push(Object obj) { 30 if (top + 1 < array.length) 31 array[++top] = obj; 32 } 33 34 /** 35 * Pop object from top of stack 36 * @return 37 */ 38 public Object pop() { 39 if (isEmpty()) 40 return null; 41 return array[top--]; 42 } 43 44 45 /** 46 *Remove all objects from stack 47 */ 48 public void clear() { 49 top = -1; 50 } 51 }//class MyArrayStack 52

本文地址:http://com.8s8s.com/it/it17668.htm