• 线性表

    class List<E> {
        private final int capacity;
        private int size;
        private boolean isInit = false;
        private Object[] elementData;
        /**
         * 后移元素
         * */
        private void moveToNextElement(int start, int end) {
            for (int i = 0; i < end - start; i++) {
                this.elementData[end - i] = this.elementData[end - i - 1];
            }
        }
    
        /**
         * 前移元素
         * */
        private void moveToHeadElement(int start, int end) {
            for (int i = 0; i < end - start; i++) {
                this.elementData[start + i] = this.elementData[start + i + 1];
            }
        }
    
        private boolean isIllegal(int index) {
            return index < 0 || index > size;
        }
    
        public List(int initialCapacity) {
            this.capacity = initialCapacity;
            this.size = 0;
        }
    
        /**
         * 线性表是否已满
         */
        private boolean isFull() {
            return this.size == capacity - 1;
        }
    
        /**
         * 线性表是否为空
         */
        public boolean isEmpty() {
            return this.size == 0;
        }
    
    
        /**
         * 获取最大容量
         */
        public int getCapacity() {
            return capacity;
        }
    
        /**
         * 获取当前长度
         */
        public int getSize() {
            return this.size;
        }
    
        /**
         * 插入元素
         */
        public boolean insertData(int index, E element) {
            if (isIllegal(index) || isFull()) {
                return false;
            }
            if (!isInit) {
                this.elementData = new Object[capacity];
                isInit = true;
            }
            moveToNextElement(index + 1, this.size);
            this.elementData[index] = element;
            this.size++;
            return true;
        }
    
        /**
         * 追加元素
         */
        public boolean appendElement(E element) {
            if (isFull()) {
                return false;
            }
            if (!isInit) {
                this.elementData = new Object[capacity];
                isInit = true;
            }
            this.elementData[this.size] = element;
            this.size++;
            return true;
        }
    
        /**
         * 清空顺序表
         */
        public void clear() {
            this.size = 0;
        }
    
        /**
         * 删除指定索引元素
         */
        public E removeElement(int index) {
            if (isEmpty() || isIllegal(index)) {
                return null;
            }
            E ele = get(index);
            moveToHeadElement(index, this.size);
            size--;
            return ele;
        }
    
        /**
         * 获取某个索引上的元素
         */
        @SuppressWarnings("unchecked")
        public E get(int index) {
            if (isIllegal(index)) {
                return null;
            }
            return (E) this.elementData[index];
        }
    
        @Override
        public String toString() {
            Object[] list = new Object[size];
            System.arraycopy(this.elementData, 0, list, 0, size);
            return Arrays.toString(list);
        }
    }
最后修改:2021 年 09 月 11 日
如果觉得我的文章对你有用,请随意赞赏