javascript中在鏈表中向前(向后)移動n個節(jié)點
1.概念
在鏈表上移動n個節(jié)點,我第一眼看到這個需求的時候首先想到的是當(dāng)前節(jié)點。使用這個當(dāng)前節(jié)點作為參考來移動,沒有這個當(dāng)前節(jié)點的話是沒有辦法在鏈表上前進和后退的。初始化定義鏈表的時候定義一個當(dāng)前節(jié)點,并且給這個當(dāng)前節(jié)點賦值為頭節(jié)點。向前移動的時候只需要使這個當(dāng)前節(jié)點指向它下一個節(jié)點:this.currentNode = this.currentNode.next; 向后移動節(jié)點只需要使當(dāng)前節(jié)點指向它前一個節(jié)點:this.currentNode = this.currentNode.next; 有了這個思路就好辦了,剩下的只不過要使用循環(huán)控制移動n個節(jié)點就好了,當(dāng)然向后移動的時候要判斷是否到達鏈表末尾,向前移動的時候要判斷是否到達鏈表頭,如果是就停下來,這就說明這個需求有問題了。
還有顯示當(dāng)前節(jié)點的值,這個就非常容易了,只需要把這個節(jié)點的element打印出來就好了。
2.代碼實現(xiàn)
/** * 實現(xiàn)在鏈表中向前移動n個節(jié)點和向后移動n個節(jié)點 * * */ //鏈表節(jié)點 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; //當(dāng)前節(jié)點就是頭節(jié)點 this.currentNode = this.head; //從鏈表開頭向前移動n個節(jié)點 this.advance = advance; //從鏈表某個節(jié)點向后回退n個節(jié)點 this.back = back; //顯示當(dāng)前節(jié)點 this.show = show; } //倒序輸出鏈表中的所有節(jié)點 function dispReverse(){ var currNode = this.head; currNode = this.findLast(); while (!(currNode.previous == null)){ document.write(currNode.element + ' '); currNode = currNode.previous; } } //找到最后一個節(jié)點 function findLast(){ var currNode = this.head; while (!(currNode.next == null)){ currNode = currNode.next; } return currNode; } //刪除某一個節(jié)點 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; } } //打印所有鏈表節(jié)點 function display(){ var currNode = this.head; while (!(currNode.next == null)){ document.write(currNode.next.element + ' '); currNode = currNode.next; } } //找到某一個節(jié)點 function find(item){ var currNode = this.head; while (currNode.element != item){ currNode = currNode.next; } return currNode; } //插入某一個節(jié)點 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個節(jié)點 function advance(n){ while ((n>0) && !(this.currentNode.next==null)){ this.currentNode = this.currentNode.next; n-- } } //在鏈表中向后移動n個節(jié)點 function back(n){ while (n>0 && !(this.currentNode.element=='head')){ this.currentNode = this.currentNode.previous; n--; } } //顯示當(dāng)前節(jié)點 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();
作者:Tyler Ning
出處:http://www.rzrgm.cn/tylerdonet/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,請微信聯(lián)系冬天里的一把火
浙公網(wǎng)安備 33010602011771號