List是日常生活中使用最多的一種數(shù)據(jù)組織工具经瓷,例如購物單汤善,運貨單,排名表等牧愁。注意List不適合需要進行頻繁排序或查找的場景素邪。
List中的元素是按順序組織(存儲)起來的。元素可以是任意類型猪半⊥秒可以在列表的任意位置添加或刪除元素。
List的ADT(抽象數(shù)據(jù)類型)定義
屬性或函數(shù) | 說明 |
---|---|
listSize | list中元素個數(shù) |
pos | 當前訪問位置 |
length | 元素個數(shù) |
clear() | 清除所有元素 |
toString() | list的字符串表示 |
getElement() | 獲取返回當前位置pos的元素 |
insert(ind) | 指定位置后插入元素 |
append() | 在結尾添加元素 |
remove() | 刪除元素 |
front() | 設置當前位置pos為首元素 |
end() | 設置當前位置pos為尾元素 |
prev() | 將當前位置回退一個元素 |
next() | 將當前位置前進一個元素 |
currPos() | 返回當前位置 |
moveTo() | 移動當前位置到指定位置 |
List的類定義
function List() { // 定義class List
this.listSize = 0;
this.pos = 0;
this.dataStore = []; // 存儲列表元素
this.clear = clear; // 此函數(shù)在后面定義
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; // 未找到時返回-1
}
function remove(element) {
var foundAt = this.find(element);
if (foundAt > -1) {
this.dataStore.splice(foundAt,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(after);
if (insertPos > -1) {
this.dataStore.splice(insertPos+1, 0, element);
++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[i] == 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];
}
假設有文件films.txt包含如下內容:
The Shawshank Redemption
The Godfather
The Godfather: Part II
Pulp Fiction
The Good, the Bad and the Ugly
12 Angry Men
Schindler’s List
The Dark Knight
The Lord of the Rings: The Return of the King
Fight Club
Star Wars: Episode V - The Empire Strikes Back
One Flew Over the Cuckoo’s Nest
The Lord of the Rings: The Fellowship of the Ring
Inception
Goodfellas
Star Wars
Seven Samurai
The Matrix
Forrest Gump
City of God
現(xiàn)在我們編程序使用List來管理電影數(shù)據(jù)
// 讀取電影清單办龄,保存在數(shù)組中
function createArr(file) {
var arr = read(file).split("\n");
for (var i = 0; i < arr.length; ++i) {
arr[i] = arr[i].trim();
}
return arr;
}
// 創(chuàng)建List烘绽,保存電影
var movieList = new List();
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 Customer(name, movie) {
this.name = name;
this.movie = movie;
}
// list中存儲的是Customer對象俐填,顯示所有Customer信息
function displayList(list) {
for (list.front(); list.currPos() < list.length(); list.next()) {
if (list.getElement() instanceof Customer) { // 使用 instanceof 判斷對象類型
print(list.getElement()["name"] + ", " +
list.getElement()["movie"]);
}
else {
print(list.getElement());
}
}
}
// 借閱電影
function checkOut(name, movie, filmList, customerList) {
if (movieList.contains(movie)) {
var c = new Customer(name, movie);
customerList.append(c);
filmList.remove(movie);
}
else {
print(movie + " is not available.");
}
}
總結
List是一種非常常用的數(shù)據(jù)結構安接,提供了靈活的訪問接口。適合管理有序數(shù)據(jù),但要注意其查詢和插入效率都是O(n)盏檐,較費時歇式。可以使用instanceof關鍵字判斷對象的類型胡野。