1.0.6 • Published 6 years ago
fixedlengthqueue v1.0.6
FixedLengthQueue
A fixed-length queue with ascending index access.
This module allows one to:
- Add items to the queue using push(item)
- Get items from the queue using get(index)
- Get items in reverse order from the queue using getReverse(index)
- Get the length of the queue using size()
Example
Installing the module:
npm install --save fixedlengthqueue
Importing the module:
const FixedLengthQueue = require('fixedlengthqueue')
A fixed-length queue with length 10 can be constructed using
var fixedLengthQueue = new FixedLengthQueue(10)
Adding items to the queue:
Just call the function push(item)
. Any older items will be overwritten.
for (var i=0; i<50; i++) {
fixedLengthQueue.push("Item " + i)
}
Getting items from the queue:
Just call the function get(index)
for (var i=0; i<fixedLengthQueue.size(); i++) {
console.log("Item at index " + i + " has content: " + fixedLengthQueue.get(i))
}
Getting items in reverse order from the queue:
Just call the function getReverse(index)
for (var i=0; i<fixedLengthQueue.size(); i++) {
console.log("Item at index " + i + " has content: " + fixedLengthQueue.getReverse(i))
}
Getting the length of the queue:
Just call the function size()
console.log("Length of the queue: " + fixedLengthQueue.size())
Output (using above fixed-length 10, pushing 50 items and get() 10 items from index 0 to 9):
Item 40
Item 41
Item 42
Item 43
Item 44
Item 45
Item 46
Item 47
Item 48
Item 49
Output (using above fixed-length 10, pushing 50 items and getReverse() 10 items from index 0 to 9):
Item 49
Item 48
Item 47
Item 46
Item 45
Item 44
Item 43
Item 42
Item 41
Item 40