js實現雙向鏈表, 雙向鏈表需要增加一個previous屬性

來源:互聯網
上載者:User

標籤:logs   node   element   sel   amp   class   next   his   鏈表   

雙向鏈表,
雙向鏈表需要增加一個previous屬性
/*雙向鏈表* */function Node(element) {    this.element = element;    this.next = null;    this.previous = null;//雙向鏈表在這裡需要增加一個previous屬性}function LList() {    this.head = new Node("head");    this.find = find;    this.insert = insert;    this.display = display;    this.remove = remove;    this.findLast = findLast;    this.dispReverse = dispReverse;//將鏈表反轉}function dispReverse() {    var currNode = this.head;    currNode = this.findLast();    var nodestr = "";    while (!(currNode.previous == null)) {        nodestr += " "+currNode.element;        currNode = currNode.previous;    }    console.log("將鏈表反轉後: "+nodestr);}function findLast() {    var currNode = this.head;    while (!(currNode.next == null)) {        currNode = currNode.next;    }    return currNode;}function remove(item) {    var currNode = this.find(item);    if (!(currNode.next == null)) {        currNode.previous.next = currNode.next;        currNode.next.previous = currNode.previous;        currNode.next = null;        currNode.previous = null;    }}// findPrevious is no longer needed/*function findPrevious(item) { var currNode = this.head; while (!(currNode.next == null) && (currNode.next.element != item)) { currNode = currNode.next; } return currNode; }*/function display() {    var currNode = this.head;    var nodestr = "";    while (!(currNode.next == null)) {        nodestr += " "+currNode.next.element;        currNode = currNode.next;    }    console.log(nodestr);}function find(item) {    var currNode = this.head;    while (currNode.element != item) {        currNode = currNode.next;    }    return currNode;}function insert(newElement, item) {    var newNode = new Node(newElement);    var current = this.find(item);    newNode.next = current.next;    newNode.previous = current;//雙向鏈表在這裡需要設定新節點previous屬性    current.next = newNode;}var cities = new LList();cities.insert("Conway", "head");cities.insert("Russellville", "Conway");cities.insert("Carlisle", "Russellville");cities.insert("Alma", "Carlisle");cities.display();//Conway Russellville Carlisle Almacities.remove("Carlisle");cities.display();//Conway Russellville Almacities.dispReverse();// Alma Russellville Conway

 

js實現雙向鏈表, 雙向鏈表需要增加一個previous屬性

聯繫我們

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