zoukankan      html  css  js  c++  java
  • 数据结构与算法 --- js描述队列

    js描述队列

    • 队列的特性是只能在队尾插入元素,在队首删除元素,先进先出;
    • 队列被用在很多地方,比如提交操作系统执行的一系列进程,打印任务池,模拟现实中的排队;
    //队列类
    function Queue (){
        this.dataSource=[];
        this.enqueue=enqueue;
        this.dequeue=dequeue;
        this.front=front;
        this.back=back;
        this.toString=toString;
        this.empty=empty;
    }
    //向对尾添加元素
    function enqueue(element){
        this.dataSource.push(element);
    }
    //删除队首元素
    function dequeue(){
        return this.dataSource.shift();
    }
    //返回队首元素
    function front (){
        return this.dataSource[0];
    }
    //返回队尾元素
    function back (){
        return this.dataSource[this.dataSource.length-1];
    }
    //将所有元素转化为字符串
    function toString(){
        var str='';
        for(var i=0;i<this.dataSource.length;i++){
            str+=this.dataSource+"
    ";
        }
        return str;
    }
    //判断队列是否为空
    function empty(){
        if(this.dataSource.length==0){
            return true;
        }else{
            return false;
        }
    }
    

    队列的实际运用

    • 解决男女配对跳舞问题
    var people=[
        {
            name:'frank',
            sex:'man',
        },{
            name:'rose',
            sex:'woman',
        },{
            name:'even',
            sex:'man',
        },{
            name:'xiaowang',
            sex:'man',
        },{
            name:'xiaoliu',
            sex:'man',
        },{
            name:'xiaohua',
            sex:'woman',
        },{
            name:'xiaoqiang',
            sex:'man',
        },{
            name:'xiaoli',
            sex:'woman',
        },{
            name:'xiaomei',
            sex:'woman',
        }
    ];
    function Dencer(name,sex){
        this.name=name;
        this.sex=sex;
    }
    function getManDencer(){
        var man=new Queue();
        var woman=new Queue();
        for(var i=0;i<people.length;i++){
            if(people[i].sex=='man'){
                var manDencer=new Dencer(people[i].name,people[i].sex);
                man.enqueue(manDencer);
            }else{
                var womanDencer=new Dencer(people[i].name,people[i].sex);
                woman.enqueue(womanDencer);
            }
        }
        dancer(man,woman);
    }
    function dancer(man,woman){
        while (!man.empty() && !woman.empty()){
            manPeople=man.dequeue();
            womanPeople=woman.dequeue();
            console.log(manPeople.name+">>>>>>>>>>>>>>"+womanPeople.name);
        };
    }
    getManDencer()
    

    github:https://github.com/Frankltf/js-queue/tree/features-one

  • 相关阅读:
    [Selenium]Eclipse hangs at 57% in debug mode with TestNG tests
    [Selenium] CSS3 选择器
    [Selenium]如何实现上传本地文件
    [Selenium]显式等待 Explicit wait & 隐式等待 Implicit wait
    [Selenium]中使用css选择器进行元素定位
    [Selenium]验证点了某个Button之后无反应
    7. Debug on local machine
    6. Manage the driver for browser and the script for Hub
    4. Configure maven in Spring Tool Suite
    3. Install Spring-Tool-Suite & TestNG
  • 原文地址:https://www.cnblogs.com/frankltf/p/7574089.html
Copyright © 2011-2022 走看看