码迷,mamicode.com
首页 > 编程语言 > 详细

算法之 线性表顺序结构

时间:2014-12-07 23:06:52      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   java   on   

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{
                System.out.println("--");
                Object temp = new Object();
                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; j++) {
                temp = list.get(j+1);
                // 当前项 等于后一项
                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("无效的下标值");
        }
    }
}

 

算法之 线性表顺序结构

标签:style   blog   io   ar   color   sp   for   java   on   

原文地址:http://www.cnblogs.com/bin-pureLife/p/4150033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!