日撸java 三百行 (day 05 )顺序表(二)

今天在成功建立顺序表的基础上实现顺序表的基本操作:增、删、改、查

  • 增操作:在给定位置增加元素. 如果线性表已满, 或位置不在已有位置范围之内, 就拒绝增加. 该位置可以是在最后一个元素之后一个.
  /**
     *********************
     * Insert a value to a position. If the list is already full, do nothing.
     *
     * @param paraPosition The given position.
     * @param paraValue    The given value.
     * @return Success or not.
     *********************
     */

    public boolean insert(int paraPosition,int paraValue){
        if(length==MAX){
            System.out.println("线性表已满,插入失败");
            return false;
        }// of if
        if(paraPosition<0||paraPosition>length){
            System.out.println("插入位置不合法,插入失败");
            return false;
        } //of if
        for(int i=length;i>paraPosition;--i){
            data[i]=data[i-1];
        } //of for
        data[paraPosition]=paraValue;
        length++;
        return true;
    }// of insert

增操作首先判断顺序表是否已满,若已满则不能添加元素。之后需要判断插入的位置是否合法,如果合法,则从顺序表的尾部开始直到插入点的所有元素全部往后移动一位,最后将类中的length属性加一。

  • 删除操作:删除给定位置的元素. 要处理给定位置不合法的情况. 该位置必须是已经有数据的.
/**
     *********************
     * Delete a value at a position.
     *
     * @param paraPosition The given position.
     * @return Success or not.
     *********************
     */
    public boolean delete(int paraPosition){
        if(paraPosition<0||paraPosition>length-1){
            System.out.println("删除位置不合法,删除失败");
            return false;
        } //of if
        for(int i=paraPosition;i<length-1;++i){
            data[i]=data[i+1];
        } // of for
        length--;
        return true;
    } //of delete

删除操作需要判断删除的位置是否合法,如果合法,则需要将删除点以后的元素全部前移一位,最后把类中的length属性减一。

  • 修改操作:修改给定位置的元素。要处理给定位置不合法的情况. 该位置必须是已经有数据的.
/**
     *********************
     * Alter a value at a position.
     *
     * @param paraPosition The given position.
     * @param paraValue    The given value.    
     * @return Success or not.
     *********************
     */
    public boolean alter(int paraPosition,int paraValue){
        if(paraPosition<0||paraPosition>length-1){
            System.out.println("修改元素位置不合法,修改失败");
            return false;
        } // of if

        data[paraPosition]=paraValue;
        return true;
    } // of alert
  • 查询操作:查找给定元素所处的位置. 找不到就返回 -1.
 /**
     *********************
     * Find the index of the given value. If it appears in multiple positions,
     * simply return the first one.
     *
     * @param paraValue The given value.
     * @return The position. -1 for not found.
     *********************
     */
    public int  indexOf(int paraValue){
        int i=0,tempPosution=-1;
        while(data[i]!=paraValue&&i<length){
            ++i;
        } // of while

        if(i<length){
            tempPosution=i;
            return tempPosution;
        } //of if
        return tempPosution;

    } // of indexOf
  • 测试:
/**
     *********************
     * The entrance of the program.
     *
     * @param args Not used now.
     *********************
     */
    public static void main(String args[]) {
        int[] tempArray = { 1, 4, 6, 9 };
        SequentialList tempFirstList = new SequentialList(tempArray);
        System.out.println("初始化的顺序表为: " + tempFirstList.toString());
        System.out.println("当前的顺序表为: " + tempFirstList);

        int tempValue = 4;
        int tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("位置为 " + tempValue + " 的元素是 " + tempPosition);

        tempValue = 5;
        tempPosition = tempFirstList.indexOf(tempValue);
        System.out.println("位置为 " + tempValue + " 的元素是 " + tempPosition);

        tempPosition = 2;
        tempValue = 5;
        tempFirstList.insert(tempPosition, tempValue);
        System.out.println(
                "把元素 " + tempValue + "插入到位置 " + tempPosition + "后" + ", 顺序表的元素为: " + tempFirstList);

        tempPosition = 8;
        tempValue = 10;
        tempFirstList.insert(tempPosition, tempValue);
        System.out.println(
                "把元素 " + tempValue + "插入到位置 " + tempPosition + "后" + ", 顺序表的元素为: " + tempFirstList);

        tempPosition = 3;
        tempFirstList.delete(tempPosition);
        System.out.println("删除" + tempPosition +"号位置上的元素后" +", 顺序表为: " + tempFirstList);

        tempPosition = 2;
        tempValue = 8;
        tempFirstList.alter(tempPosition,tempValue);
        System.out.println("更改" + tempPosition +"号位置上的元素为"+tempValue+"后" +", 顺序表为: " + tempFirstList);

        tempFirstList.reset();
        System.out.println("重置顺序表后: " + tempFirstList);
    }}// Of main

运行效果:

总结:顺序表的基本操作相对链表来说还是比较好实现,但作为代价,顺序表的最大长度是固定的,如果分配的空间过多 会造成浪费;分配过少 顺序表的扩容代价又很大。顺序表每次删除和增加元素后都得把 length 加 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值