1.Stack
function Stack(){
let item=[];
this.push=(element)=>{
item.push(element);
};
this.pop=()=>{
return item.pop();
};
this.peek=()=>{
return item[item.length-1];
};
this.isempty=()=>{
return item.length==0;
};
this.clear=()=>{
item=[];
}
this.print=()=>{
item.forEach((e)=>{
console.log(e);
});
}
}
2.Queue&priority_queue
function queue(){
let item=[];
this.enqueue=(value:any)=>{
item.push(value);
};
this.dequeue=()=>{
item.shift();
};
this.isEmpty=()=>{
return item.length==0;
};
this.front=()=>{
return item[0];
}
this.print=()=>{
item.forEach((e)=>{
console.log(e);
})
};
}
function priority_queue(){
let item=[];
function queueElement(element,value:Number){
this.element=element;
this.value=value;
}
this.enqueue=(element,value)=>{
let data=new queueElement(element,value);
let added=false;
for(let i=0;i<item.length;i++){
if(data.value<item[i].value){
item.splice(i,0,data);
added=true;
break;
}
}
if(!added){
item.push(data);
}
};
this.print=()=>{
item.forEach((e)=>{
console.log(`值为${e.element},权重为${e.value}`);
})
};
}
网友评论