JavaScript資料結構——隊列的實現

來源:互聯網
上載者:User

標籤:鏈表   not   console   prot   random   []   取出   dig   不同   

  前面樓主簡單介紹了JavaScript資料結構棧的實現,http://www.cnblogs.com/qq503665965/p/6537894.html,本次將介紹隊列的實現。  

  隊列是一種特殊的線性表,特殊之處在於它只允許在表的前端(front)進行刪除操作,而在表的後端(rear)進行插入操作,和棧一樣,隊列是一種操作受限制的線性表。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。

  隊列的兩種主要操作是:向隊列中插入新元素和刪除隊列中的元素。插入操作也叫做入隊,刪除操作也叫做出隊。入隊操作在隊尾插入新元素,出隊操作刪除隊頭的元素。示範了這兩個操作。

 

  隊列的另外一項重要操作是讀取隊頭的元素。這個操作叫做 peek() 。該操作返回隊頭元素,但不把它從隊列中刪除。除了讀取隊頭元素,我們還想知道隊列中儲存了多少元素,可以使用 length 屬性滿足該需求;要想清空隊列中的所有元素,可以使用 clear() 方法來實現。下表定義了隊列的一些主要方法:

 dataStorage Array 儲存資料的底層資料結構
enqueue int 入隊
dequeue fucntion 出隊
front fucntion 返回隊首元素
back fucntion 返回隊尾元素
toString fucntion   顯示隊列內的所有元素
empty function   判斷隊列是否為空白

  

 

 

 

 

 

 

隊列的實現
 1 function Queue(array) {    2     if (array instanceof Array) { 3         this.dataStorage = array; 4     } else { 5         throw "‘"+array + "‘ is not an Array"; 6     } 7 } 8  9 Queue.prototype = {10     enqueue: function (element) {//向隊列中添加元素11         this.dataStorage.push(element);12     },13     dequeue: function () {//刪除隊首元素14         return this.dataStorage.shift();15     },16     front: function () {//返回隊首元素17         return this.dataStorage[0];18     },19     back: function () {//返回隊尾元素20         return this.dataStorage[this.dataStorage.length - 1];21     },22     toString: function () {//輸出隊列中所有元素23         var result = "";24         for (var i = 0; i < this.dataStorage.length; i++) {25             result += this.dataStorage[i] + "\t";26         }27         return result;28     },29     empty: function () {//判定隊列是否為空白30         if (this.dataStorage.length == 0) {31             return true;32         }33         return false;34     }35 }

  測試:  

1  var queue = new Queue([‘a‘,‘b‘]);       2 console.log(queue.toString());3 //queue.dequeue();//a出隊4 //console.log(queue.toString());5 console.log(queue.front());//隊頭元素6 console.log(queue.back());//隊尾元素7 queue.dequeue();8 queue.dequeue();9 console.log(queue.empty());
隊列的應用——基數排序

  原理:對於 0~99 的數字,基數排序將資料集掃描兩次。第一次按個位上的數字進行排序,第二次按十位上的數字進行排序。每個數字根據對應位上的數值被分在不同的盒子裡。

  假設有這樣一個數組:{4, 73, 51, 76, 70, 19, 63, 64, 43, 12},經過基數排序第一次掃描之後,數字被分配到如下盒子中:  

 1 Box 0: 70 2 Box 1: 51 3 Box 2: 12 4 Box 3: 73,63,43 5 Box 4: 4,64 6 Box 5: 76 7 Box 6:  8 Box 7: 9 Box 8:10 Box 9: 19

  根據盒子的順序,對數字進行第一次排序的結果如下:

  70,51,12,73,63,43,4,64,76,19

  然後根據十位上的數值再將上次排序的結果分配到不同的盒子中:

 1 Box 0: 4 2 Box 1: 12,19 3 Box 2:  4 Box 3:  5 Box 4: 43 6 Box 5: 51 7 Box 6: 63,64 8 Box 7: 70,73,76 9 Box 8: 10 Box 9: 

  最後,將盒子中的數字取出,組成一個新的列表,該列表即為排好序的數字:

  4,12,19,43,51,63,64,70,73,76

  使用隊列代表盒子,可以實現這個演算法。我們需要九個隊列,每個對應一個數字。將所有隊列儲存在一個數組中,使用取餘和除法操作決定個位和十位。演算法的剩餘部分將數字加入相應的隊列,根據個位元值對其重新排序,然後再根據十位上的數值進行排序,結果即為排好序的數字。

  下面是根據相應位(個位或十位)上的數值,將數字分配到相應隊列的方法:  

 1      function allocation(nums,queues,n,digit) { 2             for (var i = 0; i < n; ++i) { 3                 if (digit == 1) { 4                     queues[nums[i] % 10].enqueue(nums[i]); 5                 } 6                 else { 7                     queues[Math.floor(nums[i] / 10)].enqueue(nums[i]); 8                 } 9             }10         }

  下面是從隊列中收集數位方法: 

1     function collection(queues, nums) {2             var i = 0;3             for (var digit = 0; digit < 10; ++digit) {4                 while (!queues[digit].empty()) {5                     nums[i++] = queues[digit].dequeue();6                 }7             }8         }

  測試程式:

 1         var queues = []; 2         for (var i = 0; i < 10; ++i) { 3             queues[i] = new Queue([]); 4         } 5         var nums = []; 6         for (var i = 0; i < 10; ++i) {//目標排序數組 7             nums[i] = Math.floor(Math.floor(Math.random() * 101)); 8         } 9         console.log("排序前:");10         showArray(nums);11         allocation(nums, queues, 10, 1);12         collection(queues, nums);13         allocation(nums,queues,10,10);14         collection(queues, nums);15         console.log("排序後");16         showArray(nums);17 18         function showArray(array) {19             console.log(array);20         }     

  結果:

  下篇樓主將帶來,鏈表的實現。

  本文的範例程式碼地址:https://github.com/LJunChina/JavaScript

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.