This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStack() { | |
const empty = { next: undefined, value: undefined! }; | |
let top = empty; | |
function isEmpty() { return top === empty; } | |
function push(v) { top = { next: top, value: v }; } | |
function pop() { | |
if (!top.next) throw new Error('Can not pop from empty'); | |
const { value } = top; | |
top = top.next; | |
return value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createQueue() { | |
let head = undefined; | |
let tail = head; | |
function isEmpty() { return head === undefined; } | |
function push(value) { | |
const newElement = { next: undefined, value }; | |
if (!tail) { | |
head = newElement; | |
tail = newElement; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TS | |
function test<T extends unknown>(actual: T, expected: T, text?: string): void { | |
function difference<D>(a: Set<D>, b: Set<D>) { | |
const arr: D[] = []; | |
a.forEach(d => arr.push(d)); | |
return new Set<D>(arr.filter(x => !b.has(x))); | |
} | |
function same<S extends unknown>(o1: S, o2: S): boolean { | |
const t1 = typeof o1; | |
const t2 = typeof o2; |
OlderNewer