JS實現一個基於對象的鏈表

來源:互聯網
上載者:User

標籤:current   nbsp   previous   return   rem   對象   測試   例子   blog   

JS實現一個基於對象的鏈表
/*JS實現一個基於對象的鏈表*/function Node(element){    this.element = element;//節點儲存的元素    this.next = null;//節點指向的下一個節點,這裡先設定為空白}function LList(){    this.head = new Node("head");//產生一個前端節點    this.find = find;//在鏈表中找到某個節點    this.insert = insert;//在鏈表中某個元素後面插入某個節點元素    this.display = display;//在將鏈表中的節點元素顯示出來    this.findPrevious = findPrevious;//找到某個節點的上一個節點    this.remove = remove;//刪除某個節點}function remove(item) {    var prevNode = this.findPrevious(item);    if (!(prevNode.next == null)) {        prevNode.next = prevNode.next.next;    }}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;    current.next = newNode;}/*測試例子*/var num = new LList();num.insert("a1","head");num.insert("b1","a1");num.insert("c1","b1");num.display();//  a1  b1  c1num.remove("b1");num.display();//  a1  c1

 

JS實現一個基於對象的鏈表

聯繫我們

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