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);
    	}
    	
    }
    

      

    积累知识,分享知识,学习知识。
  • 相关阅读:
    6、查看历史记录
    A Tour of Go Range
    Go Slices: usage and internals
    A Tour of Go Nil slices
    A Tour of Go Making slices
    A Tour of Go Slicing slices
    A Tour of Go Slices
    A Tour of Go Arrays
    A Tour of Go The new function
    A Tour of Go Struct Literals
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4326062.html
Copyright © 2011-2022 走看看