javascript完成數據構造中的列表構造

  • 定義組織列表的函數
function List() {
    
    this.listSize = 0;   列表的元素個數
 
    this.pos = 0;    列表的當前位置

    this.dataStore = [];    列表數組

    this.append = append;    列表的末端增加新元素

    this.find = find;    找到指定元素的位置

    this.toString = toString;     返回列表的字符串情勢

    this.insert = insert;    在現有元素后插進去新元素

    this.remove = remove;    從列表中刪除元素

    this.clear = clear;    清空列表中的一切元素

    this.front = front;    將列表的當前位置移到第一個元素

    this.end = end;    將列表的當前位置移到末了一個元素

    this.next = next;    將當前位置后移一名

    this.hasNext;    推斷是不是有后一名

    this.hasPrev;    推斷是不是有前一名

    this.length = length;   返回列表元素的個數
 
    this.currPos = currPos;    返回列表的當前位置

    this.moveTo = moveTo;    將列表的當前位置挪動到指定位置

    this.getElement = getElement;    返回當前位置的元素

    this.contains = contains; 推斷給定元素是不是在列表中
}
  • 完成元素插進去函數
function append(element) {
    this.dataStore[this.listSize++] = element;
}

  • 找到元素函數
function find(element) {
    for (let i = 0; i < this.listSize; i++) {
        console.log(i);
        if (element == this.dataStore[i]) {
            return i;
        }
    }

    return -1;
}
  • 刪除列表中的某個元素

function remove(element) {
    let findAt = this.find(element);
    if (findAt > -1) {
        this.dataStore.splice(findAt, 1);
        --this.listSize;
        return true;
    }
}
  • 取得列表的長度
function length() {
    return this.listSize;
}
  • 返回列表的字符串範例數據
function toString() {
    return this.dataStore;
}
  • 在列表中指定元素后插進去元素
function insert(element, after) {
    let insertAt = this.find(after);
    if (insertAt > -1) {
        this.dataStore.splice(insertAt + 1, 0, element);
        this.listSize++;
        return true;
    }
    return false;
}
  • 清空全部列表
function clear() {
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = this.pos = 0;
}
  • 列表是不是包括某個元素
function contains(element) {
    for (let i = 0; i < this.listSize; i++) {
        if (this.dataStore[i] == element) {
            return true;
        }
    }
    return false;
}
  • 當前列表的指針指向首部
function front() {
    this.pos = 0;
}
  • 當前列表的指針指向尾部
function end() {
    this.pos = this.listSize - 1;
}
  • 當前列表元素的前一個
function prev() {
    if (this.pos > 0) {
        this.pos--;
    }
}
  • 當前列表元素的后一個
function next() {
    if (this.pos < this.listSize - 1) {
        this.pos++;
    }
}

  • 當前的位置

    function currPos() {

       return this.pos;

    }

  • 挪動到指定位置
function moveTo(position) {
    if (position < this.listSize - 1) {
        this.pos = position;
    }
}
  • 取得當前位置的元素
function getElement() {
    return this.dataStore[this.pos];
}
  • 是不是有下一個元素
function hasNext() {
    return this.pos < this.listSize - 1;
}
  • 是不是有上一個元素
function hasPrev() {
    return this.pos > 0;
}

//初始化一個列表
let list = new List();
list.append('jianguang');
list.append('yinjun');
list.append('jiangsssuang');
list.append('yinssjun');


挪動到第一個元素位置而且顯現
list.front();
print(list.getElement());
挪動向前一個元素位置,而且顯現
list.next(); 
print(list.getElement());

還能夠測試列表的其他數據來經由過程列表完成想要的結果

迎接批評以及留言,同時迎接關注我的博客定時不斷地更新我的文章 陳建光的博客

    原文作者:陳建光
    原文地址: https://segmentfault.com/a/1190000014442085
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞