數據結構之棧(java版)

本文力求簡潔,只包含基礎的棧功能,不想將大片的代碼展現出來,讓讀者興趣索然,閱讀起來也十分費力,若有須要能夠自行添加相關功能好比java.util.Stack包中的Stack類包含的peek()empty()等等函數.
能力有限,有誤之處還請不吝賜教java

定義內部類用於存儲棧元素

class Node {                                
        private Node below;   //指向下一個棧元素的reference                  
        private T type;           //泛型元素                                             
        public Node(Node below, T type) {       
            this.below = below;                 
            this.type = type;                   
        }                                       
    }

Push()方法

public void push(T element) {               
        if (top == null) {                      
            Node node = new Node(null, element);
            this.top = node;                    
            this.base = node;                   
            length++;                           
        } else {                                
            Node node = new Node(top, element); 
            this.top = node;                    
            length++;                           
        }                                       
    }

pop()方法

public T pop() {                            
        if (top != null) {                      
            Node temp = top;                    
            if (temp.below != null) {           
                top = temp.below;               
                length--;                       
            } else {                            
                this.base=null;                 
                this.top=null;                  
                length=0;                       
            }                                   
            return temp.type;                   
        } else return null;                     
    }                                                                                
    public int getLength() {                    
        return length;                          
    }

總體代碼比較簡單,這裏再也不贅述,有必定java基礎的應該都可以看懂node

總體代碼

public class MyStack<T> {                       
    private Node base;                          
    private Node top;                           
    private int length = 0;                     
    class Node {                                
        private Node below;                     
        private T type;                                                        
        public Node(Node below, T type) {       
            this.below = below;                 
            this.type = type;                   
        }                                       
    }                                           
    public void push(T element) {               
        if (top == null) {                      
            Node node = new Node(null, element);
            this.top = node;                    
            this.base = node;                   
            length++;                           
        } else {                                
            Node node = new Node(top, element); 
            this.top = node;                    
            length++;                           
        }                                       
    }                                           
    public boolean isEmpty(){                   
        if(base==null){                         
            return true;                        
        }else return false;                     
    }                                           
    public T pop() {                            
        if (top != null) {                      
            Node temp = top;                    
            if (temp.below != null) {           
                top = temp.below;               
                length--;                       
            } else {                            
                this.base=null;                 
                this.top=null;                  
                length=0;                       
            }                                   
            return temp.type;                   
        } else return null;                     
    }                                                                                
    public int getLength() {                    
        return length;                          
    }                                                                                        
    @Override                                   
    public String toString() {                  
        StringBuffer sb = new StringBuffer();   
        Node current = top;                     
        while (current != null) {               
            sb = sb.append("/"+current.type);   
            current = current.below;            
        }                                       
        return sb.toString();                   
    }                                           
    public static void main(String[] args) {    
        MyStack<String> ms=new MyStack<>();     
        System.out.println(ms.getLength());     
        ms.push("value1");                      
        ms.push("value2");                      
        ms.push("value3");                      
        System.out.println(ms.getLength());     
        System.out.println(ms.pop());           
        System.out.println(ms.pop());           
        System.out.println(ms.getLength());     
        System.out.println(ms.toString());      
    }                                           
}
更多關於java的文章請戳這裏:(您的留言意見是對我最大的支持)

個人文章列表app

Email:sxh13208803520@gmail.comide

相關文章
相關標籤/搜索