数据结构-顺序存储线性表

 

import java.util.Arrays;

public class SequenceList<T> {

    //数组默认长度
    private int DEFAULT_SIZE = 16;
    // 数组容量
    private int capacity;
    //定义一个数组保存顺序线性表的元素
    private Object[] elementData;
    //保存顺序表中元素的当前个数
    private int size = 0;

    //使用默认数组长度初始化空顺序线性表
    public SequenceList() {
        capacity = DEFAULT_SIZE;
        elementData = new Object[capacity];
    }

    //以一个初始化元素创建顺序线性表
    public SequenceList(T element) {
        this();
        elementData[0] = element;
        size++;
    }

    /**
     * 以指定长度的数组还创建顺序线性表
     *
     * @param element
     * @param initSize
     */
    public SequenceList(T element, int initSize) {
        /**
         *  二进制
         *  0 0 0 0 0 0 0 1     1
         *  0 0 0 0 0 0 1 0     2
         *  0 0 0 0 0 0 1 1     3
         *  0 0 0 0 0 1 0 0     4
         *  0 0 0 0 0 1 0 1     5
         *  0 0 0 0 0 1 1 0     6
         *  0 0 0 0 0 1 1 1     7
         *  0 0 0 0 1 0 0 0     8
         *  0 0 0 0 1 0 0 1     9
         *  0 0 0 0 1 0 1 0     10
         *  0 0 0 0 1 0 1 1     11
         *  0 0 0 0 1 1 0 0     12
         *  0 0 0 0 1 1 0 1     13
         *  0 0 0 0 1 1 1 0     14
         *  0 0 0 0 1 1 1 1     15
         *  例如 initSize 为 10 则下面循环
         *  0 0 0 0 0 0 1 0   2<10
         *  0 0 0 0 0 1 0 0   4<10
         *  0 0 0 0 1 0 0 0   8<10
         *  0 0 0 1 0 0 0 0   16>10
         */
        //将capacity设为大于initSize的最小的2的n次方
        capacity = 1;
        while (capacity < initSize) {
            capacity <<= 1;
        }
        elementData = new Object[capacity];
        elementData[0] = element;
        size++;
    }

    //获取顺序线性表的大小
    public int length() {
        return size;
    }

    //获取顺序线性表中索引为 i 处的元素
    public T get(int i) {
        if (i < 0 || i > size - 1) {
            throw new IndexOutOfBoundsException("线性表索引越界");
        }
        return (T) elementData[i];
    }

    //查找顺序线性表中指定元素的索引
    public int locate(T element) {
        for (int i = 0; i < size; i++) {
            if (elementData[i].equals(element)) {
                return i;
            }
        }
        return -1;
    }

    //向顺序线性表的指定位置插入一个元素
    public void insert(T element, int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("线性表索引越界");
        }
        ensureCapacity(size + 1);
        //从索引指定位置复制元素到新的数组
        /**
         * int示例:数组长度为10,index为3的位置插入1
         *  默认数组: 2 1 3 4 5 0 0 0 0 0
         *  arraycopy执行后为:2 1 3 4 4 5 0 0 0 0
         */
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = element;
        size++;
    }

    //在线性顺序表的开始处添加一个元素
    public void add(T element) {
        insert(element, size);
    }

    //数组动态扩容
    private void ensureCapacity(int minCapacity) {
        //如果数组的原有长度小于目前所需的长度
        if (minCapacity > capacity) {

            while (capacity < minCapacity) {
                capacity <<= 1;
            }
            elementData = Arrays.copyOf(elementData, capacity);
        }
    }

    //删除顺序线性表中指定索引处的元素
    public T delete(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException("线性表索引越界");
        }
        /**
         * int示例:数组占用长度为6 删除索引为2的元素
         *  此时size为 6;索引为2 的元素是3
         *  -------------------------
         *  | 1 | 2 | 3 | 4 | 5 | 1 |
         *  -------------------------
         *  arraycopy: index+1 位置的元素为 4; 目标数组元素 index位置元素为 3,需要移动的元素数量为3
         *  从原数组指定索引处开始 复制到目标数组 指定索引处,复制实际使用内存-索引处-1 位,得出:
         *  -------------------------
         *  | 1 | 2 | 4 | 5 | 1 | 1 |
         *  -------------------------
         */
        T oldValue = (T) elementData[index];
        int numMoved = size - index - 1;
        if (numMoved > 0) {
            System.arraycopy(elementData, index + 1, elementData, index, numMoved);
        }
        elementData[--size] = null;
        return oldValue;
    }

    public T remove() {
        return delete(size - 1);
    }

    public boolean empty() {
        return size == 0;
    }

    public void clear() {
        //将底层数组所有元素赋值为null
        Arrays.fill(elementData, null);
        size = 0;
    }

    public String toString() {
        if (size == 0) {
            return "[]";
        } else {
            StringBuilder sb = new StringBuilder("[");
            for (int i = 0; i < size; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(elementData[i].toString());
            }
            sb.append("]");
            return sb.toString();
        }
    }

    public static void main(String[] args) {
        SequenceList<String> list = new SequenceList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");
        list.insert("dddd", 1);
        System.out.println(list);
        list.delete(2);
        System.out.println(list);
        System.out.println("cccc在顺序线性表中的位置:" + list.locate("cccc"));
    }
}

线性表的顺序存储结构是指用一组地址连续的存储单元依次存放线性表的元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值