UNPKG

esds

Version:

ES6 JS lightweight data structures (Priority Queue, Binary Search Tree (BST), Graph, Bloom Filters, Trie, Queue, Stack, Linked-List)

140 lines (120 loc) 3.13 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: Queue.mjs</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: Queue.mjs</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** * Represents an element in the List. */ class Node { /** * @param {any} [val] - The value of the element */ constructor(val) { this.val = val ?? null; this.next = null; } } /** * Queue Class */ class Queue { constructor() { this.queue = new Node(); this.head = this.queue; this.tail = this.queue; this.idx = 0; } /** * Add a new value to the queue * @param {any} val */ add(val) { if (Array.isArray(val)) for (let v of val) this.add(v); else { this.tail.next = new Node(val); this.tail = this.tail.next; this.idx++; } } /** * Retrieves and remove next element * @returns {any} element */ poll() { let temp = this.head.next?.val ?? null; this.head = this.head.next; this.idx--; return temp; } /** * Peek next element * @returns {any} element */ get peek() { return this.head.next?.val ?? null; } /** * Returns number of elements * @returns {number} size */ get size() { return this.idx; } /** * Returns true/false if queue is empty * @returns {boolean} */ get isEmpty() { return this.idx === 0; } /** * Converts and return an array from queue * @returns {Array} */ toArray() { let curr = this.head.next; const arr = []; while (curr) { arr.push(curr.val); curr = curr.next; } return arr; } /** * Removes all the elements in the queue */ clear() { this.head = null; this.idx = 0; } } export { Queue }; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BloomFilter.html">BloomFilter</a></li><li><a href="BST.html">BST</a></li><li><a href="Element.html">Element</a></li><li><a href="Graph.html">Graph</a></li><li><a href="List.html">List</a></li><li><a href="Node.html">Node</a></li><li><a href="PriorityQueue.html">PriorityQueue</a></li><li><a href="Queue.html">Queue</a></li><li><a href="Stack.html">Stack</a></li><li><a href="TreeElement.html">TreeElement</a></li><li><a href="Trie.html">Trie</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Thu Sep 23 2021 00:49:18 GMT-0700 (Pacific Daylight Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>