zoukankan      html  css  js  c++  java
  • 算法之 栈内存共享空间

    package bin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.management.RuntimeErrorException;
    
    /**
     * @author bin 15/3/10
     * target  共享栈空间
     *
     */
    public class Bin_stack_share {
        private int top1;//栈一的栈顶记录
        private int top2;//栈二的栈顶记录
        private Object obj[];//存储容器
        private int type;
        private int size;//栈长度
        
        
        
        public int getTop1() {
            return top1;
        }
    
    
    
        public void setTop1(int top1) {
            this.top1 = top1;
        }
    
    
    
        public int getTop2() {
            return top2;
        }
    
    
    
        public void setTop2(int top2) {
            this.top2 = top2;
        }
    
    
    
        public Object[] getObj() {
            return obj;
        }
    
    
    
        public void setObj(Object[] obj) {
            this.obj = obj;
        }
    
    
        public int getSize() {
            return size;
        }
    
    
    
        public void setSize(int size) {
            this.size = size;
        }
    
    
        //构造方法 初始化栈长度 
        public Bin_stack_share(int size){
            this.setTop1(-1);
            this.setTop2(size);
            this.setSize(size);
            this.setObj(new Object[size]);
        }
         
            
        //进栈方法
        public void push(Object val,int type){
            if(top1+1 == top2){
                throw new RuntimeException("栈满");
            } 
            //如果type 为1 则入栈一    else 为栈二
            if(type == 1){
                obj[top1+1] = val;
                top1 = ++top1;
            }else{
                obj[top2-1] = val;
                top2 = --top2;
            }
        }
        public Object pop(int type){
            
            if(top1 == -1 && top2 == size){
                throw new RuntimeException("空栈");
            } 
            Object temp = new Object();
            if(type == 1){
                //当TYPE等于1的时候 我们弹出栈1的栈顶
                temp = obj[top1];
                obj[top1] = null;
                top1 = --top1;
            }else{
                temp = obj[top2];
                obj[top2] = null;
                top2 = ++top2;
            }
            return temp;
        }
        
        public static void main(String[] args) {
            
            Bin_stack_share b = new Bin_stack_share(2);
            b.pop(2);
            
            System.out.println(b.top1);
            
        }
        
    }
     

    场景:这样的数据结构使用场景在哪儿呢?

            量是一定的,分两边,一边多+另一边等于总量。

            比如投票: A B 两位选手,一共三十个人投票,只允许投这俩人,所以非A即B。

            如果按常规的,开两个栈,如果最极端的情况,一边没有,一边满栈,就浪费了一倍的内存。

    积累知识,分享知识,学习知识。
  • 相关阅读:
    关于Maya Viewport 2.0 API 开发的介绍视频
    春节大假
    Some tips about the life cycle of Maya thread pool
    Can I compile and run Dx11Shader for Maya 2015 on my side?
    How to get current deformed vertex positions in MoBu?
    想加入全球首届的 欧特克云加速计划吗?
    三本毕业(非科班),四次阿里巴巴面试,终拿 offer(大厂面经)
    mac、window版编辑器 webstorm 2016... 永久破解方法。
    node 搭载本地代理,处理web本地开发跨域问题
    js 一维数组,转成嵌套数组
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4326633.html
Copyright © 2011-2022 走看看