列表
列表的抽象数据类型定义
- 列表是一组有序的数据
- 每个列表中的数据项称为元素
- 在JavaScript中,列表中的元素可以是任意数据类型
- 列表中可以保存多少元素并没有事先限定,实际使用时元素的数量受到程序内存的限制
- 不包含任何元素的列表称为空列表。列表中包含元素的个数称为列表的length
- 在内部实现上,用一个变量listSize保存列表中元素的个数
- 可以在列表末尾append一个元素,也可以在一个给定元素后或列表的起始位置insert一个元素
- 使用remove方法从列表中删除元素,使用clear方法清空列表中所有的元素
- 还可以使用toString()方法显示列表中所有的元素,使用getElement()方法显示当前元素
- 列表拥有描述元素位置的属性
- 列表有前有后(分别对应front和end)
- 使用next()方法可以从当前元素移动到下一个元素,使用prev()方法可以移动到当前元素的前一个元素
- 还可以使用moveTo(n)方法直接移动到指定位置,这里的n表示要移动到第n个位置
- currPos属性表示列表中的当前位置
列表的抽象数据类型定义
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.length = length;
this.contains = contains;
}
function append(element){
this.dataStore[this.listSize++]= element;
}
function find(element){
for(var i =0; i <this.dataStore.length;++i){
if(this.dataStore[i]== element){
return i;
}
}
return-1;
}
function remove(element){
var foundAt =this.find(element);
if(foundAt >-1){
this.dataStore.splice(foundAt,1);
--this.listSize;
returntrue;
}
returnfalse;
}
function length(){
returnthis.listSize;
}
function toString(){
returnthis.dataStore;
}
var names =newList();
names.append("Cynthia");
names.append("Raymond");
names.append("Barbara");
print(names.toString());
names.remove("Raymond");
print(names.toString());
function insert(element, after){
var insertPos =this.find(after);
if(insertPos >-1){
this.dataStore.splice(insertPos+1,0, element);
++this.listSize;
return true;
}
returnfalse;
}
function clear(){
deletethis.dataStore;
this.dataStore =[];
this.listSize =this.pos =0;
}
function contains(element){
for(var i =0; i <this.dataStore.length;++i){
if(this.dataStore[i]== element){
return true;
}
}
returnfalse;
}
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(){
returnthis.pos;
}
function moveTo(position){
this.pos = position;
}
function getElement(){
returnthis.dataStore[this.pos];
}
var names =newList();
names.append("Clayton");
names.append("Raymond");
names.append("Cynthia");
names.append("Jennifer");
names.append("Bryan");
names.append("Danny");
- 访问列表元素时不必关心底层的数据存储结构
- 当为列表添加一个元素时,索引的值就不对了,此时只用更新列表,而不用更新迭代器。
- 可以用不同类型的数据存储方式实现
- cList类,迭代器为访问列表里的元素提供了一种统一的方式
for(names.front(); names.currPos()< names.length(); names.next()){
print(names.getElement());
}
for(names.end(); names.currPos()>=0; names.prev()){
print(names.getElement());
}
一个基于列表的应用
读取文本文件
function createArr(file){
var arr = read(file).split("\n");
for(var i =0; i < arr.length;++i){
arr[i]= arr[i].trim();
}
return arr;
}
使用列表管理影碟租赁
var movieList =newList();
for(var i =0; i < movies.length;++i){
movieList.append(movies[i]);
}
function displayList(list){
for(list.front(); list.currPos()< list.length(); list.next()){
print(list.getElement());
}
}
function displayList(list){
for(list.front(); list.currPos()< list.length(); list.next()){
if(list.getElement() instanceof Customer){
print(list.getElement()["name"]+", "+
list.getElement()["movie"]);
}
else{
print(list.getElement());
}
}
}
functionCustomer(name, movie){
this.name = name;
this.movie = movie;
}
function checkOut(name, movie, filmList, customerList){
if(movieList.contains(movie)){
var c =newCustomer(name, movie);
customerList.append(c);
filmList.remove(movie);
}
else{
print(movie +" is not available.");
}
}
var movies = createArr("films.txt");
var movieList =newList();
var customers =newList();
for(var i =0; i < movies.length;++i){
movieList.append(movies[i]);
}
print("Available movies: \n");
displayList(movieList);
checkOut("Jane Doe","The Godfather", movieList, customers);
print("\nCustomer Rentals: \n");
displayList(customers);
var movies = createArr("films.txt");
var movieList =newList();
var customers =newList();
for(var i =0; i < movies.length;++i){
movieList.append(movies[i]);
}
print("Available movies: \n");
displayList(movieList);
putstr("\nEnter your name: ");
var name = readline();
putstr("What movie would you like? ");
var movie = readline();
checkOut(name, movie, movieList, customers);
print("\nCustomer Rentals: \n");
displayList(customers);
print("\nMovies Now Available\n");
displayList(movieList);
程序输出如下:
Available movies:
TheShawshankRedemption
TheGodfather
TheGodfather:Part II
PulpFiction
TheGood, the Bad and the Ugly
12AngryMen
Schindler's List
TheDarkKnight
TheLord of the Rings:TheReturn of the King
FightClub
StarWars:Episode V -TheEmpireStrikesBack
OneFlewOver the Cuckoo's Nest
TheLord of the Rings:TheFellowship of the Ring
Inception
Goodfellas
StarWars
SevenSamurai
TheMatrix
ForrestGump
City of God
Enter your name:JaneDoe
What movie would you like?TheGodfather
CustomerRentals:
JaneDoe,TheGodfather
MoviesNowAvailable
TheShawshankRedemption
TheGodfather:Part II
PulpFiction
TheGood, the Bad and the Ugly
12AngryMen
Schindler's List
TheDarkKnight
TheLord of the Rings:TheReturn of the King
FightClub
StarWars:Episode V -TheEmpireStrikesBack
OneFlewOver the Cuckoo's Nest
TheLord of the Rings:TheFellowship of the Ring
Inception
Goodfellas
StarWars
SevenSamurai
TheMatrix
ForrestGump
City of God