这是我的Que类,但我对对象不是很熟悉,所以获取对象的大小和元素对我不起作用
class Queue{
// Array is used to implement a Queue
constructor(){
this.items = {};
this.count = 0;
}
// enqueue(item)
enqueue(element){
// adding element to the queue
this.items.push(element);
this.count++;
}
// dequeue()
dequeue(){
// removing element from the queue
// returns underflow when called
// on empty queue
if(this.isEmpty())
return "Underflow";
this.count--;
return this.items.shift();
}
// front()
front(){
// returns the Front element of
// the queue without removing it.
if(this.isEmpty())
return "No elements in Queue";
return this.items[0];
}// isEmpty()
isEmpty(){
// return true if the queue is empty.
return this.items.length == 0;
}
// peek()
peek(){
return this.items[this.count]
}
size(element){
return this.count;
}
show(){
var result = '';
var i=this.count-1;
for(i; i>=0; i--){
result += this.items[i] +' ';
}
return result;
}
}像size()、show()和isEmpty()这样的方法不能工作吗?它们返回未定义的。
https://stackoverflow.com/questions/52505685
复制相似问题