数据结构 JAVA描述(一) 线性表

一、线性表的抽象数据类型描述

IList

package List;

public interface IList {
   

    public void clear();

    public boolean isEmpty();

    public int length();

    public Object get(int i) throws Exception;

    /**
     * @description 在线性表的第i个元素之前插入一个值为x的数据元素 
     * @param i  0<=i<=length()
     * @param x
     * @throws Exception
     * @time 2015年12月22日 下午9:21:24
     */
    public void insert(int i, Object x) throws Exception;

    public void remove(int i) throws Exception;

    public int indexOf(Object x);

    public void display();
}

二、线性表的顺序存储

顺序表的特点

  • 在线性表中逻辑上相邻的数据元素,在物理存储位置上也是相邻的。
  • 存储密度高,但要预先分配,可能会造成空间的浪费。
  • 便于随机存取
  • 不便于插入和删除操作,会引起大量的数据元素的移动

顺序表类的描述

SqList

package List;

public class SqList implements IList {
   

    private Object[] listElem;  // 线性表存储空间
    private int curLen; // 线性表的当前长度


    /**
     * @description 构造一个存储空间容量为maxSize的线性表
     * @param maxSize
     * @time 2015年12月22日 下午9:50:05
     */ 
    public SqList(int maxSize) {
        curLen = 0;
        listElem = new Object[maxSize];
    }

    public void clear() {
        curLen = 0;
    }

    public boolean isEmpty() {
        return curLen == 0;
    }

    public int length() {
        return curLen;
    }


    /**
     * @description 读取线性表的第i个元素并返回其值
     * @param i 0<=i<=length()
     * @return
     * @throws Exception
     * @time 2015年12月22日 下午9:50:21
     */
    public Object get(int i) throws Exception {
        if (i < 0 || i > curLen - 1)
            throw new Exception("第" + i + "个元素不存在.");
        return listElem[i];
    }

    /**
     * @description 在线性表的第i个元素之前插入一个值为x的数据元素
     * @param i
     * @param x
     * @throws Exception
     * @time 2015年12月22日 下午9:50:34
     */
    public void insert(int i, Object x) throws Exception {
        if (curLen == listElem.length)
            throw new Exception("顺序表已满");
        if (i < 0 || i > curLen)
      
  • 10
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值