首页 > 代码库 > javascript中在链表中向前(向后)移动n个节点
javascript中在链表中向前(向后)移动n个节点
/** * 实现在链表中向前移动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 + ‘ ‘); 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 + ‘ ‘); 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个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。