• 算法之 线性表链式结构


    代码逻辑很多地方可以重用,没有封装。

    链表主要是,根据前一项找后一项。

    代码:

    package math;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Random;
    import java.util.UUID;
    
    /**
     * @author bin
     *
     * @param 链表 主要是必须前项包含后继得内存位置,只有根据前一项才能找到后一项。
     */
    public class SingleLinkList<E> {
        public List l;
        public int currentLen;//当前长度
        public Map m;
        public String address;//
        
        class Obj{
            private String address;
            private Object obj;
            
            public Obj(String str,Object o){
                this.address = str;
                this.obj = o;
            }
            public Obj(){
                
            }
        }
        
        public SingleLinkList(){
            this.currentLen = 0;
            //初始化,内存地址
            this.address = this.getUUid();
            //初始化map
            m = new HashMap();
        }
        
        // 增删改查  整表删除  是否为空   随机表创建 
        public void creatList(int i){
            this.check(i);
            Random r = new Random();
            String tempStr = new String();
            String uuid = new String();
            int rint;
            for (int j = 0; j < i; j++) {
                rint =  r.nextInt(40);
                System.out.println(rint);
                uuid  = this.getUUid();
                Obj o = new Obj(uuid, rint);
                if(j == 0){
                    //当前的记录
                    m.put(address, o);
                }else{
                    m.put(tempStr, o);
                }
                tempStr = uuid;
                currentLen = currentLen+1;
            }
        } 
        //取数据
        public Object getEle(int i){
            this.check(i);
            int start = 0;
            Obj o = new Obj();
            if(currentLen < 1 ){
                throw new RuntimeException("不合理得参数");
            }else if(i > currentLen){
                throw new RuntimeException("越界错误");
            }else{
                while (start <= i-1) {
                    if(start == 0){
                        o = (SingleLinkList<E>.Obj) m.get(address);
                    }else{
                        //System.out.println(m.get(o.address).toString());
                        o = (SingleLinkList<E>.Obj) m.get(o.address);
                    }
                    start++;                                                                     
                }
                return o.obj;
            }
        }
        public void delEle(int i){
            this.check(i);
            int start = 0;
            Obj opre = new Obj();
            Obj onow = new Obj();
            Obj opreer = new Obj();
            if(currentLen < 1 ){
                throw new RuntimeException("不合理得参数");
            }else if(i > currentLen){
                throw new RuntimeException("越界错误");
            }else{
                    while(start <= i-1) {
                        if(start >= 2){
                            //上一项的上一项
                            if(start == 2){
                                opreer = (SingleLinkList<E>.Obj)m.get(address);
                            }else{
                                opreer = (SingleLinkList<E>.Obj)m.get(opreer.address);
                            }
                        }
                        if(start == 0){
                            onow = (SingleLinkList<E>.Obj) m.get(address);
                        }else{
                            opre = onow;
                            onow = (SingleLinkList<E>.Obj) m.get(opre.address);
                            
                        }                    
                        if(start == i-1){
                            if(currentLen == i && i!=1){
                                m.remove(opre.address);
                            }else if(currentLen == i && i==1){
                                m.remove(address);
                            }else{
                                if(start == 1){
                                    String addr = onow.address;
                                    m.remove(opre.address);
                                    opre.address = addr;
                                    m.put(address, opre);
                                }else if(start == 0){
                                    Obj o = new Obj();
                                    o = (SingleLinkList<E>.Obj) m.get(onow.address);
                                    m.remove(address);
                                    m.put(address, o);
                                }else{
                                    String addr = onow.address;
                                    m.remove(opre.address);
                                    opre.address = addr;
                                    m.put(opreer.address, opre);
                                }
                            }
                            this.currentLen = currentLen-1;     
                        }                    
                        start++;
                    }
            }
        }
        public void insert(int i,Object object){
            this.check(i);
            int start = 0;
            Obj opre = new Obj();
            Obj onow = new Obj();
            Obj opreer = new Obj();
            Random r = new Random();
            String uuid ="";
            int rint = 0;
            if(currentLen < 1 ){
                throw new RuntimeException("不合理得参数");
            }else if(i > currentLen+1){
                throw new RuntimeException("越界错误");
            }else{
                    while(start <= i-1) {
                        if(start >= 2){
                            //上一项的上一项
                            if(start == 2){
                                opreer = (SingleLinkList<E>.Obj)m.get(address);
                            }else{
                                opreer = (SingleLinkList<E>.Obj)m.get(opreer.address);
                            }
                        }
                        if(start == 0){
                            onow = (SingleLinkList<E>.Obj) m.get(address);
                        }else{
                            opre = onow;
                            onow = (SingleLinkList<E>.Obj) m.get(opre.address);
                            
                        }                    
                        if(start == i-1){
                            if(currentLen == i && i!=1){
                                m.put(onow.address, object);
                            }else if(currentLen == i && i==1){
                                m.put(address, object);
                            }else{
                                rint =  r.nextInt(40);
                                uuid  = this.getUUid();
                                if(start == 1){
                                    String addr = opre.address;
                                    Obj o = new Obj(addr, object);
                                    m.put(uuid, o);
                                    opre.address = uuid;
                                    m.put(address, opre);
                                }else if(start == 0){
                                    Obj o = new Obj(uuid, object);
                                    m.put(uuid, onow);
                                    m.put(address, o);
                                
                                }else{
                                    String addr = opre.address;
                                    Obj o = new Obj(addr, object);
                                    m.put(uuid, o);
                                    opre.address = uuid;
                                    m.put(opreer.address, opre);
                                }
                            }
                            this.currentLen = currentLen+1;     
                        }                    
                        start++;
                    }
            }
        }
        
        public void update(int i,Object o){
            this.check(i);
            if(i > currentLen){
                throw new RuntimeException("越界错误");
            }
            Obj opre = new Obj();
            Obj onow = new Obj();
            int start = 0;
            while(start <= i-1){
                if(start == 0){
                    onow = (SingleLinkList<E>.Obj) m.get(address);
                }else{
                    opre = onow;
                    onow = (SingleLinkList<E>.Obj) m.get(opre.address);
                }
                
                if(start == i-1){
                    onow.obj = o;
                    m.put(opre.address, onow);
                }
                start++;
            }
        }
        
        public void check(int i){
            if(i<=0){
                throw new RuntimeException("不合法得参数");
            } 
        }
        public static void main(String[] args) {
            SingleLinkList sing = new SingleLinkList();
            sing.creatList(5);
            sing.delEle(3);
            sing.insert(3, 12);
            System.out.println(sing.currentLen+"--");
            for (int i = 1; i <=sing.currentLen; i++) {
                System.out.println(sing.getEle(i));
            }
        }
        //生成唯一得地址信息
        public String getUUid(){
            UUID uuid = UUID.randomUUID();
            return uuid.toString();
        }
    }
    积累知识,分享知识,学习知识。
  • 相关阅读:
    责任链模式(Chain of Responsibility)
    模板模式(Template Method)
    组合模式(Composite Pattern)
    原型模式(Prototype Pattern)
    策略模式(Strategy Pattern)
    状态模式(State Pattern)
    增删改查
    安卓sql
    安卓第三次作业
    安卓第四周作业
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4182164.html
Copyright © 2020-2023  润新知