Deques are a generalization of stacks and queues (the name is pronounced "deck" and is short for "double-ended queue"). -- Python
collections.
npm install @blakeembrey/deque --save
push(x)Addxto right side of the deque.pushLeft(x)Addxto the left side of the deque.clear()Remove all elements from the deque leaving it with length 0.extend(iterable)Extend the right side of the deque by appending elements from iterable.extendLeft(iterable)Extend the left side of the deque by appending elements from iterable.peek(i)Return the element at indexiin the deque.indexOf(x, start?)Return the position ofxin the deque.has(x)Return a boolean indicating whetherxis in the deque.insert(i, x)Insertxinto the deque at positioni.pop()Remove and return an element from the right side of the deque. If no elements are present, throwsRangeError.popLeft()Return and return an element from the left side of the deque. If no elements are present, throwsRangeError.delete(i)Delete the value at positioni.reverse()Reverse the elements of the deque in-place.rotate(n=1)Rotate the dequensteps to the right.
import { Deque } from '@blakeembrey/collections'
const d = new Deque('ghi')
for (const elem of d) {
console.log(elem.toUpperCase()) //=> G H I
}
d.push('j')
d.pushleft('f')
d //=> deque(['f', 'g', 'h', 'i', 'j'])
d.pop() //=> 'j'
d.popleft() //=> 'f'
Array.from(d) //=> ['g', 'h', 'i']
d.peek(0) //=> 'g'
d.peek(-1) //=> 'i'
d.extend('jkl')
d //=> deque(['g', 'h', 'i', 'j', 'k', 'l'])
d.rotate(1)
d //=> deque(['l', 'g', 'h', 'i', 'j', 'k'])
d.rotate(-1)
d //=> deque(['g', 'h', 'i', 'j', 'k', 'l'])This project uses TypeScript and publishes definitions on NPM.
Circular array implementation based on denque.
Apache 2.0