javascript資料結構之雙鏈表插入排序執行個體詳解_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了javascript資料結構之雙鏈表插入排序實現方法。分享給大家供大家參考,具體如下:

數組儲存前提下,插入排序演算法,在最壞情況下,前面的元素需要不斷向後移,以便在插入點留出空位,讓目標元素插入。

換成鏈表時,顯然無需做這種大量移動,根據每個節點的前驅節點“指標”,向前找到插入點後,直接把目標值從原鏈表上摘下,然後在插入點把鏈表斷成二截,然後跟目標點重新接起來即可。

<!doctype html><html><head>  <title>雙鏈表-插入排序</title>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /></head><script type="text/javascript">  //節點類  var Node = function (pData) {    this.next = null; //後繼“指標”    this.prev = null; //前驅"指標"    this.data = pData;  }  //單鏈表(約定:前端節點不放內容,當哨兵位,有效元素從前端節點後的第1個元素開始)  var DbLinkList = function () {    this.head = new Node(null); //前端節點       //插入新元素    this.insert = function (pNodeValue) {      var newNode = new Node(pNodeValue);      //如果只有前端節點      if (this.head.next == null) {        this.head.next = newNode;        newNode.prev = this.head;        return;      }      //否則遍曆找到尾節點      var p = this.head;      while (p.next != null) {        p = p.next;      }      p.next = newNode;      newNode.prev = p;    }    //擷取第n個元素的資料值    this.getData = function (index) {      if (index < 1 || index > this.size) {        return null;      }      var p = this.head;      var i = 1;      while (p.next != null && i <= index) {        p = p.next;        i += 1;      }      return p.data;    }    //取尾節點    this.getTail = function () {      if (this.head.next == null) {        return null;      }      var p = this.head.next;      while (p.next != null) {        p = p.next;      }      return p;    }    //刪除指定位置的元素    this.removeAt = function (index) {      if (index < 1 || index > this.size) {        return null;      }      var p = this.head;      var i = 1;      //從頭開始遍曆,找到index位置的前一個元素      while (p.next != null && i < index) {        p = p.next;        i += 1;      }      p.next = p.next.next; //修改index位置前一個元素的後繼指標      p.next.prev = p;      return p.data; //返回刪除元素的值        }    //列印所有元素    this.print = function () {      document.write("<br/>");      if (this.head.next == null) {        return;      }      var p = this.head.next;      while (p.next != null) {        document.write(p.data + " ");        p = p.next;      }      document.write(p.data + " "); //最後一個元素,需要單獨列印      document.write("<br/>");    }    //從後列印所有元素    this.printFromBack = function () {      document.write("該鏈表共有" + this.size + "個元素,從後向前分別為:<br/>");      var tail = this.getTail();      var p = tail;      if (p == null) {        return;      }      while (p.prev != null) {        document.write(p.data + " ");        p = p.prev;      }      document.write("<br/>");    }    //插入排序    this.insertSort = function () {      if (this.head.next == null || this.head.next.next == null) {        return;      }      var p = this.head.next;      while (true) {        if (p == null) {          return;        }        var t = p.prev;        //向前尋找p之前的插入點        while (t.prev != null && t.data > p.data) {          t = t.prev;        }        //如果插入點就是p的前驅節點,不用調整,        //忽略,直接進入下一輪        if (t.next == p) {          p = p.next;          continue;        }        //將p的後續節點先保護起來,以便下一輪迴圈時確定起始位置        var x = p.next;        //將p從鏈表上摘下        if (p.next != null) {          p.next.prev = p.prev;        }        p.prev.next = p.next;        //p插入到t之後        t.next.prev = p;        p.next = t.next;        t.next = p;        p.prev = t;        this.print(); //列印輸出,調試用          //重新將p定位到下一輪迴圈的"正確"起始節點        p = x;      }    }  }  var linkTest = new DbLinkList();  linkTest.insert(10);  linkTest.insert(9);  linkTest.insert(8);  linkTest.insert(7);  linkTest.insert(6);  linkTest.insert(5);  linkTest.insert(4);  linkTest.insert(3);  linkTest.insert(2);  linkTest.insert(1);  document.write("--排序前---<br/>")  linkTest.print();  linkTest.insertSort();  document.write("<br/>--排序後---<br/>")  linkTest.print();</script></html>

運行結果如下:

 --排序前---10 9 8 7 6 5 4 3 2 1 9 10 8 7 6 5 4 3 2 1 8 9 10 7 6 5 4 3 2 1 7 8 9 10 6 5 4 3 2 1 6 7 8 9 10 5 4 3 2 1 5 6 7 8 9 10 4 3 2 1 4 5 6 7 8 9 10 3 2 1 3 4 5 6 7 8 9 10 2 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 --排序後---1 2 3 4 5 6 7 8 9 10

希望本文所述對大家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.