java数据结构之栈

  1. 栈接口 
    
        
        
    package  pku.ss.datastructure.IStackLi;   
      
    public   interface  IStackLi  {   
        
    /**  
         * Get the size of the stack  
         * 
    @return size of the stack  
         
    */
      
        
    public int getSize();   
        
    /**  
         * Judge that if the stack is full  
         * 
    @return true or false  
         
    */
      
        
    public boolean isFull();   
        
    /**  
         * Judge that if the stack is empty  
         * 
    @return true or false  
         
    */
      
        
    public boolean isEmpty();   
        
    /**  
         * Set the stack to be empty  
         
    */
      
        
    public void makeEmpty();   
        
    /**  
         * Push a element x into stack, if the operation is right then return true,  
         * else return false  
         * 
    @param x  
         * 
    @return true or false  
         
    */
      
        
    public boolean push(Object x);   
        
    /**  
         * return the top element of the stack  
         * 
    @return the top element  
         
    */
      
        
    public Object top();   
        
    /**  
         * Pop the top element from the stack, if the operation is right, then return   
         * true, else return false  
         * 
    @return true or false  
         
    */
      
        
    public boolean pop();   
           
    }
      


    2,栈的实现
    
        
        
    package  pku.ss.datastructure.StackLi;   
      
    import  pku.ss.datastructure.IStackLi.IStackLi;   
      
    public   class  StackLi  implements  IStackLi  {   
        
    private int maxSize;   
        
    private int size;   
        
    private ListNode topOfStack;   
      
        
    public StackLi(int maxSize) {   
            
    this.maxSize = maxSize;   
            size 
    = 0;   
            topOfStack 
    = null;   
        }
       
      
        @Override  
        
    public int getSize() {   
            
    return this.size;   
        }
       
      
        @Override  
        
    public boolean isEmpty() {   
            
    return size == 0;   
        }
       
      
        @Override  
        
    public boolean isFull() {   
            
    return size > maxSize - 1;   
        }
       
      
        @Override  
        
    public void makeEmpty() {   
            size 
    = 0;   
            topOfStack 
    = null;   
        }
       
      
        @Override  
        
    public boolean pop() {   
            
    if (isEmpty()) {   
                System.out.println(
    "[ERROR] Atempt to pop from a empty stack!");   
                
    return false;   
            }
     else {   
                topOfStack 
    = topOfStack.next;   
                size
    --;   
                
    return true;   
            }
       
        }
       
      
        @Override  
        
    public boolean push(Object x) {   
            
    if (isFull()) {   
                System.out.println(
    "[ERROR] Atempt to push into a full stack!");   
                
    return false;   
            }
     else {   
                ListNode temp 
    = new ListNode(x);   
                temp.next 
    = topOfStack;   
                topOfStack 
    = temp;   
                size
    ++;   
                
    return true;   
            }
       
        }
       
      
        @Override  
        
    public Object top() {   
            
    if (isEmpty()) {   
                System.out.println(
    "[ERROR] Atempt to get the top element from a empty stack!");   
                
    return null;   
            }
       
            
    return topOfStack.element;   
        }
       
      
    }
      


    3,节点类型
    
    
        
        
    package  pku.ss.datastructure.StackLi;   
      
    public   class  ListNode  {   
        ListNode(Object theElement) 
    {   
            
    this(theElement, null);   
        }
       
      
        ListNode(Object theElement, ListNode aNext) 
    {   
            element 
    = theElement;   
            next 
    = aNext;   
        }
       
           
        Object element;  
    //节点中的元素   
        ListNode next;   //指向下一个节点   
    }
      


    4,测试类
    package  pku.ss.datastructure.Demo;   
      
    import  pku.ss.datastructure.StackLi.StackLi;   
      
    public   class  StackLiDemo  {   
      
        
    /**  
         * 
    @param args  
         
    */
      
        
    public static void main(String[] args) {   
            
    // TODO Auto-generated method stub   
            StackLi stack = new StackLi(5);   
            stack.push(
    "A");   
            stack.push(
    "B");   
            stack.push(
    "C");   
            stack.push(
    "D");   
            stack.push(
    "E");   
            stack.push(
    "F");   
            stack.push(
    "G");   
            stack.push(
    "H");   
            System.out.println(
    "**********************");   
      
            System.out.println(
    "The size of the stack is: " + stack.getSize());   
      
            System.out.println(
    "**********************");   
            System.out.println(
    "The top element of the stack is: " + stack.top());   
      
            System.out.println(
    "**********************");   
            stack.makeEmpty();   
            System.out.println(
    "The top element of the stack is: " + stack.top());   
      
            System.out.println(
    "**********************");   
      
            System.out.println(
    "The size of the stack is: " + stack.getSize());   
        }
       
      
    }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值