<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//基於數組實現列
function Queue(){
//屬性
this.items = []
// 1.將元素加入隊列中
Queue.prototype.enqueue = function(element){
this.items.push(element)
}
// 2.從隊列中刪除前端元素
Queue.prototype.dequeue = function(){
return this.items.shift()
}
// 3.查看前端元素
Queue.prototype.front = function(){
return this.items[0]
}
// 4.查看隊列是否爲空
Queue.prototype.isEmpty = function(){
return this.items.length == 0
}
// 5.查看隊列中元素的個數
Queue.prototype.size = function(){
return this.items.length
}
// 6.toString方法
Queue.prototype.toString = function(){
var resultString = ''
for(var i=0;i<this.items.length;i++){
resultString+=this.items[i]+''
}
return resultString
}
// 方法
}
var queue = new Queue()
// 將元素加入到隊列中
queue.enqueue('abc')
queue.enqueue('111')
alert(queue)
queue.dequeue()
alert(queue)
// 驗證其他方法
alert(queue.isEmpty())
alert(queue.size())
</script>
</body>
</html>
擊鼓傳花的實現方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//基於數組實現列
function Queue(){
//屬性
this.items = []
// 1.將元素加入隊列中
Queue.prototype.enqueue = function(element){
this.items.push(element)
}
// 2.從隊列中刪除前端元素
Queue.prototype.dequeue = function(){
return this.items.shift()
}
// 3.查看前端元素
Queue.prototype.front = function(){
return this.items[0]
}
// 4.查看隊列是否爲空
Queue.prototype.isEmpty = function(){
return this.items.length == 0
}
// 5.查看隊列中元素的個數
Queue.prototype.size = function(){
return this.items.length
}
// 6.toString方法
Queue.prototype.toString = function(){
var resultString = ''
for(var i=0;i<this.items.length;i++){
resultString+=this.items[i]+''
}
return resultString
}
// 方法
}
var queue = new Queue()
// 將元素加入到隊列中
queue.enqueue('abc')
queue.enqueue('111')
alert(queue)
queue.dequeue()
alert(queue)
// 驗證其他方法
alert(queue.isEmpty())
alert(queue.size())
// 擊鼓傳花
function passGame(nameList,num){
// 創建隊列結構
var queue = new Queue()
for(var i=0;i<nameList.length;i++){
queue.enqueue(nameList[i])
}
//開始數數字,
// 不是num的時候,重新加入到隊列的末尾
// 是num這個數字的時候,將其從隊列中刪除
// Num數字之前的人重新放到隊列的末尾
while(queue.size()>1){
for(var i=0;i<num-1;i++){
queue.enqueue(queue.dequeue())
}
//num對應的這個人,直接從隊列中刪除
dequeue.dequeue()
}
}
</script>
</body>
</html>
優先隊列
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>優先隊列</title>
</head>
<body>
<script>
// 封裝優先級隊列
function PriorityQueue(){
PriorityQueue.prototype.enqueue = function(element){
this.items.push(element)
}
// 2.從隊列中刪除前端元素
PriorityQueue.prototype.dequeue = function(){
return this.items.shift()
}
// 3.查看前端元素
PriorityQueue.prototype.front = function(){
return this.items[0]
}
// 4.查看隊列是否爲空
PriorityQueue.prototype.isEmpty = function(){
return this.items.length == 0
}
// 5.查看隊列中元素的個數
PriorityQueue.prototype.size = function(){
return this.items.length
}
// 6.toString方法
PriorityQueue.prototype.toString = function(){
var resultString = ''
for(var i=0;i<this.items.length;i++){
resultString+=this.items[i]+''
}
return resultString
}
//封裝屬性
function QueueElement(element,priority){
this.element = element
this.priority = priority //數據的優先級
}
//封裝屬性
this.items = []
PriorityQueue.prototype.enqueue = function(element,priority){
//創建QueueElement
var queueElement = new QueueElement(element,priority)
// 判斷隊列是否爲空
if(this.items.length==0){
this.items.push(queueElement)
}else{
var added = false
for(var i=0;i<this.items.length;i++){
if(queueElement.priority<this.items[i]){
self.items.splice(i,0,queueElement)
added = true
break
}
}
if(!added){
this.items.push(queueElement)
}
}
}
}
//測試代碼
var pq = new PriorityQueue()
pq.enqueue('abc',111)
pq.enqueue('cnb',11)
pq.enqueue('nbc',123)
pq.enqueue('vbn',9)
console.log('pq',pq)
alert('pq',pq)
</script>
</body>
</html>