javascript中在鏈表中向前(向後)移動n個節點

來源:互聯網
上載者:User

標籤:

 

 

/** * 實現在鏈表中向前移動n個節點和向後移動n個節點 *  * *///鏈表節點function Node(element){    this.element = element;    this.next = null;    this.previous = null;}//鏈表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;    //當前節點就是前端節點    this.currentNode = this.head;    //從鏈表開頭向前移動n個節點    this.advance = advance;    //從鏈表某個節點向後回退n個節點    this.back = back;    //顯示當前節點    this.show = show;}//倒序輸出鏈表中的所有節點function dispReverse(){    var currNode = this.head;    currNode = this.findLast();    while (!(currNode.previous == null)){        document.write(currNode.element + ‘&nbsp;‘);        currNode = currNode.previous;    }}//找到最後一個節點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;    }}//列印所有鏈表節點function display(){    var currNode = this.head;    while (!(currNode.next == null)){        document.write(currNode.next.element + ‘&nbsp;‘);        currNode = currNode.next;    }}//找到某一個節點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;    current.next = newNode;}//在鏈表中向前移動n個節點function advance(n){    while ((n>0) && !(this.currentNode.next==null)){        this.currentNode = this.currentNode.next;         n--    }}//在鏈表中向後移動n個節點function back(n){    while (n>0 && !(this.currentNode.element==‘head‘)){        this.currentNode = this.currentNode.previous;        n--;    }}//顯示當前節點function show(){    document.write(this.currentNode.element);}var cities = new LList();cities.insert(‘Conway‘,‘head‘);cities.insert(‘Russellville‘, ‘Conway‘);cities.insert(‘Carlisle‘, ‘Russellville‘);cities.insert(‘Alma‘ , ‘Carlisle‘);cities.insert(‘dezhou‘ , ‘Alma‘);cities.insert(‘alasijia‘ , ‘dezhou‘);cities.display();document.write(‘<br>‘);cities.show();cities.advance(4);document.write(‘<br>‘);cities.show();cities.back(2);document.write(‘<br>‘);cities.show();

 

javascript中在鏈表中向前(向後)移動n個節點

聯繫我們

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