JavaScript Data Structures and Algorithms - Queue Details (Part 2)

Next, I will implement an application with the help of the queue class and its methods implemented in my other article, JavaScript data structure and algorithm - Queue Detailed Explanation (Part 1) .

pairing problem

Demand analysis: A file of mixed gender data is stored in a file. The name begins with B for men and G for women. Read out the men and women in the file, put them into a queue, and pair them in pairs (such as queues). form two teams, matching male and female friends), until the number of one party is 0, if both are matched successfully, it will display "all matching completed", if no one is successfully matched, it will display the gender of the unmatched person and its name.

document preparation

Prepare a document containing both men and women, what I have prepared is people.txt, the content format is as follows:

G 小花1
G 小花2
B 小明1
B 小明2
B 小明3
B 小明4
G 小花3
B 小明5
G 小花4
B 小明6
G 小花5

Define a Person object and save the information of the person to be matched

function Person(name, sex) {
    this.name = name
    this.sex = sex
}

Read file, assign enqueue

To read the file, we can read the file with the help of the file system of nodejs fs, and then use the conversion function between strings and arrays to realize the division of names, and then save them into their respective queues by men and women.

function getPerson(boysQueue, girlsQueue) {
    // 读取包含性别的名字
    var names = fs.readFileSync('people.txt')
    // 分行保存
    var namesArray = names.toString().split('\n')
    // 取出末尾空格
    for (var i = 0; i < namesArray.length; i++) {
        namesArray[i] = namesArray[i].trim()
    }
    // 性别与名称分割,并按性别入队
    for (var i = 0; i < namesArray.length; i++) {
        var sexAndName = namesArray[i].split(' ')
        var sex = sexAndName[0]
        var name = sexAndName[1]
        if (sex === 'B') {
            boysQueue.enQueue(new Person(name, sex))
        } else {
            girlsQueue.enQueue(new Person(name ,sex))
        }
    }
}

Implement the pairing method

function match(boysQueue, girlsQueue) {
    var currBoy, currGirl
    while (!boysQueue.empty() && !girlsQueue.empty()) {
        currBoy = boysQueue.deQueue()
        currGirl = girlsQueue.deQueue()
        console.log(currBoy.name + '与' + currGirl.name + '配对成功啦!!!')
    }
    if (boysQueue.count() > 0) {
        console.log('有' + boysQueue.count() + '个男士缺少女士配对!!')
    }
    if (girlsQueue.count() > 0) {
        console.log('有' + girlsQueue.count() + '个女士缺少男士配对!!')
    }
    if ((girlsQueue.count() === 0) && (boysQueue.count() === 0)) {
        console.log('恭喜!!刚好全部配对完毕,不存在有人单身啦!!!')
    }
}

This is relatively simple and will not be commented. If you have any doubts, you can understand it by studying the code.

test case

var boysQueue = new Queue()
var girlsQueue = new Queue()
getPerson(boysQueue, girlsQueue)
match(boysQueue, girlsQueue)

The complete code integrated with the previous code queue.jsis as follows

// Created by xiaoqiang on 07/04/2018.
var fs = require('fs')
function Queue() {
    this.data = []            // 存放数据
    this.enQueue = enQueue    // 入队操作
    this.deQueue = deQueue    // 出队操作
    this.first = first        // 访问第一个元素
    this.last = last          // 访问最后一个元素
    this.toString = toString  // 显示队列中的数据
    this.empty = empty        // 清空队列数据
    this.count = count        // 显示队列当前元素数量
}

function enQueue(param) {
    this.data.push(param)
    return this
}

function deQueue() {
    return this.data.shift()
}

function first() {
    return this.data[0]
}

function last() {
    return this.data[this.data.length - 1]
}

function toString() {
    var str = ''
    for (var i = 0; i < this.data.length; i++) {
        str += this.data[i] + '\n'
    }
    return str
}

function empty() {
    if (this.data.length === 0) {
        return true
    } else {
        return false
    }
}

// 以下为下篇代码
function count() {
    return this.data.length
}

function Person(name, sex) {
    this.name = name
    this.sex = sex
}

function getPerson(boysQueue, girlsQueue) {
    // 读取包含性别的名字
    var names = fs.readFileSync('people.txt')
    // 分行保存
    var namesArray = names.toString().split('\n')
    // 取出末尾空格
    for (var i = 0; i < namesArray.length; i++) {
        namesArray[i] = namesArray[i].trim()
    }
    // 性别与名称分割,并按性别入队
    for (var i = 0; i < namesArray.length; i++) {
        var sexAndName = namesArray[i].split(' ')
        var sex = sexAndName[0]
        var name = sexAndName[1]
        if (sex === 'B') {
            boysQueue.enQueue(new Person(name, sex))
        } else {
            girlsQueue.enQueue(new Person(name ,sex))
        }
    }
}

function match(boysQueue, girlsQueue) {
    var currBoy, currGirl
    while (!boysQueue.empty() && !girlsQueue.empty()) {
        currBoy = boysQueue.deQueue()
        currGirl = girlsQueue.deQueue()
        console.log(currBoy.name + '与' + currGirl.name + '配对成功啦!!!')
    }
    if (boysQueue.count() > 0) {
        console.log('有' + boysQueue.count() + '个男士缺少女士配对!!')
    }
    if (girlsQueue.count() > 0) {
        console.log('有' + girlsQueue.count() + '个女士缺少男士配对!!')
    }
    if ((girlsQueue.count() === 0) && (boysQueue.count() === 0)) {
        console.log('恭喜!!刚好全部配对完毕,不存在有人单身啦!!!')
    }
}
// test

var boysQueue = new Queue()
var girlsQueue = new Queue()
getPerson(boysQueue, girlsQueue)
match(boysQueue, girlsQueue)

run and results

Used in the node environment node queue.js, the running results are as follows:
write picture description here
Combined with the previous article and this article, a JavaScript-implemented queue class has been implemented, and a complete matching application has been implemented. So far, the JavaScript data structure and algorithm - the detailed explanation of the queue is over, generally Writing late at night, there may be some mistakes, welcome to point out, thank you O(∩_∩)O~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472834&siteId=291194637