List DT

List Abstract Data Type

by nickadeemus2002

JavaScript

/**
************************
* ES 5
************************
*/
/*
//List constructor
function List() {
   this.listSize = 0;
   this.pos = 0;
   this.dataStore = []; // initializes an empty array to store list elements
}

List.prototype.clear = clear;
List.prototype.find = find;
List.prototype.toString = toString;
List.prototype.insert = insert;
List.prototype.append = append;
List.prototype.remove = remove;
List.prototype.front = front;
List.prototype.end = end;
List.prototype.previous = previous;
List.prototype.next = next;
List.prototype.hasPrevious = hasPrevious;
List.prototype.hasNext = hasNext;
List.prototype.length = length;
List.prototype.currPos = currPos;
List.prototype.moveTo = moveTo;
List.prototype.getElement = getElement;
List.prototype.contains = contains;


//es2015
List.prototype = {
   constructor: List,
   clear,
   find,
   toString,
   insert,
   append,
   remove,
   front,
   end,
   previous,
   next,
   hasPrevious,
   hasNext,
   length,
   currPos,
   moveTo,
   getElement,
   contains
};


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

//remove element from list
function remove(element) {
   var foundAt = find(element);
   if (foundAt > -1) {
      this.dataStore.splice(foundAt,1);
      --this.listSize;
      return true;
   }

   return false;
}

//find element in list
function find(element) {
   if(this.dataStore){
      for (var i = 0; i < this.dataStore.length; ++i) {
         if (this.dataStore[i] == element) {
            return i;
         }
         else {
            return -1;
         }
      }
   } else {
         return -1;
   }
}

// get lengh of list
function length() {
   return this.listSize;
}

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 clear() {
   delete this.dataStore;
  ...