资讯专栏INFORMATION COLUMN

数据结构之栈(java版)

hizengzeng / 1452人阅读

摘要:本文力求简洁,只包含基础的栈功能,不想将大片的代码展示出来,让读者兴趣索然,阅读起来也十分费力,如有需要可以自行添加相关功能比如包中的类包含的,等等函数能力有限,有误之处还请不吝赐教定义内部类用于存储栈元素指向下一个栈元素的泛型元素方法方法

本文力求简洁,只包含基础的栈功能,不想将大片的代码展示出来,让读者兴趣索然,阅读起来也十分费力,如有需要可以自行添加相关功能比如java.util.Stack包中的Stack类包含的peek()empty()等等函数.
能力有限,有误之处还请不吝赐教

定义内部类用于存储栈元素
    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基础的应该都能够看懂

整体代码
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());      
    }                                           
}   
更多关于java的文章请戳这里:(您的留言意见是对我最大的支持)

我的文章列表

Email:sxh13208803520@gmail.com

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66963.html

相关文章

  • 源码|jdk源码之栈、队列及ArrayDeque分析

    摘要:栈队列双端队列都是非常经典的数据结构。结合了栈和队列的特点。因此,在中,有栈的使用需求时,使用代替。迭代器之前源码源码之与字段中分析过,容器的实现中,所有修改过容器结构的操作都需要修改字段。 栈、队列、双端队列都是非常经典的数据结构。和链表、数组不同,这三种数据结构的抽象层次更高。它只描述了数据结构有哪些行为,而并不关心数据结构内部用何种思路、方式去组织。本篇博文重点关注这三种数据结构...

    ZHAO_ 评论0 收藏0
  • 利用PHP实现常用的数据结构之栈(小白系列文章四)

    摘要:堆栈算法引子栈是计算机术语中比较重要的概念,实质上栈就是一段内存区域,但是栈满足一定的特性,那就是只有一个口,具有先入后出的特性,这种特性在计算机中有很广泛的运用。 /** * PHP堆栈算法 * Created on 2017-4-27 * Author entner * Email 1185087164@qq.com */ 引子     栈...

    array_huang 评论0 收藏0
  • 利用PHP实现常用的数据结构之栈(小白系列文章四)

    摘要:堆栈算法引子栈是计算机术语中比较重要的概念,实质上栈就是一段内存区域,但是栈满足一定的特性,那就是只有一个口,具有先入后出的特性,这种特性在计算机中有很广泛的运用。 /** * PHP堆栈算法 * Created on 2017-4-27 * Author entner * Email 1185087164@qq.com */ 引子     栈...

    yankeys 评论0 收藏0
  • js数据结构之栈

    摘要:向一个栈插入新元素又称作进栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素从一个栈删除元素又称作出栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。 栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素...

    LeviDing 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<