資料結構之列表-javascript實現

來源:互聯網
上載者:User

標籤:name   判斷   資料   類型   模式   new   操作   上進   remove   

學習資料結構的記錄

 

列表是一種資料項目構成的有限序列,即按照一定的線性順序,排列而成的資料項目的集合,在這種資料結構上進行的基本操作包括對元素的的尋找,插入,和刪除列表的兩種主要表現是數組和鏈表,棧和隊列是兩種特殊類型的列表

 

迭代器(iterator)有時又稱遊標(cursor)是程式設計的軟體設計模式,可在容器(container,例如鏈表或陣列)上遍訪的介面,設計人員無需關心容器的內容

 

front,end,prev,next使用了迭代器的原理

‘use strict‘    function List(){        this.size = 0;        this.pos = 0;        this.data = [];        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.contains = contains;    };    function append(element){        this.data[this.size++] = element;    }    function find(element){        for(var i = 0; i < this.data.length; i++){            if(this.data[i] == element){                return i;            }        }        return -1;    }    function remove(element){        var foundAt = this.find(element);        if(foundAt > -1){            this.data.splice(foundAt,1);            --this.size;            return true;        }        return false;    }    function length(){        return this.size;    }    function toString(){        return this.data;    }    function insert(element, after){        var insertPos = this.find(after);        if(insertPos > -1){            this.data.splice(insertPos+1, 0, element);            ++this.size;            return true;        }        return false;    }    function clear(){        delete this.data;        this.data.length = 0;        this.size = this.pos = 0;    }    /*判斷查詢元素是否在列表內*/    function contains(element){        for (var i = 0; i < this.data.length; i++) {            if(this.data[i] == element){                return true;            }        }        return false    }    /*移動到第一個元素*/    function front(){        this.pos = 0;    }    /*移動到最後一個元素*/    function end(){        this.pos = this.size - 1;    }    function prev(){        if(this.pos > 0){            --this.pos;        }    }    function next(){        if(this.pos < this.size -1){            ++this.pos;        }    }    function currPos(){        return this.pos;    }    function getElement(){        return this.data[this.pos];    }    var names = new List();    names.append(‘lily‘);    names.append(‘tom‘);    names.append(‘king‘);    names.append(‘lihua‘);    names.append(‘lisi‘);    names.front();    console.log(names.getElement()) //lily    names.next();    console.log(names.getElement()) //tom    var asd = names.find(‘king‘)    console.log(asd)                //2    names.insert(‘jacks‘, ‘king‘)    console.log(names.data)         //["lily", "tom", "king", "jacks", "lihua", "lisi"]

 

資料結構之列表-javascript實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.