UNPKG

esds

Version:

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

141 lines (120 loc) 3.12 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: Stack.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: Stack.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; } } /** * Stack Class */ class Stack { constructor() { this.stack = new Node(); this.head = this.stack; this.idx = 0; } /** * Push a new value to the stack * @param {any} val */ push(val) { if (Array.isArray(val)) for (let v of val) this.push(v); else { let temp = new Node(val); temp.next = this.head.next; this.head.next = temp; this.idx++; } } /** * Retrieves and remove next element * @return {any} element */ pop() { let temp = this.head.next?.val ?? null; this.head = this.head.next; this.idx--; return temp; } /** * Peek next element * @return {any} element */ get peek() { return this.head.next?.val ?? null; } /** * Returns number of elements * @return {number} size */ get size() { return this.idx; } /** * Returns true/false if stack is empty * @return {boolean} */ get isEmpty() { return this.idx === 0; } /** * Converts and return an array from stack * @return {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 stack */ clear() { this.head = null; this.idx = 0; } } export { Stack }; </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>