Data Structure: List

algorithms: ADT List

by nickadeemus2002

JavaScript

/* node module: */

//constructor
var List = function() {
  //properties
  this.dataStore    = [];
  this.type         ='';
  this.position     = 0;
  this.listSize     = 0;
}

//methods
List.prototype = {
  constructor: List,
  // insert single item to list
  // and increment listSize
  insertItem:  function(element){
    this.dataStore[this.listSize++] = element;
  },
  appendItem:  function(){},
  // remove item from dataStore
  // using instance attribute
  removeItem:  function(topLevelProp){
    var targetId = this.findItemPosition(topLevelProp),
        removed= false;
    if(targetId !== -1){
      this.dataStore.splice(targetId, 1);
      --this.listSize;
      removed = true;
    }
    return removed;
  },
  // remove all values with
  // new empty array, reset
  // listSize and position
  removeAllItems:  function(){
    delete this.dataStore;
    this.dataStore = [];
    this.listSize = this.position = 0;
    return true;
  },
  replaceAllItems:  function(dataStore){
    delete this.dataStore;
    this.dataStore = dataStore;
    this.listSize = dataStore.length;
    this.position = 0;
    return true;
  },
  // find element value in
  // datastore by id
  findItem:  function(id){
    return this.dataStore.filter(function(item, idx){
      return item.id === id;
    });
  },
  // find element datastore
  // index position by id
  findItemPosition:  function(topLevelProp){
    var position = -1;
    this.dataStore.forEach(function(item, idx){
      var targetKey = Object.keys(topLevelProp)[0];
      if (item[targetKey] === topLevelProp[targetKey]){
        position = idx;
      }
    });
    return position;
  },
  nextItem:  function(){},
  previousItem:  function(){},
  moveToItem:  function(){},
  getFront:  function(){},
  getEnd:  function(){},
  getElement:  function(){},
  containsItem:  function(){},
  // determines whether current
  // item is preceeded by another
  // item.
  hasPrevious:  function(idx){
    return !!this.dataStore[idx -1];
 ...