数据结构之栈(java版)

本文力求简洁,只包含基础的栈功能,不想将大片的代码展示出来,让读者兴趣索然,阅读起来也十分费力,如有需要可以自行添加相关功能比如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<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());      
    }                                           
}   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值