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());   
        }
       
      
    }
      
發佈了47 篇原創文章 · 獲贊 4 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章