function List() {
this.listSize = 0;
this.pos = 0;
this.dataSource = [];
this.clear = function() {
this.dataSource = [];
this.pos = 0;
this.listSize = 0;
};
this.find = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return i;
}
};
return -1;
}
this.toString = function() {
return this.dataSource;
};
this.insert = function(item) {
var insertPos = this.find(item);
if (insertPos > -1) {
this.dataSource.splice(insertPos + 1, 0, item);
++this.listSize;
return true;
}
return false;
};
this.append = function(item) {
this.dataSource[this.listSize++] = item;
};
this.remove = function(item) {
var foundAt = this.find(item);
if (foundAt > -1) {
this.dataSource.splice(foundAt, 1);
--this.listSize;
return true;
}
return false;
};
this.front = function() {
this.pos = 0;
};
this.end = function() {
this.pos = this.listSize - 1;
};
this.prev = function() {
if (this.pos > 0) {
--this.pos;
}
};
this.next = function() {
if (this.pos < this.listSize - 1) {
++this.pos;
}
};
this.length = function() {
return this.listSize;
};
this.currPos = function() {
return this.pos;
};
this.moveTo = function(position) {
this.pos = position;
};
this.getElement = function() {
return this.dataSource[this.pos];
};
this.contains = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return true;
}
};
return false;
};
}
var list = new List();
list.append("shidengyun");
list.append("zhujing");
list.append("shidengyun");
list.append("zhujing");
console.log(list.toString());
console.log('-------------');
list.insert("shidengyun");
console.log(list.toString());
console.log('-------------');
list.remove("zhujing");
console.log(list.toString());
console.log('-------------');
list.front();
console.log(list.getElement());
list.next();
console.log(list.getElement());
list.prev();
console.log(list.getElement());
console.log('-------------');
for (list.front(); list.currPos < list.length(); list.next()) {
console.log(list.getElement());
}
console.log('-------------');
for (list.end(); list.currPos >= 0; list.prev()) {
console.log(list.getElement());
}
this.listSize = 0;
this.pos = 0;
this.dataSource = [];
this.clear = function() {
this.dataSource = [];
this.pos = 0;
this.listSize = 0;
};
this.find = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return i;
}
};
return -1;
}
this.toString = function() {
return this.dataSource;
};
this.insert = function(item) {
var insertPos = this.find(item);
if (insertPos > -1) {
this.dataSource.splice(insertPos + 1, 0, item);
++this.listSize;
return true;
}
return false;
};
this.append = function(item) {
this.dataSource[this.listSize++] = item;
};
this.remove = function(item) {
var foundAt = this.find(item);
if (foundAt > -1) {
this.dataSource.splice(foundAt, 1);
--this.listSize;
return true;
}
return false;
};
this.front = function() {
this.pos = 0;
};
this.end = function() {
this.pos = this.listSize - 1;
};
this.prev = function() {
if (this.pos > 0) {
--this.pos;
}
};
this.next = function() {
if (this.pos < this.listSize - 1) {
++this.pos;
}
};
this.length = function() {
return this.listSize;
};
this.currPos = function() {
return this.pos;
};
this.moveTo = function(position) {
this.pos = position;
};
this.getElement = function() {
return this.dataSource[this.pos];
};
this.contains = function(item) {
for (var i = this.dataSource.length - 1; i >= 0; i--) {
if (this.dataSource[i] === item) {
return true;
}
};
return false;
};
}
var list = new List();
list.append("shidengyun");
list.append("zhujing");
list.append("shidengyun");
list.append("zhujing");
console.log(list.toString());
console.log('-------------');
list.insert("shidengyun");
console.log(list.toString());
console.log('-------------');
list.remove("zhujing");
console.log(list.toString());
console.log('-------------');
list.front();
console.log(list.getElement());
list.next();
console.log(list.getElement());
list.prev();
console.log(list.getElement());
console.log('-------------');
for (list.front(); list.currPos < list.length(); list.next()) {
console.log(list.getElement());
}
console.log('-------------');
for (list.end(); list.currPos >= 0; list.prev()) {
console.log(list.getElement());
}