列表

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.coutains = coutains;
}

append給列表添加元素

function append(element){
this.dataStore[this.listSize++] = element;
}

find在列表中查找某一元素

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 founAt = this.find(element);
if(founAt>-1){
this.dataStore.splice(founAt,1);
--this.listSize;
return true;
}
return false;
}

length列表中有多少個(gè)元素

function length(){
return this.listSize;
}

toString顯示列表中的元素

function toString(){
return this.dataStore;
}

insert向列表中插入一個(gè)元素

function insert(element,after){
//after 插入那個(gè)元素后
var insertPos = this.find(after);
if(insert>-1){
this.dataStore.splice(insertPos+1,0,element);
++this.listSize;
return true;
}
return false;
}
}

clear清空列表中所有的元素

function clear(){
delete this.dataStore;
this.dataStore = [];
this.listSize = this.pos = 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,899評(píng)論 0 33
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,051評(píng)論 0 6
  • 數(shù)組 js中本身就有數(shù)組對(duì)象,數(shù)組中的每一項(xiàng)都可以保存任何類型的數(shù)據(jù).且ECMAScript 數(shù)組的大小是可以動(dòng)態(tài)...
    escawn閱讀 1,735評(píng)論 0 2
  • 就在不久的剛才,剛剛結(jié)束一個(gè)83歲老頭的手術(shù),按理說(shuō)師傅推進(jìn)復(fù)蘇室,后面會(huì)跟著一堆的家屬。 然而,并沒(méi)有看到一個(gè)家...
    扣扣摳腳大漢閱讀 154評(píng)論 0 1
  • 因?yàn)槲冶旧韾?ài)很豐盛,但七米卻會(huì)為我擴(kuò)展愛(ài)的能力,所以你知道這種感覺(jué)嗎?除非你遇到過(guò),除非你體驗(yàn)到我的體驗(yàn),除非你真...
    水元閱讀 228評(píng)論 2 2

友情鏈接更多精彩內(nèi)容