Coder Social home page Coder Social logo

js-queue's Introduction

Queue

This is a linked-list implementation of a queue.

Multiple arguments can be taken in the constructor to add to the queue.

var q = new Queue();

q.enqueue(5); // 1 (length)
q.enqueue(6); // 2
q.enqueue(7); // 3

q.dequeue(); // 5
q.dequeue(); // 6
q.dequeue(); // 7

.enqueue(...item), .push(...item)

Add elements to the queue using enqueue or its alias push. These can take multiple arguments and will queue the items in FIFO order.

var q = new Queue();

q.enqueue(5, 6, 7); // 3

q.dequeue(); // 5
q.dequeue(); // 6
q.dequeue(); // 7

.dequeue(), .shift()

dequeue and its alias shift removes and returns the first element in the queue.

.unshift(...item)

Adds an item to the front of the queue.

var q = new Queue(5, 6, 7);

q.unshift(1); // 4

q.dequeue(); // 1
q.dequeue(); // 5
q.dequeue(); // 6
q.dequeue(); // 7

.pop()

Removes an item from the end of the queue.

This method is in O(n) time.

var q = new Queue(5, 6, 7);

q.pop(); // 7

.first

Returns the first item in the queue without removing it.

.last

Returns the last item in the queue without removing it.

.length

Returns the number of items in the queue.

Iteratable

Queues are an iterable and iterating them does not affect their contents.

var q = new Queue(1, 2, 3);

for(var item of q) {
  console.log(item); // 1, 2, 3
}

Queue.from([[iterable]])

Queues can be created from an iterable using Queue.from.

var set = new Set([1, 2, 3]);
var q = Queue.from(set);

while(q.length) {
  q.dequeue(); // 1, 2, 3
}

js-queue's People

Contributors

michaeltheriot avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.