数据结构与算法JavaScript描述[第六章](链表)


   //双向链表
    var BLinkList = function () {
        this.head = new BNode('head');
        this.find = function (item) {
            var currNode = this.head;
            while(currNode.element != item){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.insert = function (newElement,item) {
            var found = this.find(item);
            var newNode = new BNode(newElement);
            newNode.next = found.next;
            newNode.previous = found;
            found.next = newNode;
        }
        this.remove = function (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;
            }
        }
        this.findLast = function () {
            var currNode = this.head;
            while(!(currNode.next == null)){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.disReverse = function () {
            var currNode = this.findLast();
            while(!(currNode.previous == null)){
                console.log(currNode.element);
                currNode = currNode.previous;
            }
        }
        this.display = function () {
            var currNode = this.head;
            while(currNode != null){
                console.log(currNode.element);
                currNode = currNode.next;
            }
        };
    }



   var BBLinkList = function () {
        this.currNode = this.head;
    };
    BBLinkList.prototype = new BLinkList();



1.实现advance(n)方法,使当前节点向前移动n个节点。

  //向前移动n个节点
    BBLinkList.prototype.advance = function (n) {
        while ((n>0) && !(this.currNode==null)){
            this.currNode = this.currNode.next;
            n--
        }
    };


2.实现back(n)方法,使当前节点向后移动n个节点。
    BBLinkList.prototype.back = function (n) {
        while ((n>0) && !(this.currNode==null)){
            this.currNode = this.currNode.previous;
            n--
        }
    }


3.实现show()方法,只显示当前节点上的数据
    BBLinkList.prototype.show = function () {
        return this.currNode?this.currNode.element:'溢出';
    }


    //测试程序
    var bblist = new BBLinkList();
    bblist.insert('A','head');
    bblist.insert('B','A');
    bblist.display();
    console.log('向前移动2个节点');
    bblist.advance(2);
    console.log(bblist.show());
    console.log('向后移动1个节点');
    bblist.back(1);
    console.log(bblist.show());

4.使用单向列表写一段程序,记录用户输入的一组测验成绩。

    //单向列表
    var LinkedList = function () {
        this.head = new Node('head');
        this.find = function (item) {
            var currNode = this.head;
            while (currNode.element != item){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.insert = function (newElement,item) {
            var newNode = new Node(newElement);
            var foundAt = this.find(item);
            newNode.next = foundAt.next;
            foundAt.next = newNode;
        }
        this.remove = function (item) {
            var foundPreviousAt = this.findPrevious(item);
            if(foundPreviousAt.next != null){
                foundPreviousAt.next = foundPreviousAt.next.next;
            }
        }
        this.findPrevious = function (item) {
            var currNode = this.head;
            var previousNode = null;
            while (currNode.element != item){
                previousNode = currNode;
                currNode = currNode.next;
            }
            return previousNode;
        }
        this.findLast = function () {
            var currNode = this.head;
            while(!(currNode.next == null)){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.display = function () {
            var currNode = this.head;
            while(currNode != null){
                if(currNode.element != 'head'){
                    console.log(currNode.element);
                }
                currNode = currNode.next;
            }
        }
    };
//学生类
    var Student = function (name,score) {
        this.name = name;
        this.score = score;
    };
    //成绩类
    var Grade = function () {};

    Grade.prototype = new LinkedList();

    //输入成绩
    Grade.prototype.input = function (student) {
        var lastNode = this.findLast();
        this.insert(student,lastNode.element);
    }

    var grade = new Grade();
    grade.input(new Student('a',30));
    grade.input(new Student('b',40));
    grade.display();


5.使用双向链表重写例6-4程序

    //双向链表
    var BLinkList = function () {
        this.head = new BNode('head');
        this.find = function (item) {
            var currNode = this.head;
            while(currNode.element != item){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.insert = function (newElement,item) {
            var found = this.find(item);
            var newNode = new BNode(newElement);
            newNode.next = found.next;
            newNode.previous = found;
            found.next = newNode;
        }
        this.remove = function (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;
            }
        }
        this.findLast = function () {
            var currNode = this.head;
            while(!(currNode.next == null)){
                currNode = currNode.next;
            }
            return currNode;
        }
        this.disReverse = function () {
            var currNode = this.findLast();
            while(!(currNode.previous == null)){
                console.log(currNode.element);
                currNode = currNode.previous;
            }
        }
        this.display = function () {
            var currNode = this.head;
            while(currNode != null){
                console.log(currNode.element);
                currNode = currNode.next;
            }
        };
    }
    //双向列表
    var blist = new BLinkList();
    //插入A
    blist.insert('A','head');
    //插入B
    blist.insert('B','A');
    //删除A
    blist.remove('A');
    //只剩下B被显示
    blist.disReverse();


6.传说在公元1世纪的犹太战争中,犹太历史学家费拉奥*约瑟夫和他的40个同胞
    被罗马士兵保卫。犹太士兵决定宁可自杀也不做俘虏,于是商量除了一个自杀方案。
    他们围城一个圈,从一个人开始,数到第三个人时将第三个人杀死,然后再数,直到杀光所有人。
    约瑟夫和另外一个人决定不参加这个疯狂的游戏,他们快速地计算出了两个位置,站在那里得意幸存。
    写一段程序将n个人围城一圈,并且第m个人会被杀掉,计算一圈人中哪两个人最后会存货。使用循环链表
    解决该问题。
   //循环链表
    var CycleChain = function () {
        this.head = new Node('head');
        this.head.next = this.head;
        this.find = function (item) {
            var currNode = this.head;
            while(!(currNode.element == item)){
                currNode = currNode.next;
            }
            return currNode;
        };
        this.insert = function (newElement,item) {
            var found = this.find(item);
            var newNode = new Node(newElement);
            newNode.next = found.next;
            found.next = newNode;
            var lastNode = this.findLast();
            lastNode.next = this.head;
        };
        this.display = function () {
            var currNode = this.head;
            while (!(currNode == null) && !(currNode.next.element == 'head')){
                console.log(currNode.next.element);
                currNode = currNode.next;
            }
        };
        this.findPrevious = function (item) {
            var currNode = this.head;
            var previous = null;
            while(!(currNode.element == item)){
                previous = currNode;
                currNode = currNode.next;
            }
            return previous;
        };
        this.remove= function (item) {
            var foundPrevious = this.findPrevious(item);
            if(!(foundPrevious == null)){
                foundPrevious.next = foundPrevious.next.next;
            }
        };
        this.findLast = function () {
            var currNode = this.head;
            while(!(currNode.next == null) && !(currNode.next.element == 'head')){
                currNode = currNode.next;
            }
            return currNode;
        };
    };


    //杀人游戏类
    var Game = function () {};
    //继承循环链表
    Game.prototype = new CycleChain();
    Game.prototype.currNode = Game.prototype.head;
    //向前移动n个节点
    Game.prototype.advance = function (n) {
        while (n>0){
            this.currNode = this.currNode.next;
            if(this.currNode.element == 'head'){
                this.currNode = this.currNode.next;
            }
            n--;
        }
    };
    //计算幸存者
    Game.prototype.survive = function (n,m) {
        for(var i = 0; i < (n-2); i++){
            this.kill(m);
            this.display();
        }
    }
    //杀掉第m个人
    Game.prototype.kill = function (m) {
        this.advance(m);
        this.remove(this.currNode.element);
    }
    Game.prototype.display = function () {
        var currNode = this.head;
        var arr = [];
        while (!(currNode == null) && !(currNode.next.element == 'head')){
            arr.push(currNode.next.element);
            currNode = currNode.next;
        }
        console.log(arr);
    }
    //A B C D E F
    var game = new Game();
    game.insert('1','head');
    game.insert('2','1');
    game.insert('3','2');
    game.insert('4','3');
    game.insert('5','4');
    game.insert('6','5');
    game.insert('7','6');
    game.insert('8','7');
    game.insert('9','8');
    game.insert('10','9');
    game.display();
    game.survive(10,3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值