#Data Structure - List @(Data Structure & Algorithm) ##Define
//define List
List = function(){
this.listSize = 0;
this.curPos = 0;
this.dataStore = [];
};
List.prototype = {
constructor: List,
getSize: function(){
return this.listSize;
},
clear: function(){
delete this.dataStore;
this.dataStore = [];
this.listSize = 0;
this.curPos = 0;
},
reveal: function(){
return this.dataStore;
},
revealCurPos: function(){
return this.curPos;
},
getElement: function(pos){
return this.dataStore[pos];
},
find: function(el){
for (var i = 0; i < this.dataStore.length; i++){
if (this.dataStore[i] === el){
return i;
}
}
return -1;
},
contains: function(el){
if (this.find(el) > -1){
return true;
}
return false;
},
insert: function(el,prev){
var pos = this.fine(prev);
if (pos > -1){
this.dataStore.splice(prev + 1, 0, el);
this.listSize++;
return true;
}
return false;
},
append: function(el){
this.dataStore[this.listSize++] = el;
},
remove: function(){
var i = this.find(el);
if (i > -1){
this.dataStore.splice(i, 1);
this.listSize--;
return true;
}
return false;
},
//Iterator methods
toFront: function(){
this.curPos = 0;
},
toEnd: function(){
this.curPos = this.listSize - 1;
},
toPrev: function(){
if (this.curPos > 0){
this.curPos--;
}
},
toNext: function(){
if (this.curPos < this.listSize - 1){
this.curPos ++;
}
},
moveTo: function(pos){
if (pos > -1 && pos < this.listSize){
this.curPos = pos;
}
}
};