成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

數(shù)據(jù)結(jié)構(gòu)之棧(java版)

hizengzeng / 1643人閱讀

摘要:本文力求簡潔,只包含基礎(chǔ)的棧功能,不想將大片的代碼展示出來,讓讀者興趣索然,閱讀起來也十分費力,如有需要可以自行添加相關(guān)功能比如包中的類包含的,等等函數(shù)能力有限,有誤之處還請不吝賜教定義內(nèi)部類用于存儲棧元素指向下一個棧元素的泛型元素方法方法

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

定義內(nèi)部類用于存儲棧元素
    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基礎(chǔ)的應(yīng)該都能夠看懂

整體代碼
public class MyStack {                       
    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 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());      
    }                                           
}   
更多關(guān)于java的文章請戳這里:(您的留言意見是對我最大的支持)

我的文章列表

Email:sxh13208803520@gmail.com

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66963.html

相關(guān)文章

  • 源碼|jdk源碼之棧、隊列及ArrayDeque分析

    摘要:棧隊列雙端隊列都是非常經(jīng)典的數(shù)據(jù)結(jié)構(gòu)。結(jié)合了棧和隊列的特點。因此,在中,有棧的使用需求時,使用代替。迭代器之前源碼源碼之與字段中分析過,容器的實現(xiàn)中,所有修改過容器結(jié)構(gòu)的操作都需要修改字段。 棧、隊列、雙端隊列都是非常經(jīng)典的數(shù)據(jù)結(jié)構(gòu)。和鏈表、數(shù)組不同,這三種數(shù)據(jù)結(jié)構(gòu)的抽象層次更高。它只描述了數(shù)據(jù)結(jié)構(gòu)有哪些行為,而并不關(guān)心數(shù)據(jù)結(jié)構(gòu)內(nèi)部用何種思路、方式去組織。本篇博文重點關(guān)注這三種數(shù)據(jù)結(jié)構(gòu)...

    ZHAO_ 評論0 收藏0
  • 利用PHP實現(xiàn)常用的數(shù)據(jù)結(jié)構(gòu)之棧(小白系列文章四)

    摘要:堆棧算法引子棧是計算機術(shù)語中比較重要的概念,實質(zhì)上棧就是一段內(nèi)存區(qū)域,但是棧滿足一定的特性,那就是只有一個口,具有先入后出的特性,這種特性在計算機中有很廣泛的運用。 /** * PHP堆棧算法 * Created on 2017-4-27 * Author entner * Email 1185087164@qq.com */ 引子 ????棧...

    array_huang 評論0 收藏0
  • 利用PHP實現(xiàn)常用的數(shù)據(jù)結(jié)構(gòu)之棧(小白系列文章四)

    摘要:堆棧算法引子棧是計算機術(shù)語中比較重要的概念,實質(zhì)上棧就是一段內(nèi)存區(qū)域,但是棧滿足一定的特性,那就是只有一個口,具有先入后出的特性,這種特性在計算機中有很廣泛的運用。 /** * PHP堆棧算法 * Created on 2017-4-27 * Author entner * Email 1185087164@qq.com */ 引子 ????棧...

    yankeys 評論0 收藏0
  • js數(shù)據(jù)結(jié)構(gòu)之棧

    摘要:向一個棧插入新元素又稱作進棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素從一個棧刪除元素又稱作出棧,它是把棧頂元素刪除掉,使其相鄰的元素成為新的棧頂元素。 棧(stack)又名堆棧,它是一種運算受限的線性表。其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素...

    LeviDing 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<