1.0.1 • Published 2 years ago

spark-data-structures v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

A javascript implementation of Stack, Queue, LinkedList & DoublyLinkedList with typescript support

install

npm i spark-data-structures --save

require

const { Stack, Queue, LinkedList } = require('spark-data-structures');

import

import { Stack, Queue, LinkedList } from 'spark-data-structures';

Stack Implementation ways in javascript

// empty stack
const stack = new Stack();

// from an array
const stack = new Stack([10, 3, 8, 40, 1]);

// with elements
const list = [10, 3, 8, 40, 1];
const stack = Stack.fromArray(list);

// If the list should not be mutated, use a copy of it.
const stack = Stack.fromArray(list.slice());

// adds an element to the top of the stack.
stack.push(11);

// returns the top element in the stack.
console.log(stack.peek()); // 11

// removes and returns the top element of the stack.
console.log(stack.pop()); // 11
console.log(stack.peek()); // null

// checks if the stack is empty.
stack.push(11);
console.log(stack.isEmpty()); // false

// returns the number of elements in the stack.
console.log(stack.size()); // 1


// creates a shallow copy of the 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 }

// returns a copy of the remaining elements as an array.

console.log(stack.toArray()); // [{ id: 2 }, { id: 4 } , { id: 8 }]

// clears all elements from the stack.

stack.clear();
stack.size(); // 0

Queue Implementation ways in javascript

// empty queue
const queue = new Queue();

// from an array
const queue = new Queue([1, 2, 3]);

// empty queue
const queue = Queue.fromArray([]);

// with elements
const list = [10, 3, 8, 40, 1];
const queue = Queue.fromArray(list);

// If the list should not be mutated, use a copy of it.
const queue = Queue.fromArray(list.slice());

// adds an element to the back of the queue.

queue.enqueue(10).enqueue(20); // or queue.push(123)

// peeks on the front element of the queue.

console.log(queue.front()); // 10

// peeks on the back element in the queue.

console.log(queue.back()); // 20
dequeue (pop);

// removes and returns the front element of the queue.

console.log(queue.dequeue()); // 10 // or queue.pop()
console.log(queue.front()); // 20
// Dequeuing all elements takes O(n*log(n)) instead of O(n2) when using shift/unshift with arrays.

// dequeuing 1 million elements in Node v12

dequeue	shift
~40 ms	~3 minutes

// checks if the queue is empty.

console.log(queue.isEmpty()); // false

// returns the number of elements in the queue.

console.log(queue.size()); // 1

// creates a shallow copy of the queue.

const queue = Queue.fromArray([{ id: 2 }, { id: 4 } , { id: 8 }]);
const clone =  queue.clone();

clone.dequeue();

console.log(queue.front()); // { id: 2 }
console.log(clone.front()); // { id: 4 }

// returns a copy of the remaining elements as an array.

queue.enqueue(4).enqueue(2);
console.log(queue.toArray()); // [20, 4, 2]

// clears all elements from the queue.

queue.clear();
queue.size(); // 0