”为了设计列表的抽象数据类型,需要给出列表的定义,包括列表应该拥有哪些属性,应该 在列表上执行哪些操作。
列表是一组有序的数据。每个列表中的数据项称为元素。在 JavaScript 中,列表中的元素 可以是任意数据类型。列表中可以保存多少元素并没有事先限定,实际使用时元素的数量 受到程序内存的限制。
列表是一组有序的数据。每个列表中的数据项称为元素。在 JavaScript 中,列表中的元素 可以是任意数据类型。列表中可以保存多少元素并没有事先限定,实际使用时元素的数量 受到程序内存的限制。
不包含任何元素的列表称为空列表。
列表中包含元素的个数称为列表的 length。
在内部实 现上,用一个变量 listSize 保存列表中元素的个数
。可以在列表末尾 append 一个元素
, 也可以在一个给定元素后或列表的起始位置 insert 一个元素。
使用 remove 方法从列表中 删除元素,
使用 clear 方法清空列表中所有的元素。
还可以使用 toString() 方法显示列表中所有的元素,
使用 getElement() 方法显示当前元素“
function List() {
this.listSize = 0;
this.pos= 0; //索引指向
this.dataStore = []; //初始化一个空数组来保存列表元素,可以理解在内存里面开一个位置存储数据
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;
}
function append(element) {
this.dataStore[this.listSize++] = element;
}
/*
如皋找到返回位置,否则返回-1
*/
function find(element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[i] == element) {
return i;
}
}
return -1;
};
/*
依赖find
*/
function remove(element) {
var fondAt = this.find(element);
if (fondAt > -1) {
this.dataStore.splice(fondAt, 1);
--THIS.listSize;
return true;
}
return false;
}
function length() {
return this.listSize;
}
//返回一个数组
function toString() {
return this.dataStore;
}
///在指定位置插入元素
function insert(element, after) {
var insertPos = this.find(element);
console.log("find",insertPos)
if (insertPos > -1) {
this.dataStore.splice(insertPos + 1, 0, after);
++this.listSize;
return true;
}
return false;
}
function clear() {
delete this.dataStore; //没必要上次的,多此一举,下面一行把引用地址替换就可以了
this.dataStore = [];
this.listSize = this.pos = 0;
}
//判断是否在列表中
function contains(element) {
for (var i = 0; i < this.dataStore.length; ++i) {
if (this.dataStore[0] === element) {
return true;
}
}
return false;
};
一下方法运行用户在列表上只有移动
//当前位置指向最前面
function front() {
this.pos = 0;
}
//当前位置指向最后
function end() {
this.pos = this.listSize - 1;
}
function prev() {
if (this.pos > 0) {
--this.pos;
}
}
function next() {
if (this.pos < this.listSize - 1) {
++this.pos;
}
};
function currPos() {
return this.pos;
}
function moveTo(position) {
this.pos = position;
};
//返回当前元素
function getElement() {
return this.dataStore[this.pos];
}
使用:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="List.js"></script>
<script>
var names=new List();
names.append("01");
names.append("01");
names.append("02");
names.append("03");
console.log("show",names.toString())
names.insert("02","04");
console.log("show afte insert",names.toString())
console.log("默认当前是第一个",names.getElement());
names.front();
console.log("afer front之后也是第一个",names.getElement());
names.end();
console.log("afer end之后也是最后一个",names.getElement());
console.log("使用迭代器------------->")
for(names.end();names.currPos()>0;names.prev()){
console.log("使用迭代器:",names.getElement())
};
</script>
</body>
</html>