列表
定义
列表是一组有序的数据。每个列表中的数据称为 元素 。在JavaScript中,列表中的元素可以使任何数据类型,同时没有显示能保存多少元素
列表抽象数据类型定义
属性和方法
- listSize         列表的元素个数
- pos               列表的当前元素
- length()       返回列表元素个数
- clear()          清空列表所有元素
- toString()   返回列表字符串形式
- getElement()返回当前位置元素
- insert() 在现有元素后插入新元素
- append() 在列表末尾添加新元素
- remove()从列表中删除元素
- front()将列表的当前位置移动到第一个元素
- end() 将列表当前位置移动到最后一个元素
- prev()将当前位置后移一位
- next()将当前元素前移一位
- currPos()返回列表的当前位置
- moveTo()将当前位置移动到指定位置
实现列表类
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | 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.remo=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.length=length;
 this.contains=contains;
 }
 
 | 
append 给列表添加元素
| 12
 3
 
 | function append(element){this.dataStore[this.listSize++]=element;
 }
 
 | 
find在列表查找某一元素
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | function find(element){for(var i=0;i<this.dataStore.length;i++){
 if(this.dataStore[i]==element{
 return i;
 }
 }
 return -1;
 }
 #### remove从列表删除元素
 function remove(element){
 var foundAt=this.find(element);
 if(foundAt-1{
 this.dataStore.splice(foundAt,1)
 --this.listsize;
 return false;
 }
 }
 
 |