JavaScript数据结构-列表


应用场景:购物清单


不包含任何元素的列表称为空列表


列表的完整抽象数据类型定义:

listSize( 属性) 列表的元素个数
pos(属性) 列表的当前位置
length(属性) 返回列表中元素的个数
clear(方法) 清空列表中的所有元素
toString(方法) 返回列表的字符串形式
getElement(方法) 返回当前位置的元素
insert(方法) 在现有元素后插入新元素
append(方法) 在列表的末尾添加新元素
remove(方法) 从列表中删除元素
front(方法) 将列表的当前位置设移动到第一个元素
end(方法) 将列表的当前位置移动到最后一个元素
prev(方法) 将当前位置后移一位
next(方法) 将当前位置前移一位
currPos(方法) 返回列表的当前位置
moveTo(方法) 将当前位置移动到指定位置

 


 

 

function List() {
        this.listSize = 0;
        this.append = append;
        this.remove = remove;
        this.pos = 0;
        this.dataStore = [];
        this.find = find;
        this.toString = toString;
        this.length = length;
        this.clear = clear;
        this.insert = insert;
        this.front = front;
        this.end = end;
        this.prev = prev;
        this.next = next;
        this.currPos = currPos;
        this.moveTo = moveTo;
        this.getElement = getElement;
        this.contains = contains;
      }
      function append(element) {
        this.dataStore[this.listSize++] = element;
      }
      function find(element) {
        var dataStore = this.dataStore;
        for(var i = 0, len = dataStore.length; i < len; i++) {
          if(dataStore[i] == element) {
            return i;
          }
        }
        return -1;
      }
      function remove(element) {
        var fountAt = this.find(element);
        if(fountAt > -1) {
          this.dataStore.splice(fountAt, 1);
          --this.listSize;
          return true;
        }
        return false;
      }
      function length() {
        return this.listSize;
      }
      function toString() {
        return this.dataStore;
      }
      function clear() {
        delete this.dataStore;
        this.dataStore = [];
        this.listSize = this.pos = 0;
      }
      function insert(element, after) {
        var insertPos = this.find(after);
        if(insertPos > -1) {
          this.dataStore.splice(insertPos + 1, 0, element); ++this.listSize;
          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) {
        this.pos = position;
      }
      function getElement() {
        return this.dataStore[this.pos];
      }
      function contains(element) {
        var dataStore = this.dataStore;
        for(var i = 0, len = dataStore.length; i < len; i++) {
          if(dataStore[i] == element) {
            return true;
          }
        }
        return false;
      }
      var names = new List();
      names.append("1");
      names.append("2");
      names.append("3");
      names.append("4");
      names.append("5");
      names.append("6");
      names.insert("7", "6");
      names.prev();
      console.log(names.contains("7"));

 

 

 

 

 


你可能感兴趣的:(JavaScript,数据结构,列表)