資料結構與演算法JavaScript描述——使用隊列

來源:互聯網
上載者:User

標籤:儲存   一個人   queue類   view   while   資料   hid   使用   color   

1.使用隊列:方塊舞的舞伴分配問題                                    

前面我們提到過,經常用隊列類比排隊的人。下面我們使用隊列來類比跳方塊舞的人。當男男女女來到舞池,他們按照自己的性別排成兩隊。當舞池中有地方空出來時,選兩個隊列中的第一個人組成舞伴。他們身後的人各自向前移動一位,變成新的隊首。當一對舞伴邁入舞池時,主持人會大聲喊出他們的名字。當一對舞伴走出舞池,且兩排隊伍中有任意一隊沒人時,主持人也會把這個情況告訴大家。為了類比這種情況,我們把跳方塊舞的男男女女的姓名儲存在一個文字檔中: 下面是程式碼的實現:
<script type="text/javascript">function Queue(){    this.dataStore = [];    this.enqueue = enqueue;    this.dequeue = dequeue;    this.front = front;    this.back = back;    this.toString = toString;    this.empty = empty;    this.count = count;}/***    向隊尾添加一個元素*/function enqueue(element){    this.dataStore.push(element);}/***    刪除隊首的元素:*/function dequeue(){    this.dataStore.shift();}/***    讀取隊首的元素:*/function front(){    return this.dataStore[0];}/***    讀取隊尾的元素:*/function back(){    return this.dataStore[this.dataStore.length - 1];}/***    顯示隊列內的所有元素*/function toString(){    var retStr = "";    for (var i = 0; i < this.dataStore.length; ++i) {        retStr += this.dataStore[i] + "\n";    }    return retStr;}/***    判斷隊列是否為空白*/function empty(){    if(this.dataStore.length == 0){        return true;    }else{        return false;    }}/***    顯示隊列中有多少個元素*/function count(){    return this.dataStore.length;}//===================================使用Queue類=============================================/***    每個舞者資訊都被儲存在一個Dancer 對象中*/function Dancer(name, sex) {    this.name = name;    this.sex = sex;}/***    將舞者資訊從檔案中讀到程式裡來*    trim() 函數除去了每行字串後的空格*    根據性別,將舞者加入不同的隊列*/function getDancers(males, females){    var names = read("dancers.txt").split("\n");    for (var i = 0; i < names.length; ++i) {        names[i] = names[i].trim();    }    for (var i = 0; i < names.length; ++i) {        var dancer = names[i].split(" ");        var sex = dancer[0];        var name = dancer[1];        if (sex == "F") {            females.enqueue(new Dancer(name, sex));        }else{            males.enqueue(new Dancer(name, sex));        }    }}/***    將男性和女性組成舞伴,並且宣布配對結果*/function dance(males, females){    console.log("The dance partners are: \n");    while (!females.empty() && !males.empty()) {        person = females.dequeue();        console.log("Female dancer is: " + person.name);        person = males.dequeue();        console.log(" and the male dancer is: " + person.name);    }}/***測試程式:*/var maleDancers = new Queue();var femaleDancers = new Queue();getDancers(maleDancers, femaleDancers);dance(maleDancers, femaleDancers);if (!femaleDancers.empty()) {    print(femaleDancers.front().name + " is waiting to dance.");}if (!maleDancers.empty()) {    print(maleDancers.front().name + " is waiting to dance.");}//顯示等候跳舞的人數if (maleDancers.count() > 0) {    print("There are " + maleDancers.count() +" male dancers waiting to dance.");}if (femaleDancers.count() > 0) {    print("There are " + femaleDancers.count() +" female dancers waiting to dance.");}</script>
View Code

 

 

2.

 

資料結構與演算法JavaScript描述——使用隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.