// 封装队列(基于数组)
class Queue {
// 属性
items = [];
// 方法
// 1.将元素加入到队列中
enqueue(element: any) {
this.items.push(element);
}
// 2.从队列中删除前端元素
dequeue() {
this.items.shift();
}
// 3.查看队列前端元素
front() {
return this.items[0];
}
// 4.查看队列是否为空
isEmtpy() {
return this.items.length === 0;
}
// 5.查看队列中元素的个数
size() {
return this.items.length;
}
// 6.tostring方法
toString() {
return this.items.toString();
}
}
// 使用队列
const q = new Queue();
// 将元素加入队列
q.enqueue('forward');
q.enqueue('ej');
q.enqueue('xq');
// 返回队列前面元素
console.log(q.front());
// 删除队列中的元素
q.dequeue();