Module
x/datastructure/stack/stack.ts
Implement different Data Structures using TypeScript. Deno Third-party Module.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import { StackType, DataType, StackApi } from "./helper.d.ts"import { StackNode } from "./stackNode.ts"import { searchGenerator, updateGenerator, iteratorGenerator} from "./generators.ts"
export class Stack implements StackApi { #topNode: StackType #bottomNode: StackType public size: number
constructor() { this.#topNode = null this.#bottomNode = null this.size = 0 }
getTop() { if (this.#topNode === null) return null; return this.#topNode!.data }
getBottom() { if (this.#topNode === null) return null; return this.#bottomNode!.data }
push(data: DataType<any>) { const newNode = new StackNode(data) if (this.#topNode === null) { this.#topNode = newNode this.#bottomNode = newNode return true; } newNode.next = this.#topNode if (newNode.next === null) { this.#bottomNode = newNode.next } this.#topNode = newNode this.size++
return true; }
pop() { if (this.#topNode === null) { return false; } else if (this.#topNode!.next === null) { this.#bottomNode = null } const node = this.#topNode this.#topNode = node!.next this.size-- if (this.#topNode === null) this.size = 0
return node!.data; }
search(key: string|number) { if (this.#topNode === null) { return false }
if (key === this.#topNode!.data.key) { return this.#topNode!.data } if (key === this.#bottomNode!.data.key) { return this.#bottomNode!.data }
// generator function that returns an iterator const iterator = searchGenerator(key, this.#topNode) const iteratorNext = iterator.next() if (iteratorNext.value) { return iteratorNext.value }
return false }
update(key: string|number, newValue: any) { if (this.#topNode === null) { return false }
if (key === this.#topNode!.data.key) { this.#topNode!.data.value = newValue return this.#topNode!.data } if (key === this.#bottomNode!.data.key) { this.#bottomNode!.data.value = newValue return this.#bottomNode!.data }
// generator function that returns an iterator const iterator = updateGenerator(key, this.#topNode, newValue) const iteratorNext = iterator.next() if (iteratorNext.value) { return iteratorNext.value }
return false }
log() { let currentNode = this.#topNode while (currentNode !== null) { console.log(currentNode.data); currentNode = currentNode.next } }
iterator() { // generator function that returns an iterator const iterator = iteratorGenerator(this.#topNode) return iterator }}