Js迴圈連結串列(資料結構)節點向前或向後移動n位-----添加了插入排序
阿新 • • 發佈:2019-01-27
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>迴圈連結串列Circular linked list</title> </head> <body> </body> <script> /* 以下是迴圈連結串列中的構造方法(The following is the construction method in the circular list) ***************************************************************************************** this.insert()=insert//插入連結串列中(insert the node to the LinkList) this.lastNode()=lastNode//找到連結串列的最後一個節點(find the last node of LinkList) this.forEach()=forEach//正向遍歷( forward traversal) this.forEachReverse()=forEachReverse//反向遍歷(Reverse traversal) this.find() = find;////尋找連結串列中的某個節點並返回其節點(Find a node in the list and return its node) this.findPrevious()=findPrevious//尋找連結串列中的某個節點的前一個節點並返回其節點(Find a node's previus in the list and return its node) this.remove() = remove;//移除連結串列中的某個節點(remove a node in the list) this.insertAfter()=insertAfter // 向連結串列中某個節點後面插入一個節點(insert a node after a node in the list) this.insertBefore()=inserBefore // 向連結串列中某個節點前面插入一個節點(insert a node before a node in the list) this.advance()=advance //將某個節點向前移動n位(Move a node forward by n bits) this.back ()=back // //將某個節點向後移動n位(Move a node backward by n bits) this.sort()=sort// //插入排序--從小到大排序(Insert sort--Sort from small to large) */ function Node(element) { this.element = element; this.next = null; this.previous = null; } //迴圈連結串列Circular linked list function LList() { this.head=new Node('head'); //插入連結串列中(insert the node to the LinkList) this.insert=function (element) { //var currentNode=this.head; //var node = new Node(element); //while(currentNode!=null¤tNode.element!='head') // currentNode=currentNode.next; //currentNode.next=node; //node.previous=currentNode; //node.next=this.head; //this.head.previous=node; //1,建立新的節點 var node = new Node(element); //2,獲得最後的節點 var lastNode = this.lastNode(); //3,和最後的節點連在一起 lastNode.next=node; node.previous=lastNode; this.head.previous=node; node.next=this.head; } //找到連結串列的最後一個節點(find the last node of LinkList) this.lastNode=function () { if(this.head.next==null){ return this.head; } var lastNode = this.head; while(lastNode.next.element!='head'){ lastNode = lastNode.next; } return lastNode; } //正向遍歷( forward traversal) this.forEach=function (call) { if (this.head.next == null) return; var node = this.head.next; while(node != null && node.element != 'head'){ call(node); node=node.next; } } //反向遍歷(Reverse traversal) this.forEachReverse=function (call) { if (this.head.next == null) return; var lastNode = this.head.previous; while(lastNode!=null&&lastNode.element!='head'){ call(lastNode); lastNode= lastNode.previous; } } //尋找連結串列中的某個節點並返回其節點(Find a node in the list and return its node) this.find=function (element) { if (this.head.next == null) return null; var node = this.head.next; while(node.element!='head'&&node.element!=element){ node=node.next; } if(node.element=='head'){ return null; }else{ return node; } } //移除連結串列中的某個節點(remove a node in the list) this.remove=function (element) { //1,獲得當前的節點 var node = this.find(element); //2,獲得當前節點的前一個節點 var preNode = node.previous; //3,獲得當前節點的後一個節點 var nextNode= node.next; //4,連結節點 preNode.next=nextNode; nextNode.previous = preNode; } // 向連結串列中某個節點後面插入一個節點(insert a node after a node in the list) this.insertAfter=function (element,after) { //1,找到當前的節點 var node =this.find(after); if(null==node) return -1; //2,create the new node var newNode = new Node(element); //3,新節點和當前節點的後一個節點連在一起 node.next.previous=newNode; newNode.next=node.next; node.next=newNode; newNode.previous=node; return 1; } // 向連結串列中某個節點前面插入一個節點(insert a node before a node in the list) this.insertBefore=function (element,before) { //1,找到當前的節點 var node =this.find(before); if('head'==node.element) return -1; //2,create the new node var newNode = new Node(element); //3,新節點和當前節點的前一個節點連在一起 newNode.previous=node.previous; newNode.next=node.previous.next; node.previous.next=newNode; node.previous=newNode; return 1; } //將連結串列中的某個節點向前移動n位(Move a node forward by n bits) this.advance=function(currElement,n){ var currentNode=this.find(currElement); var preNode=currentNode; while(n>0){ preNode=preNode.previous; if(preNode.element==currElement) n++; n--; } if(preNode==currentNode) return; currentNode.next.previous=currentNode.previous; currentNode.previous.next=currentNode.next; currentNode.previous=preNode.previous; currentNode.next=preNode.previous.next; preNode.previous.next=currentNode; preNode.previous=currentNode; } // //將連結串列中的某個節點向後移動n位(Move a node backward by n bits) this.back=function(currElement,n){ var currentNode=this.find(currElement); var preNode=currentNode; while(n>0){ if(preNode.element==currElement) n++; preNode=preNode.next; n--; } if(lastNode==currentNode) return; currentNode.next.previous=currentNode.previous; currentNode.previous.next=currentNode.next; currentNode.previous=preNode.previous; currentNode.next=preNode.previous.next; preNode.previous.next=currentNode; preNode.previous=currentNode; } //插入排序--從小到大排序(Insert sort--Sort from small to large) this.sort=function (data) { var newNode=new Node(data); var lastNode = this.lastNode(); if(lastNode==this.head){ lastNode.next=newNode; newNode.previous=lastNode; newNode.next=lastNode; lastNode.previous=newNode }else{ while(newNode.element.age<lastNode.element.age){ lastNode=lastNode.previous; } newNode.previous=lastNode.next.previous; lastNode.next.previous=newNode; newNode.next=lastNode.next; lastNode.next=newNode; } } } var list = new LList(); list.insert("你"); list.insert("好"); list.insert("明"); list.insert("天"); list.insert("!"); list.back('天',8); list.forEach(function (node) { console.log(node.element); }) //console.log(list.find('天1')); </script> </html>
迴圈連結串列,資料結構,節點移動
新添加了插入排序