数据结构学习daytwo(一)

* 今天学习的是线性表中的顺序表。

* dayTwo:线性结构 * 线性结构特点:在数据元素的非空有限集中:

* 1)存在唯一的一个被称作“第一个”的数据元素。

* 2)存在唯一一个被称作“最后一个”的数据元素。

* 3)除第一个之外,集合中的每个数据元素均只有一个前驱。

* 4)除最后一个之外,集合中每个数据元素均只有一个后继。

 * dayTwo:(一) * 线性表类型定义:一个线性表是n个相同数据类型数据元素的有限序列。

* 首先是线性表的顺序表示

* 指用一组地址连续的存储单元依次存储线性表的数据元素。

* 只要确定了存储线性表的起始位置,线性表中任一数据元素都可随机存取,所以线性表的顺序存储结构是一种随机存取的存储结构。

* 以下为本人写的java版顺序表的实现(若有错误请各位大佬指点)。

**
 * 顺序表
 */
class SqList {
    private final int LIST_INIT_SIZE;
    private int listincrement;
    private int length;
    private Object[] sqList;

    /**
     * 默认生成长度为100的顺序表
     */
    public SqList() {
        this.length = 0;
        this.LIST_INIT_SIZE = 100;
        listincrement = 10;
        this.sqList = new Object[LIST_INIT_SIZE];
    }

    /**
     * 根据len动态创建顺序表
     *
     * @param len 生成长度为len的顺序表
     */
    public SqList(int len) {
        this.length = 0;
        this.LIST_INIT_SIZE = len;
        listincrement = 10;
        this.sqList = new Object[LIST_INIT_SIZE];
    }

    /**
     * 顺序存入数据
     */
    public void add(Object object) {
        if (this.isFull()) {
            this.increase();
        }
        this.sqList[length++] = object;
    }

    /**
     * 扩充顺序表总长度
     */
    private void increase() {
        Object[] dataList = this.sqList;
        this.sqList = new Object[LIST_INIT_SIZE + listincrement];
        this.listincrement += this.listincrement;
        for (int i = 0; i < dataList.length; i++) {
            this.sqList[i] = dataList[i];
        }
    }

    /**
     * 判断线性表是否已满
     *
     * @return true为线性表已满,false为线性表未满
     */
    private boolean isFull() {
        return length >= LIST_INIT_SIZE;
    }

    /**
     * 在指定位置存入数据
     */
    public void add(int place, Object object) {
        if (this.isFull()) {
            this.increase();
        }
        if (place == this.length) {
            this.add(object);
        } else if (place > length || place < 0) {
            System.out.println("错误的插入位置");
        } else {
            for (int i = 0; i < length; i++) {
                if (i == place - 1) {
                    for (int j = length; j >= i; j--) {
                        this.sqList[j + 1] = this.sqList[j];
                    }
                    this.sqList[i] = object;
                }
            }
            this.length++;
        }
    }

    /**
     * 根据位置查询
     *
     * @param place 所查数据所在的位置
     * @return 查出的数据元素
     */
    public Object select(int place) {
        if (place > length || place < 0) {
            System.out.println("输入位置有误");
            return null;
        }
        return this.sqList[place];
    }

    /**
     * 更新顺序表中的数据
     *
     * @param place  要更新的位置
     * @param object 要更新的数据元素
     */
    public void update(int place, Object object) {
        if (place > length || place < 0) {
            System.out.println("输入位置有误");
            return;
        }
        this.sqList[place - 1] = object;
    }

    /**
     * 删除指定位置的数据元素
     *
     * @param place 要删除数据元素的位置
     */
    public void remove(int place) {
        if (place > length || place < 0) {
            System.out.println("输入位置有误");
            return;
        }
        this.sqList[place - 1] = null;
        for (int i = place - 1; i < length; i++) {
            this.sqList[i] = this.sqList[i + 1];
        }
        this.length--;
    }

    /**
     * 清空顺序表
     */
    public void clear() {
        this.sqList = null;
        this.length = 0;
    }

    /**
     * 显示顺序表长度
     *
     * @return返回顺序表长度
     */
    public int size() {
        return this.length;
    }

}

转载于:https://my.oschina.net/u/3970508/blog/3027066

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值