public class Stack {   
    
    
//栈的最大长度
    private int maxsize;   
    
//栈中的元素
    private int[] datas;   
    
//栈顶的位置
    private int top;   
    
    
public Stack() {
        
this.maxsize = 100;        
        datas 
= new int[100];
        top 
= -1;
    }
    
    
public Stack(int maxsize) {   
        
this.maxsize = maxsize;   
        datas 
= new int[maxsize];   
        top 
= -1;   
    }   

    
/**  
     * 功能:插入元素data为新的栈顶元素  
     * 参数:Object data  
     
*/   
    
public void push(int data) {   
        
if (top < maxsize - 1) {   
            datas[
++top] = data;   
        }   
    }   

    
/**  
     * 功能:从栈中取出数据  
     * 返回值:Object 从栈中取出的元素,堆栈是空的就返回-10001  
     
*/   
    
public int pop() {   
        
if (top == -1)   
            
return -10001;   
        
return datas[top--];   
    }   

    
/**  
     * 功能:从栈中取出栈顶元素  
     * 返回值:Object 从栈中取出的元素,堆栈是空的就返回-10001  
     
*/   
    
public int getTop() {   
        
if (top == -1)   
            
return -10001;   
        
return datas[top];   
    }   

    
/**  
     * 功能:判断栈是否为空  
     * 返回值:boolean 其中true为空,false为不空  
     
*/   
    
public boolean empty() {   
        
return top == -1;   
    }   

    
public void print() {   
        
for (int i = top; i >=0; i--)  { 
            System.out.print(datas[i] 
+ " ");} 
    }
}
posted on 2009-10-18 14:24  Yean  阅读(357)  评论(0编辑  收藏  举报