码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript数据结构-列表

时间:2015-04-26 01:51:40      阅读:167      评论:0      收藏:0      [点我收藏+]

标签: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数据结构-列表

标签:javascript;列表

原文地址:http://51web.blog.51cto.com/4386311/1638254

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!