js:資料結構筆記2---列表

來源:互聯網
上載者:User

標籤:style   blog   io   os   ar   java   for   strong   資料   

列表:

  • 定義:一組有序的資料;
function List() {   this.listSize = 0;   this.pos = 0;   this.dataStore = [];   this.find = find;   .........................}
  • 方法:
    • append:添加資料

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

    • find:尋找元素://indexOf
 function find(element) {      for(var i=0; i < this.dataStore.length; ++i) {       if(this.dataStore[i] === element) {            return 1;        }   }    return -1;}
    • remove:刪除元素;
 function  remove() {   var foundAt = this.find(element);   if(foundAt > -1) {     this.dataStore.splice(foundAt,1);     --this.listSize;     return true;   }   return false;}
    • length:返回元素個數;
function length() {   return this.listSize;}
    • toString:顯示元素;
function toString() {   return this.dataStore;}
    • insert:插入元素;
function insert(elem,after) {   var insertPos = this.find(elem);   if(insertPos > -1) {     this.dataStore.splice(insertPos+1,0,elem);  //     ++this.listSize;     return true;  }  return false;}
    • clear:清空所有元素;

 

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

 

    • contains:判斷元素;

 

function contains(elem) {   for(var i = 0; i < this.dataStore.length; ++i) {      if(this.dataStore[i] ===  elem) {          return true;     }  }     return false;}

 

    • 遍曆:

 

function front() {  this.pos = 0;    //pos範圍[0-listSize-1]}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];}

 

js:資料結構筆記2---列表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.