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("无效的下标值");
            }
        }
    }
    积累知识,分享知识,学习知识。
  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4150033.html
Copyright © 2011-2022 走看看