Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
A01 Implement Queue with Limited Size of Arrays
/* A01 Implement Queue with Limited Size of Arrays 解法: 用2-D array */ var Queue = function(arraySize){ this.arraySize = arraySize; this.q = [[]] this.used = 0 ; } Queue.prototype.push = function(num){ let len = this.q.length; let arr = this.q[len-1] arr.push(num); this.used++; if(arr.length == this.arraySize){ this.q.push([]); } } Queue.prototype.shift =function(){ if(this.used > 0){ this.used--; let res = this.q[0].shift(); if(this.q[0].length == 0 && this.used >0) { this.q.shift(); // get rid of the empty [] } return res; } else{ return null; } } Queue.prototype.getFront = function(){ if(this.used > 0) return this.q[0][0]; else return null; } // let q = new Queue(3) console.log(q) q.push(1); q.push(2); q.push(3) console.log(q) q.push(4); q.push(5); console.log(q) let x = q.shift() console.log("x=", x) console.log(q) console.log("front = ", q.getFront()); q.shift() q.shift() console.log(q) q.shift() console.log(q) q.shift() q.shift() q.shift() console.log(q )
run
|
edit
|
history
|
help
0
lodash
Get stack from error
Union of two sorted arrays
Rest Params
demo.js
Count digits
Funny stuff
myfirstmodule.js
Include object in a query string
Range List for JavaScript