zoukankan      html  css  js  c++  java
  • 算法之 栈的顺序结构

    package bin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.omg.CORBA.SystemException;
    
    /**
     * @author bin
     * target 实现栈的顺序结构
     *	摘要:栈  栈顶  栈底 LIFO 结构  last In First Out 
     */
    
    
    public class Bin_stack {
    
    	private int StackSize;
    	private int current;
    	private List list;
    	
    	public int getStackSize() {
    		return StackSize;
    	}
    
    	public void setStackSize(int stackSize) {
    		StackSize = stackSize;
    	}
    
    	public int getCurrent() {
    		return current;
    	}
    
    	public void setCurrent(int current) {
    		this.current = current;
    	}
    
    	public List getList() {
    		return list;
    	}
    
    	public void setList(List list) {
    		this.list = list;
    	}
    	
    	
    	public Bin_stack(int size){
    		this.setList(new ArrayList<>());
    		this.setCurrent(0);
    		this.setStackSize(size);
    	}
    	
    	public void push(Object o){
    		if(this.current == this.StackSize){
    			 throw new RuntimeException("栈满");
    		}else{
    			list.add(o);
    			current = ++current; 
    		}
    	}
    	
    	public Object pop(){
    		if(current == 0){
    			throw new RuntimeException("空栈");
    		}
    		Object o = list.get(current-1);
    		list.remove(current-1);
    		current = --current;
    		return o;
    	
    	}
    	
    	public static void main(String[] args) {
    		Bin_stack b = new Bin_stack(4);
    		b.push(123);
    		b.push(43);
    		System.out.println(b.pop());
    		System.out.println(b.pop());
    		b.push(4312);
    	}
    	
    }
    

      

    积累知识,分享知识,学习知识。
  • 相关阅读:
    滑动窗口法学习
    209. Minimum Size Subarray Sum
    485. Max Consecutive Ones
    27. Remove Element
    167. Two Sum II
    561. Array Partition I
    344. Reverse String
    14. 最长公共前缀
    layui上传文件时出现 请求上传接口出错
    Linux-5.13将初步支持苹果M1 Soc
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4326062.html
Copyright © 2011-2022 走看看