队列的实现

来源:互联网 发布:网络代理可信吗 编辑:程序博客网 时间:2024/06/03 20:41

同样使用数组来模拟队列


/*Queue class*/function Queue() {var items = [];/*入队*/this.enqueue = function (value) {items.push(value);} /*出队,出列*/this.dequeue = function () {return items.shift();}/*返回队列的第一个元素*/this.front = function () {return items[0];}/*判断队列是否为空*/this.isEmpty = function () {return items.length == 0;}/*清空队列*/this.clear = function () {items = [];}/*判断队列长度*/this.size = function () {return items.length;}/*打印队列*/this.print = function () {console.log(items);}}


原创粉丝点击