Created
May 19, 2021 15:28
-
-
Save fzn0x/10885a90a3626ff95446beab9b2d5b2c to your computer and use it in GitHub Desktop.
FIFO Algorithm in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// define the queue class | |
class Queue { | |
constructor(...elements) { | |
// set array as value of construct args | |
this.elements = [...elements]; | |
} | |
push(...args) { | |
// push arguments to this.elements | |
return this.elements.push(...args); | |
} | |
shift() { | |
// you can also use splice | |
//return this.elements.splice(0,1)[0]; | |
return this.elements.shift(); | |
} | |
// access method as property (modern JS Engine) | |
get length(){ | |
return this.elements.length; | |
} | |
// set method in property (modern JS Engine) | |
set length(length){ | |
return this.elements.length = length; | |
} | |
} | |
const q = new Queue (5,8) ; | |
q.push(9); | |
console.log(q.length); | |
while(q.length) | |
console.log(q.shift()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FIFO (First In First Out) adalah teknik untuk mengatur persediaan barang yang merupakan hasil dari keputusan manajerial dalam kegiatan normal perusahaan, di mana barang yang pertama masuk berarti barang tersebutlah yang pertama harus keluar.