Skip to content

Instantly share code, notes, and snippets.

@binary10ve
Created April 23, 2016 19:25
Show Gist options
  • Save binary10ve/b70d608c248032bd2fb1fe480f6a6f72 to your computer and use it in GitHub Desktop.
Save binary10ve/b70d608c248032bd2fb1fe480f6a6f72 to your computer and use it in GitHub Desktop.
List ADT
function List(){
this.listSize = 0;
this.pos = 0;
this.data = [];
}
List.prototype = {
toString : function(){
return this.data.toString();
},
getElement : function(){
return this.data[this.pos];
},
clear : function(){
this.data = [];
},
insert : function(element, i){
this.data.splice(i,0, element);
},
add : function(element){
this.data.push(element);
this.listSize = this.data.length;
},
remove : function(){
this.data.splice(this.pos, 1);
this.listSize = this.data.length;
},
next : function(){
this.pos = (this.data.length - 1) === this.pos ? this.pos : this.pos + 1;
return this.pos;
},
prev : function(){
this.pos = this.pos === 0 ? this.pos : this.pos - 1;
return this.pos;
},
moveTo : function(n){
if(n < this.data.length && n !== 0){
this.pos = n;
}else{
throw "Scandal at";
}
return this.pos;
},
currPos : function(){
return this.pos;
},
front : function(){
this.pos = 0;
},
end : function(){
this.pos = this.data.length - 1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment