zoukankan      html  css  js  c++  java
  • 算法之 线性表顺序结构

    package math;
    
    import java.util.ArrayList;
    import java.util.List;
    
    //线性表顺序结构
    public class LinearTable {
        public int len = 0; //线性表表长度
        public List list;
        public int currentLen = 0;
        //构造方法,申请一个对应长度得 线性表
        public LinearTable(int i){
            this.list = new ArrayList(i);
            this.len = i;
        }
        
        //是否微空
        public boolean listEmpty(){
            if(this.getElem(0) == null){
                return true;
            }else{
                return false;
            }
        }
        //清空数组
        public void clearList(){
            for (int i = 0; i < len; i++) {
                if(list.get(i) != null){
                    list.remove(i);
                }else{
                    break;
                }
            }
            
        }
        //增删改查
        //在某个位置 增加一个
        public void listInsert(int i,Object obj){
            if(this.check(i) && obj != null){
                //当线性表长度 等于数据长度时候
                if(currentLen == len){
                    throw new RuntimeException("线性表已满");
                }
                //从最后一项开始 让前一项等于后一项   一直到J = i-1 也就是 j>i
                if(i >= currentLen+1){
                    //判断是否插入 插入最后一项 
                    list.add(obj);
                    System.out.println("插入在当前线性表最后面");
                }else{
                    Object temp = new Object();
                    list.add(list.get(currentLen-1));
                    for (int j = currentLen-1; j >=i-1; j--) {
                        temp = list.get(j-1);
                        list.set(j,temp);
                        // 后一项 等于前一项
                        if(j == i-1){
                            list.set(j, obj);
                        } 
                    }
                }
                currentLen = currentLen+1;//长度加一
            }else{
                throw new RuntimeException("listInsert 第二个参数不能为空");
            }
        }
        public void listDelete(int i){
            this.check(i);
            //验证是否微空表
            if(this.listEmpty()){
                throw new RuntimeException("当前线性表为空,不允许删除操作");
            }
            if(i > currentLen ){
                //判断是否为没有的下标
                throw new RuntimeException("下标不存在");
            }else{
                Object temp = new Object();
                for (int j = i-1; j < currentLen-1; j++) {
                    temp = list.get(j+1);
                    // 后一项 等于当前项
                    System.out.println(temp);
                    list.set(j, temp);
                }
                //每次删除最后一项
                list.remove(currentLen-1);
                currentLen = currentLen-1;//长度减一
            }
            
        }
        public void listUpdate(int i,Object obj){
            this.check(i);
            //判断与当前长度得关系
            if(i>currentLen){
                throw new RuntimeException("无效得数组下标");
            }else{
                list.set(i-1, obj);
            }
        }
        public Object getElem(int i){
    
            this.check(i);
            //判断与当前长度得关系
            if(i>currentLen){
                throw new RuntimeException("无效的组下标");
            }else{
                return list.get(i);
            }
        }
        public int listLength(){
            return currentLen;
        }
        
        public boolean check(int i){
            // 大于0项  
            if(i <= len && i >= 0 ){
                return true;
            }else{
                throw new RuntimeException("无效的下标值");
            }
        }
    }
    积累知识,分享知识,学习知识。
  • 相关阅读:
    ios UIWebView截获html并修改便签内容(转载)
    IOS获取系统时间 NSDate
    ios 把毫秒值转换成日期 NSDate
    iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用
    IOS开发 xcode报错之has been modified since the precompiled header was built
    iOS系统下 的手机屏幕尺寸 分辨率 及系统版本 总结
    iOS 切图使用 分辨率 使用 相关总结
    整合最优雅SSM框架:SpringMVC + Spring + MyBatis 基础
    Java面试之PO,VO,TO,QO,BO
    Notes模板说明
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4150033.html
Copyright © 2011-2022 走看看