zoukankan      html  css  js  c++  java
  • 顺序存储结构实现查询、删除、插入、末尾增加

    顺序存储结构查询时间复杂度 O(1),插入和删除时间复杂度 O(n)

    class LinearList{
        constructor() {  
            this.arr = [];
        }
        // 查询某位置的元素
        getElementAt(position) {
            return this.arr[position];
        } 
        // 在末尾增加元素
        append(element){
            this.arr[this.arr.length] = element; 
        }
        // 某位置插入元素 当前位置到结尾倒序 后一位=前一位
        insert(position, elements) { 
            if(position <= this.arr.length){ 
                // let originLen = this.length;
                elements.forEach(element => {
                    for(let i = this.arr.length - 1; i >= position; i--){
                        this.arr[i+1] = this.arr[i]; 
                    }
                    this.arr[position] = element;   
                })  
            }else{
                elements.forEach(element => {
                    this.arr[position] = elements; 
                })
            } 
        }
        // 在某位置删除元素 当前位置到结尾正序 前一位=后一位
        delete(position, count) {
            if(position >= this.arr.length) {
                return;
            }else{ 
                for(let i = 0; i < count; i++){
                    for(let j = position; j < this.arr.length - 1; j++){
                        this.arr[j] = this.arr[j+1];
                    }
                    this.arr.length--;
                } 
            }
        } 
        // 清空数组
        clear() {
            this.arr = [];
        }
    }
  • 相关阅读:
    poj 3096 Surprising Strings (set)
    hdu 4038 stone
    STL set 使用总结
    poj 3185 The Water Bowls (bfs 加未压缩)
    QPixmap显示图片
    addStretch的作用 .
    Qt SizeHint()
    StyleSheet
    linux编程守护进程编写
    Qt样式表的使用
  • 原文地址:https://www.cnblogs.com/Isabel-1996/p/13883932.html
Copyright © 2011-2022 走看看