class List {
constructor() {
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
}
findIndex(element) {
return this.dataStore.findIndex(ele => ele === element);
}
clear() {
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}
toString() {
return this.dataStore;
}
length() {
return this.listSize;
}
getElement() {
return this.dataStore[this.pos];
}
insert(element, after) {
const index = this.findIndex(after);
if (index > -1) {
this.dataStore.splice(index + 1, 0, element);
this.listSize++;
return true;
}
return false;
}
append(element) {
this.dataStore[this.listSize++] = element;
}
remove(elememt) {
const index = this.findIndex(elememt);
if (index > -1) {
this.dataStore.splice(index, 1);
this.listSize--;
return true;
}
return false;
}
front() {
this.pos = 0;
}
end() {
this.pos = this.listSize - 1;
}
prev() {
if (this.pos > 0) {
this.pos--;
}
}
next() {
if (this.pos < this.listSize - 1) {
this.pos++;
}
}
currPos() {
return this.pos;
}
moveTo(position) {
this.pos = position;
}
contains(element){
return this.dataStore.includes(element);
}
}
const names = new List();
names.append("Cynthia");
names.append("Raymond");
names.append("Barbara");
for (names.front(); names.currPos() < names.length(); names.next()) {
console.log(names.getElement());
}
console.log(names.contains('Raymond'))