<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>优先级队列</title>
</head>
<body>
<script>
function ProorityQueue(){
this.items=[]
ProorityQueue.prototype.push=function(element){
this.items.push(element)
}
ProorityQueue.prototype.shift=function(){
return this.items.shift()
}
ProorityQueue.prototype.front=function(){
return this.items[0]
}
ProorityQueue.prototype.isEmpty=function(){
return this.items.length==0
}
ProorityQueue.prototype.size=function(){
return this.items.length
}
ProorityQueue.prototype.toString=function(){
var resultString=""
for(var i=0;i<this.items.length;i++){
resultString+=this.items[i].element+"-"+this.items[i].priority
}
return resultString
}
}
function ProorityQueue(){
function QueueElement(element,priority){
this.element=element
this.priority=priority
}
//封装属性‘
this.items=[]
//实现输入方法
ProorityQueue.prototype.enqueue=function(element,priority){
var element=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++){
for(var i=0;i<this.items.length;i++){
if(queueElement.priority<this.items[i].priority){
this.items.splice(i,0,queueElement)
added=true
break
}
}
if(!added){
this.items.push(queueElement)
}
}
}
}
}
</script>
</body>
</html>