• javascript中在链表中向前(向后)移动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();
  • 相关阅读:
    【Linux学习七】软件安装
    【Linux学习六】用户管理
    【Linux学习五】文本处理
    【Linux学习四】正则表达式
    【Linux学习三】VI/VIM全屏文本编辑器
    【Linux学习二】文件系统
    【Linux学习一】命令查看与帮助
    【安装虚拟机四】设置快照和克隆
    【安装虚拟机三】设置Linux IP地址
    SpringBoot之定时任务详解
  • 原文地址:https://www.cnblogs.com/tylerdonet/p/5968240.html
Copyright © 2020-2023  润新知