Javascript 列表List

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關鍵字判斷對象的類型胡野。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末材失,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子硫豆,更是在濱河造成了極大的恐慌龙巨,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件熊响,死亡現(xiàn)場離奇詭異旨别,居然都是意外死亡,警方通過查閱死者的電腦和手機汗茄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門秸弛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人洪碳,你說我怎么就攤上這事递览。” “怎么了瞳腌?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵绞铃,是天一觀的道長。 經常有香客問我纯趋,道長憎兽,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任吵冒,我火速辦了婚禮纯命,結果婚禮上,老公的妹妹穿的比我還像新娘痹栖。我一直安慰自己亿汞,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布揪阿。 她就那樣靜靜地躺著疗我,像睡著了一般。 火紅的嫁衣襯著肌膚如雪南捂。 梳的紋絲不亂的頭發(fā)上吴裤,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天,我揣著相機與錄音溺健,去河邊找鬼麦牺。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的剖膳。 我是一名探鬼主播魏颓,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼吱晒!你這毒婦竟也來了甸饱?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤仑濒,失蹤者是張志新(化名)和其女友劉穎叹话,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躏精,經...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡渣刷,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了矗烛。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡箩溃,死狀恐怖瞭吃,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情涣旨,我是刑警寧澤歪架,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站霹陡,受9級特大地震影響和蚪,放射性物質發(fā)生泄漏。R本人自食惡果不足惜烹棉,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一攒霹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧浆洗,春花似錦催束、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摘昌,卻和暖如春速妖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背聪黎。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工罕容, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓杀赢,卻偏偏與公主長得像烘跺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子脂崔,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內容