JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3144
  • Score
    100M100P100Q122532F
  • License MIT

stack implementation in javascript

Package Exports

  • @datastructures-js/stack

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/stack) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@datastructures-js/stack

build:? npm npm npm

A wrapper around javascript array push/pop with a standard stack interface.

Table of Contents

Install

npm install --save @datastructures-js/stack

API

require

const Stack = require('@datastructures-js/stack');

import

import Stack from '@datastructures-js/stack';

Create a Stack

empty stack

const stack = new Stack();

// OR

const stack = Stack.fromArray([]);

from an existing array

runtime params
O(1) list: array<object>
const list = [10, 3, 8, 40, 1];

const stack = new Stack(list);

// OR

const stack = Stack.fromArray(list);

If the list should not be mutated, simply construct the stack from a copy of it.

const stack = new Stack(list.slice(0));

.push(element)

push an element to the top of the stack.

runtime params
O(1) element: object
stack.push('test');

.peek()

returns the top element in the stack.

runtime return
O(1) object
console.log(stack.peek()); // test

.pop()

removes and returns the top element of the stack.

runtime return
O(1) object
console.log(stack.pop()); // test
console.log(stack.peek()); // null

.isEmpty()

checks if the stack is empty.

runtime return
O(1) boolean
stack.push('test');
console.log(stack.isEmpty()); // false

.size()

returns the number of elements in the stack.

runtime return
O(1) number
console.log(stack.size()); // 1

.clone()

creates a shallow copy of the stack.

runtime return
O(n) Stack
const stack = Stack.fromArray([{ id: 2 }, { id: 4 } , { id: 8 }]);
const clone =  stack.clone();

clone.pop();

console.log(stack.peek()); // { id: 8 }
console.log(clone.peek()); // { id: 4 }

.toArray()

returns a copy of the remaining elements as an array.

runtime return
O(n) array
console.log(stack.toArray()); // [{ id: 2 }, { id: 4 } , { id: 8 }]

.clear()

clears all elements from the stack.

runtime
O(1)
stack.clear();
stack.size(); // 0

Build

grunt build

License

The MIT License. Full License is here