自学 数据结构(三)物理结构

物理结构

1. 顺序存储结构:

定义:一段连续的内存空间。
优点:随机访问
缺点:插入删除效率低,大小固定

package order_list;
//顺序存储结构
public class SqList {
    static final int DEFAULT_SIZE = 10;
    private Object array[];
    private int capacity;//顺序表的容量
    private int length;//顺序表的当前长度

    //    构造方法 初始化线性表
    public SqList() {
        this(DEFAULT_SIZE);//调用构造方法
    }

    public SqList(int capacity) {
        array = new Object[capacity];
        this.capacity = capacity;
        this.length = 0;
    }

    //    销毁顺序表
    public void destroy() {
        array = null;
        capacity = 0;
        length = 0;
    }

    //    将顺序表置空
    public void clear() {
        for (int i = 0; i < length; i++) {
            array[i] = null;
        }
        length = 0;
    }

    //    判断顺序表是否为空
    public boolean isEmpty() {
        return length == 0;
    }

    //    返回顺序表中元素的个数
    public int getLength() {
        return length;
    }

    //    返回线性表中的第i个元素
    public Object getElement(int i) throws Exception {
        if (i < 0 || i >= length) {
            throw new Exception("要获取的元素下标已越界");
        } else {
            return array[i];
        }
    }

    //元素判定方法,返回顺序表中第一个与elem 相等的元素的下标.若不存在则返回-1
    public int getLocate(Object object) {
        int locate = 0;
        while (locate <= length && !array[locate].equals(object)) {
            locate++;
        }
        if (locate >= length) {
            locate = -1;
        }
        return locate;
    }

    // 若object是数据表中的元素,且不是第一个,则返回它的前驱,否则返回空
    public Object priorElem(Object object) {
        int index = getLocate(object);
        if (index > 0 && index < length) {
            return array[index - 1];
        } else {
            return null;
        }
    }

    //    若object是数据表中的元素,且不是最后一个,则返回它的后继,否则返回空
    public Object nextElem(Object object) {
        int index = getLocate(object);
        if (index >= 0 && index < length - 1) {
            return array[index + 1];
        } else {
            return null;
        }
    }

    //在表中第index个位置之前插入新元素object,顺序表的长度加一
    public void listInsert(int index, Object object) throws Exception {
        if (index < 0) {
            throw new Exception("插入元素的下标越界插入失败");
        } else if (length+1 >capacity) {
            throw new Exception("顺序表已满,插入失败");
        } else if (index >= length) {
            index = length;
        }
        //将下标为index到length-1的元素全部后移一位
        for (int i = length - 1; i >= index-1; i--) {
            array[i + 1] = array[i];
        }
        array[index-1] = object;
        length++;
    }

    //删除指定位置index前一个元素
    public void listDelete(int index) throws Exception {
        if (index < 0 || index > length) {
            throw new Exception("要删除元素的下标越界删除失败");
        }
        //将下标为index到length-1的元素全部前移一位
        for (int i = index - 1; i <length-1 ; i++) {
            array[i] = array[i+1];
        }
        array[length-1]=null;
        length--;
    }

    //    打印整个顺序表
    public void display() {
        for (int i = 0; i < length; i++) {
            System.out.println(array[i] + "  ");
        }
    }

    //   添加元素
    public void add(Object object) throws Exception {
        if (capacity > length) {
            array[length] = object;
            length++;
        } else {
            throw new Exception("顺序表已满");
        }
    }
}
2. 链式存储结构:

定义:不连续的内存空间
优点:大小动态扩展,插入删除效率高
缺点:不能随机访问。
代码实例:待上传 要是等不及就找找度娘

3. 索引存储结构:

定义:为了方便查找,整体无序,但索引块之间有序,需要额外空间,存储索引表。
优点:对顺序查找的一种改进,查找效率高
缺点:需额外空间存储索引
代码实例:待上传 要是等不及就找找度娘

4. 散列存储结构:

定义:选取某个函数,数据元素根据函数计算存储位置可能存在多个数据元素存储在同一位置,引起地址冲
优点:查找基于数据本身即可找到,查找效率高,存取效率高。
缺点:存取随机,不便于顺序查找。
代码实例:待上传 要是等不及就找找度娘

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值