List Class Implementation
// Defination of construction function
function List(){
this.listSize = 0;
this.pos=0;
this.dataStore = []; //initialize an empty array to store list elements
this.clear =clear;
this.find = find;
this.toString = toString;
this.insert = insert;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.contains = contains;
}
// Append
function append(element){
this.dataStore[this.listSize++] = element;
}
// Find
function find(element){
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
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(element,after){
var insertPos = this.find(after);
if(insertPos > -1){
this.dataStore.splice(insertPos+1,0,element);
++this.listSize;
return true;
}
return false;
}
// Clear
function clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}
// Contains
function contains(element){
for(var i=0;i < this.dataStore.length; ++i){
if(this.dataStore[i] == element){
return true;
}
}
return false;
}
// front
function front(){
this.pos = 0;
}
// end
function end(){
this.pos = this.listSize -1;
}
// prev
function prev(){
if(this.pos > 0){
-- this.pos;
}
}
// next
function next(){
if (this.pos < this.listSize-1) {
++this.pos;
}
}
// currPos
function currPos(){
return this.pos;
}
// moveTo
function moveTo(position){
this.pos = position;
}
// getElement
function getElement(){
return this.dataStore[this.pos];
}
//----------------------------------//
// Test
//----------------------------------//
// var names = new List();
// names.append("Clayton");
// names.append("Raymond");
// names.append("Cynthia");
// names.append("Jennifer");
// names.append("Bryan");
// names.append("Danny");
// names.front();
// console.log(names.getElement()); // displays Clayton