zoukankan      html  css  js  c++  java
  • js:数据结构笔记2---列表

    列表:

    • 定义:一组有序的数据;
    function List() {
       this.listSize = 0;
       this.pos = 0;
       this.dataStore = [];
       this.find = find;
       .........................
    }
    • 方法:
      • append:添加数据

    function append(element) { this.dataStore[this.listSize++] = element; }

      • find:查找元素://indexOf
     function find(element) {
          for(var i=0; i < this.dataStore.length; ++i) {
           if(this.dataStore[i] === element) {
                return 1;  
          }
       }
        return -1;
    }
      • remove:删除元素;
     function  remove(element) {
       var foundAt = this.find(element);
       if(foundAt > -1) {
         this.dataStore.splice(foundAt,1);
         --this.listSize;
         return true;
       }
       return false;
    }
      • length:返回元素个数;
    function length() {
       return this.listSize;
    }
    
      • toString:显示元素;
    function toString() {
       return this.dataStore;
    }
      • insert:插入元素;
    function insert(elem,after) {
       var insertPos = this.find(elem);
       if(insertPos > -1) {
         this.dataStore.splice(insertPos+1,0,elem);  //
         ++this.listSize;
         return true;
      }
      return false;
    }
      • clear:清空所有元素;
    function clear() {
       delete this.dataStore;
       this.dataStore = [];
       this.listSize = this.pos = 0;
    }
      • contains:判断元素;
    function contains(elem) {
       for(var i = 0; i < this.dataStore.length; ++i) {
          if(this.dataStore[i] ===  elem) {
              return true;
         }
      }
         return false;
    }
      • 遍历:
    function front() {
      this.pos = 0;    //pos范围[0-listSize-1]
    }
    
    function end() {
      this.pos = this.listSize - 1;
    }
    
    function prev() {
      if(this.pos > 0) {
        --this.pos;
      }
    }
    
    function next() {
      if(this.pos < this.listSize) {
        ++this.pos;  //
      }
    }
    
    function currPos() {
      return this.pos;
    }
    
    function moveTo(position) {
      this.pos = position;
    }
    
    function getElement() {
      return this.dataStore[this.pos];
    }

    使用迭代器访问列表:迭代器只是用来在列表上随意移动,而不应该和任何为列表增加或删除的方法一起使用;

    demo

  • 相关阅读:
    【Python】错误、调试和测试
    【c++ primer, 5e】函数指针
    【英语学习】【17/4/1】
    【c++ primer, 5e】函数匹配
    FIRST GAME.
    【Thinking in Java, 4e】访问权限控制
    【c++ primer, 5e】特殊用途语言特性
    Top-Down笔记 #01# 计算机网络概述
    NHibernate之映射文件配置说明
    Web Service 部署到IIS服务器
  • 原文地址:https://www.cnblogs.com/jinkspeng/p/4021503.html
Copyright © 2011-2022 走看看