javascript中在链表中向前(向后)移动n个节点

简介: 1.概念    在链表上移动n个节点,我第一眼看到这个需求的时候首先想到的是当前节点。使用这个当前节点作为参考来移动,没有这个当前节点的话是没有办法在链表上前进和后退的。初始化定义链表的时候定义一个当前节点,并且给这个当前节点赋值为头节点。

 1.概念

   在链表上移动n个节点,我第一眼看到这个需求的时候首先想到的是当前节点。使用这个当前节点作为参考来移动,没有这个当前节点的话是没有办法在链表上前进和后退的。初始化定义链表的时候定义一个当前节点,并且给这个当前节点赋值为头节点。向前移动的时候只需要使这个当前节点指向它下一个节点:this.currentNode = this.currentNode.next; 向后移动节点只需要使当前节点指向它前一个节点:this.currentNode = this.currentNode.next; 有了这个思路就好办了,剩下的只不过要使用循环控制移动n个节点就好了,当然向后移动的时候要判断是否到达链表末尾,向前移动的时候要判断是否到达链表头,如果是就停下来,这就说明这个需求有问题了。

  还有显示当前节点的值,这个就非常容易了,只需要把这个节点的element打印出来就好了。

2.代码实现

/**
 * 实现在链表中向前移动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();

 

作者:Tyler Ning
出处:http://www.cnblogs.com/tylerdonet/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址williamningdong@gmail.com  联系我,非常感谢。

目录
相关文章
|
1月前
|
算法
【优选算法专栏】专题九:链表--------两两交换链表中的节点
【优选算法专栏】专题九:链表--------两两交换链表中的节点
17 0
|
2月前
|
前端开发 JavaScript 区块链
连接区块链节点的 JavaScript 库 web3.js
连接区块链节点的 JavaScript 库 web3.js
31 2
|
1月前
|
JavaScript 前端开发
用JavaScript正则表达式匹配对应字符串高亮显示,并过滤掉空格、<、>等HTML节点符号
用JavaScript正则表达式匹配对应字符串高亮显示,并过滤掉空格、<、>等HTML节点符号
链表遍历,链表查找和统计节点,链表插入新节点,链表删除节点,链表修改指定节点,链表头插法,尾插法总结
链表遍历,链表查找和统计节点,链表插入新节点,链表删除节点,链表修改指定节点,链表头插法,尾插法总结
|
13天前
|
存储 Java
高效删除链表倒数节点最优实现
要删除链表的倒数第 n 个节点,并返回链表的头节点,我们可以使用一趟扫描的方法来实现。这个方法涉及使用两个指针:快指针和慢指针。
|
2月前
|
存储 算法 编译器
【C/C++ 数据结构 线性表】 数据结构 解析 链表中哨兵节点(伪节点)的作用
【C/C++ 数据结构 线性表】 数据结构 解析 链表中哨兵节点(伪节点)的作用
16 0
|
2月前
leetcode2487.从链表中移除节点
leetcode2487.从链表中移除节点
20 1
|
2月前
|
C语言
【C语言】Leetcode 876. 链表的中间节点
【C语言】Leetcode 876. 链表的中间节点
19 0
|
2月前
|
设计模式 测试技术
在实现链表的代码中,为什么要使用`Node`类而不是直接在`LinkedList`类中定义节点?
在实现链表的代码中,为什么要使用`Node`类而不是直接在`LinkedList`类中定义节点?
22 1
|
3月前
|
Java
LeetCode题解- 两两交换链表中的节点-Java
两两交换链表中的节点-Java
13 0