├── .gitignore
├── .vscode
├── launch.json
└── settings.json
├── LICENSE
├── README.md
├── blockChain
├── blockChain.bundle.js
├── blockChain.ts
├── blockNode.ts
├── generators.ts
├── helper.d.ts
├── readme.md
└── test.ts
├── deploy.sh
├── hashTable
├── generators.ts
├── hashFunction.ts
├── hashNode.ts
├── hashTable.bundle.js
├── hashTable.ts
├── helper.d.ts
├── readme.md
└── test.ts
├── linkedList
├── doubly
│ ├── doublyLinkedList.bundle.js
│ ├── doublyLinkedList.ts
│ ├── doublyNode.ts
│ ├── generators.ts
│ ├── helper.d.ts
│ ├── readme.md
│ └── test.ts
└── singly
│ ├── generators.ts
│ ├── helper.d.ts
│ ├── readme.md
│ ├── singlyLinkedList.bundle.js
│ ├── singlyLinkedList.ts
│ ├── singlyNode.ts
│ └── test.ts
├── mod.js
├── mod.ts
├── queue
├── generators.ts
├── helper.d.ts
├── queue.bundle.js
├── queue.ts
├── queueNode.ts
├── readme.md
└── test.ts
└── stack
├── generators.ts
├── helper.d.ts
├── readme.md
├── stack.bundle.js
├── stack.ts
├── stackNode.ts
└── test.ts
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 | yarn-debug.log*
6 | yarn-error.log*
7 | lerna-debug.log*
8 |
9 | # Diagnostic reports (https://nodejs.org/api/report.html)
10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11 |
12 | # Runtime data
13 | pids
14 | *.pid
15 | *.seed
16 | *.pid.lock
17 |
18 | # Directory for instrumented libs generated by jscoverage/JSCover
19 | lib-cov
20 |
21 | # Coverage directory used by tools like istanbul
22 | coverage
23 | *.lcov
24 |
25 | # nyc test coverage
26 | .nyc_output
27 |
28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29 | .grunt
30 |
31 | # Bower dependency directory (https://bower.io/)
32 | bower_components
33 |
34 | # node-waf configuration
35 | .lock-wscript
36 |
37 | # Compiled binary addons (https://nodejs.org/api/addons.html)
38 | build/Release
39 |
40 | # Dependency directories
41 | node_modules/
42 | jspm_packages/
43 |
44 | # TypeScript v1 declaration files
45 | typings/
46 |
47 | # TypeScript cache
48 | *.tsbuildinfo
49 |
50 | # Optional npm cache directory
51 | .npm
52 |
53 | # Optional eslint cache
54 | .eslintcache
55 |
56 | # Microbundle cache
57 | .rpt2_cache/
58 | .rts2_cache_cjs/
59 | .rts2_cache_es/
60 | .rts2_cache_umd/
61 |
62 | # Optional REPL history
63 | .node_repl_history
64 |
65 | # Output of 'npm pack'
66 | *.tgz
67 |
68 | # Yarn Integrity file
69 | .yarn-integrity
70 |
71 | # dotenv environment variables file
72 | .env
73 | .env.test
74 |
75 | # parcel-bundler cache (https://parceljs.org/)
76 | .cache
77 |
78 | # Next.js build output
79 | .next
80 |
81 | # Nuxt.js build / generate output
82 | .nuxt
83 | dist
84 |
85 | # Gatsby files
86 | .cache/
87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js
88 | # https://nextjs.org/blog/next-9-1#public-directory-support
89 | # public
90 |
91 | # vuepress build output
92 | .vuepress/dist
93 |
94 | # Serverless directories
95 | .serverless/
96 |
97 | # FuseBox cache
98 | .fusebox/
99 |
100 | # DynamoDB Local files
101 | .dynamodb/
102 |
103 | # TernJS port file
104 | .tern-port
105 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "name": "Data Structure Test",
9 | "type": "pwa-node",
10 | "request": "launch",
11 | // "program": "${workspaceFolder}/linkedList/dubbly/test.ts",
12 | "cwd": "${workspaceFolder}",
13 | "runtimeExecutable": "deno",
14 | "runtimeArgs": [ "run", "--inspect-brk", "-A", "${file}" ],
15 | "attachSimplePort": 9229
16 | }
17 | ]
18 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "deno.enable": true
3 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 Keramot UL Islam
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Data Structure
2 |
3 | Different Data Structures implementation API provided by this module.
4 | API available in TypeScript & JavaScript[ES6].
5 |
6 | This library is also available as **[Deno Third party Module](https://deno.land/x/datastructure)**
7 |
8 | If you're already using this module in your Deno project,
9 | then run deno run -r projectName/fileName.ts
for latest version.
10 |
11 |
12 |
13 |
14 | ## List of Data Structures:
15 |
16 | * Block Chain -- **[Documentation](https://deno.land/x/datastructure/blockChain#blockChain-api)**
17 | * Hash Table -- **[Documentation](https://deno.land/x/datastructure/hashTable#hash-table-api)**
18 | * Singly Linked List -- **[Documentation](https://deno.land/x/datastructure/linkedList/singly#singly-linked-list-api)**
19 | * Doubly Linked List -- **[Documentation](https://deno.land/x/datastructure/linkedList/doubly#doubly-linked-list-api)**
20 | * Stack -- **[Documentation](https://deno.land/x/datastructure/stack#stack-api)**
21 | * Queue -- **[Documentation](https://deno.land/x/datastructure/queue#queue-api)**
22 |
23 |
24 |
25 | .
--------------------------------------------------------------------------------
/blockChain/blockChain.ts:
--------------------------------------------------------------------------------
1 | import { BlockChainApi, BlockType, DataType } from "./helper.d.ts";
2 | import { searchGenerator, iteratorGenerator } from "./generators.ts"
3 | import { Block } from "./blockNode.ts";
4 |
5 | export class BlockChain implements BlockChainApi {
6 | #chain: Array
7 | length: number
8 |
9 | constructor() {
10 | this.#chain = new Array()
11 | this.length = this.#chain.length
12 | }
13 |
14 | static createBlockChain() {
15 | return new this();
16 | }
17 |
18 | createBlock(data: DataType) {
19 | const newBlock = new Block(data, this.#chain.length + 1)
20 |
21 | if (this.#chain.length === 0) {
22 | this.#chain.push(newBlock)
23 | this.length++
24 |
25 | return true
26 | }
27 |
28 | if (this.#chain.length > 0) {
29 | const prevBlock = this.latestBlock()
30 | newBlock.prevHash = prevBlock!.hash
31 | this.#chain.push(newBlock)
32 | this.length++
33 |
34 | return true
35 | }
36 |
37 | return false
38 | }
39 |
40 | search(key: null|string, index: null|number = null) {
41 | if (this.#chain.length === 0) {
42 | return false
43 | }
44 |
45 | if (key) {
46 | const iterator = searchGenerator(key, this.#chain)
47 | let iteratorNext = iterator.next()
48 | if (iteratorNext.value) {
49 | return iteratorNext.value
50 | }
51 | }
52 |
53 | if (index) {
54 | return this.#chain[index - 1]
55 | }
56 |
57 | return false;
58 | }
59 |
60 | latestBlock() {
61 | if (this.#chain.length <= 0) return false
62 | return this.#chain[this.#chain.length - 1]
63 | }
64 |
65 | iterator() {
66 | return iteratorGenerator(this.#chain)
67 | }
68 |
69 | checkValidation() {
70 | const iterator = this.iterator()
71 | let iteratNext = iterator.next()
72 | let prevHash: null|string = null
73 | let prevBlock: null|BlockType = null
74 |
75 | while (iteratNext.value) {
76 | const block = iteratNext.value
77 | const regeneratedHash = block.regenerateHash()
78 | if (block.hash !== regeneratedHash) {
79 | console.error(`index: ${block.index}, block is corrupted`)
80 | return false
81 | }
82 | if (prevBlock != null && block.prevHash !== prevHash) {
83 | console.error(`index: ${prevBlock!.index}, block is corrupted`)
84 | return false
85 | }
86 |
87 | prevHash = block.regenerateHash()
88 | prevBlock = block
89 | iteratNext = iterator.next()
90 | }
91 |
92 | console.log('The Chain is valid.')
93 | return true
94 | }
95 |
96 | log(key: null|string = null, index: null|number = null) {
97 | if (key) {
98 | return console.log(this.search(key))
99 | }
100 |
101 | if (index) {
102 | return console.log(this.#chain[index - 1])
103 | }
104 |
105 | console.log(this.#chain)
106 | }
107 |
108 | }
109 |
--------------------------------------------------------------------------------
/blockChain/blockNode.ts:
--------------------------------------------------------------------------------
1 | import { DataType, BlockType } from "./helper.d.ts";
2 | import { createHash } from "https://deno.land/std/hash/mod.ts";
3 |
4 | function blockHash(index: number, data: DataType, time: Date) {
5 | let hash = createHash("sha256")
6 | hash = hash.update(JSON.stringify(data) + index + time)
7 |
8 | return hash.toString();
9 | }
10 |
11 | export class Block {
12 | index: number
13 | data: DataType
14 | hash: string
15 | prevHash: string|null
16 | time: Date
17 |
18 | constructor(data: DataType, index: number) {
19 | this.index = index
20 | this.data = data
21 | this.time = new Date()
22 | this.hash = blockHash(index, data, this.time)
23 | this.prevHash = null;
24 | }
25 |
26 | regenerateHash() {
27 | const data: DataType|any = this.data
28 | const hash = blockHash(this.index, data, this.time)
29 |
30 | return hash.toString();
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/blockChain/generators.ts:
--------------------------------------------------------------------------------
1 | export function *searchGenerator(key: string, chain: Array) {
2 | for (let block in chain) {
3 | if (chain[block].data.key === key) {
4 | yield chain[block]
5 | }
6 | }
7 | return false
8 | }
9 |
10 | export function *iteratorGenerator(chain: Array) {
11 | for (let block in chain) {
12 | yield chain[block]
13 | }
14 | return false
15 | }
16 |
--------------------------------------------------------------------------------
/blockChain/helper.d.ts:
--------------------------------------------------------------------------------
1 |
2 | export type DataType = {
3 | key: string
4 | value: T
5 | }
6 |
7 | // BlockChain type of each block
8 | export type BlockType = {
9 | index: number
10 | data: DataType
11 | time: Date
12 | hash: string
13 | prevHash: null|string
14 | }
15 |
16 | // Block chain interface
17 | export interface BlockChainApi {
18 | length: number
19 | createBlock(data: DataType): boolean
20 | search(key: null|string, index: null|number): boolean|BlockType
21 | latestBlock(): boolean|BlockType
22 | log(key: null|string, index: null|number): void;
23 | iterator(): Generator
24 | checkValidation(): boolean
25 | }
26 |
--------------------------------------------------------------------------------
/blockChain/readme.md:
--------------------------------------------------------------------------------
1 | ## BlockChain Api
2 | sha256
has been used for creating Hashes.
3 |
4 | ```ts
5 | // Each block type
6 | type BlockType = {
7 | index: number
8 | data: DataType
9 | time: Date
10 | hash: string
11 | prevHash: null|string
12 | }
13 |
14 | // data type
15 | DataType = {
16 | key: string
17 | value: T
18 | }
19 |
20 | // static method that creates a instance of `BlockChain` class.
21 | BlockChain.createBlockChain()
22 |
23 | // Time Complexity: O(1)
24 | length: number;
25 |
26 | // *generator function, it returens an iterator.
27 | // loop through all blocks in the chain
28 | iterator(): Generator
29 |
30 | // Time Complexity: O(n)
31 | // check for "the blockChain is corrupted/manipulated or valid".
32 | checkValidation(): boolean
33 |
34 | // Time Complexity: O(1)
35 | createBlock(data: DataType): boolean
36 |
37 | // Time Complexity: O(1)
38 | latestBlock(): boolean|BlockType
39 |
40 | // Time Complexity:
41 | // log all: O(n)
42 | // log by key: O(n)
43 | // log by index: O(1)
44 | log(key: null|string = null, index: null|number = null): void;
45 |
46 | // Time Complexity:
47 | // search by key: O(n)
48 | // search by index: O(1)
49 | search(key: null|string, index: null|number = null): boolean|BlockType
50 |
51 | ```
52 |
53 |
54 |
55 |
56 | ## Examples
57 | ```ts
58 | import { BlockChain } from "https://deno.land/x/datastructure/mod.ts";
59 |
60 | const blockChain = BlockChain.createBlockChain()
61 |
62 | // iterator method returns a *generator function
63 | const iterator = blockChain.iterator()
64 | let iteratorNext = iterator.next()
65 | // console.log(iteratorNext.next(), iteratorNext.next());
66 | while (iteratorNext.done === false) {
67 | console.log(iteratorNext.value);
68 | iteratorNext = iterator.next()
69 | }
70 |
71 | // length or count of total blocks.
72 | blockChain.length
73 |
74 | // create a new block in the blockChain.
75 | blockChain.createBlock({key: 'sourav', value: {name: "Sourav"}})
76 | blockChain.createBlock({key: 'abm', value: "AbmSourav"})
77 | blockChain.createBlock({key: 'JS', value: ['JS', 'TS']})
78 |
79 | // get latest created block
80 | blockChain.latestBlock()
81 |
82 | // console all values
83 | blockChain.log()
84 | // console the block that has key: 'abm'
85 | blockChain.log('abm')
86 | // console then block which is index: 3
87 | blockChain.log(null, 3)
88 |
89 | // searching block by key
90 | blockChain.search('JS')
91 | // searching block by index
92 | blockChain.search(null, 2)
93 |
94 | ```
95 |
--------------------------------------------------------------------------------
/blockChain/test.ts:
--------------------------------------------------------------------------------
1 | import { BlockChain } from "./blockChain.ts";
2 | // import { BlockChain } from "https://deno.land/x/datastructure/mod.ts";
3 |
4 | const blockChain = BlockChain.createBlockChain()
5 |
6 | blockChain.createBlock({key: 'sourav', value: "Sourav"})
7 | blockChain.createBlock({key: 'abm', value: "AbmSourav"})
8 | blockChain.createBlock({key: 'apple', value: "Apple Inc."})
9 |
10 | // const iterator = blockChain.iterator()
11 | // let iteratorNext = iterator.next()
12 | // console.log(iterator.next(), iterator.next());
13 | // while (iteratorNext.done === false) {
14 | // console.log(iteratorNext.value);
15 | // iteratorNext = iterator.next()
16 | // }
17 |
18 | // console.log(blockChain.latestBlock());
19 |
20 | // blockChain.latestBlock().data.key = '1'
21 | // console.log(blockChain.checkValidation());
22 |
23 | blockChain.log(null, 3)
24 | // console.log(blockChain.search('sourav'))
25 | // console.log(blockChain.length)
26 |
--------------------------------------------------------------------------------
/deploy.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | echo "Press 'Y' to start deployment"
4 | read confirmation
5 |
6 | if [ $confirmation != 'Y' ]; then
7 | echo "Exiting..."
8 | exit;
9 | fi
10 |
11 | echo "Deleting hidden settings..."
12 | vscode=".vscode"
13 | if [ -d $vscode ]; then
14 | rm -R $vscode;
15 | git add -A
16 | git commit -m "workspace settings removed"
17 | fi
18 |
19 | echo "Release Version: "
20 | read version
21 |
22 | echo "Release comments: "
23 | read comments
24 | echo ""
25 |
26 | echo "Creating git tag..."
27 | git tag $version -m "$comments"
28 |
29 |
30 | echo "Press Y to push new release"
31 | read pushNow
32 | if [ $pushNow == 'Y' ]; then
33 | git push origin $version
34 | exit;
35 | fi
36 |
--------------------------------------------------------------------------------
/hashTable/generators.ts:
--------------------------------------------------------------------------------
1 | import { NodeType } from "./helper.d.ts";
2 |
3 | export function *addGenerator(currentNode: NodeType, newNode: NodeType) {
4 | while (currentNode !== null) {
5 | if (currentNode.next === null) {
6 | currentNode.next = newNode
7 | yield true
8 | }
9 |
10 | currentNode = currentNode.next
11 | }
12 | return false
13 | }
14 |
15 | export function *removeGenerator(key: string, currentNode: NodeType) {
16 | let prevNode: NodeType = null
17 | while (currentNode !== null) {
18 | if (currentNode.data.key === key) {
19 | prevNode!.next = currentNode.next
20 | yield true
21 | }
22 |
23 | prevNode = currentNode
24 | currentNode = currentNode.next
25 | }
26 | return false
27 | }
28 |
29 | export function *iteratorGenerator(table: Array) {
30 | for (let key in table) {
31 | yield table[key]
32 | }
33 | return false
34 | }
35 |
36 | export function *updateGenerator(key: string, newValue: any, currentNode: NodeType) {
37 | while (currentNode !== null) {
38 | if (currentNode.data.key === key) {
39 | currentNode.data.value = newValue
40 | yield true
41 | }
42 |
43 | currentNode = currentNode.next
44 | }
45 | return false
46 | }
47 |
48 | export function *getGenerator(key: string, currentNode: NodeType) {
49 | while (currentNode !== null) {
50 | if (currentNode.data.key === key) {
51 | yield currentNode.data
52 | }
53 |
54 | currentNode = currentNode.next
55 | }
56 | return false
57 | }
58 |
--------------------------------------------------------------------------------
/hashTable/hashFunction.ts:
--------------------------------------------------------------------------------
1 | export function hashFunction(str: string) {
2 | let hash = 17
3 |
4 | for (let i = 0; i < str.length; i++) {
5 | let newStr = str.charCodeAt(i) / 31
6 | hash = (13.11 * hash * newStr) / str.length
7 | }
8 |
9 | // weak hash function for testing
10 | // for (let i = 0; i < str.length; i++) {
11 | // // let newStr = str.charCodeAt(i) / 31
12 | // hash = (2 * hash) % str.length
13 | // }
14 | // console.log(hash);
15 |
16 | return hash;
17 | }
18 |
--------------------------------------------------------------------------------
/hashTable/hashNode.ts:
--------------------------------------------------------------------------------
1 | import { NodeType, DataType } from "./helper.d.ts";
2 |
3 | export class HashNode {
4 | public data: DataType
5 | public next: NodeType|null
6 |
7 | constructor(data: DataType) {
8 | this.data = data;
9 | this.next = null;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/hashTable/hashTable.bundle.js:
--------------------------------------------------------------------------------
1 | function hashFunction(str) {
2 | let hash = 17;
3 | for(let i = 0; i < str.length; i++){
4 | let newStr = str.charCodeAt(i) / 31;
5 | hash = 13.11 * hash * newStr / str.length;
6 | }
7 | return hash;
8 | }
9 | class HashNode {
10 | data;
11 | next;
12 | constructor(data1){
13 | this.data = data1;
14 | this.next = null;
15 | }
16 | }
17 | function* addGenerator(currentNode, newNode) {
18 | while(currentNode !== null){
19 | if (currentNode.next === null) {
20 | currentNode.next = newNode;
21 | yield true;
22 | }
23 | currentNode = currentNode.next;
24 | }
25 | return false;
26 | }
27 | function* removeGenerator(key, currentNode) {
28 | let prevNode = null;
29 | while(currentNode !== null){
30 | if (currentNode.data.key === key) {
31 | prevNode.next = currentNode.next;
32 | yield true;
33 | }
34 | prevNode = currentNode;
35 | currentNode = currentNode.next;
36 | }
37 | return false;
38 | }
39 | function* iteratorGenerator(table) {
40 | for(let key in table){
41 | yield table[key];
42 | }
43 | return false;
44 | }
45 | function* updateGenerator(key, newValue, currentNode) {
46 | while(currentNode !== null){
47 | if (currentNode.data.key === key) {
48 | currentNode.data.value = newValue;
49 | yield true;
50 | }
51 | currentNode = currentNode.next;
52 | }
53 | return false;
54 | }
55 | function* getGenerator(key, currentNode) {
56 | while(currentNode !== null){
57 | if (currentNode.data.key === key) {
58 | yield currentNode.data;
59 | }
60 | currentNode = currentNode.next;
61 | }
62 | return false;
63 | }
64 | class HashTable1 {
65 | #table;
66 | length;
67 | constructor(){
68 | this.#table = new Array();
69 | this.length = 0;
70 | }
71 | static createHT() {
72 | return new this();
73 | }
74 | add(data) {
75 | const index = hashFunction(data.key);
76 | const node = new HashNode(data);
77 | if (this.#table[index] === undefined) {
78 | this.#table[index] = node;
79 | this.length++;
80 | return true;
81 | }
82 | if (this.#table[index].next === null) {
83 | this.#table[index].next = node;
84 | return true;
85 | }
86 | let currentNode = this.#table[index].next;
87 | const iterator = addGenerator(currentNode, node);
88 | const iteratorNext = iterator.next();
89 | if (iteratorNext.value) {
90 | return true;
91 | }
92 | return false;
93 | }
94 | remove(key) {
95 | let index = hashFunction(key);
96 | if (this.#table[index] === undefined) return false;
97 | if (this.#table[index].data.key === key) {
98 | delete this.#table[index];
99 | this.length--;
100 | return true;
101 | }
102 | if (this.#table[index].next !== null) {
103 | let currentNode = this.#table[index];
104 | const iterator = removeGenerator(key, currentNode);
105 | const iteratorNext = iterator.next();
106 | if (iteratorNext.value) {
107 | return true;
108 | }
109 | }
110 | return false;
111 | }
112 | update(key, newValue) {
113 | let index = hashFunction(key);
114 | if (this.#table[index] === undefined) return false;
115 | if (this.#table[index].data.key === key) {
116 | this.#table[index].data.value = newValue;
117 | return this.#table[index].data.value;
118 | }
119 | if (this.#table[index].next !== null) {
120 | let currentNode = this.#table[index];
121 | const iterator = updateGenerator(key, newValue, currentNode);
122 | const iteratorNext = iterator.next();
123 | if (iteratorNext.value) {
124 | return true;
125 | }
126 | }
127 | return false;
128 | }
129 | get(key) {
130 | let index = hashFunction(key);
131 | if (this.#table[index] === undefined) return false;
132 | if (this.#table[index].data.key === key) {
133 | return this.#table[index].data;
134 | }
135 | if (this.#table[index].next !== null) {
136 | let currentNode = this.#table[index];
137 | const iterator = getGenerator(key, currentNode);
138 | const iteratorNext = iterator.next();
139 | if (iteratorNext.value) {
140 | return iteratorNext.value;
141 | }
142 | }
143 | return false;
144 | }
145 | log(key) {
146 | if (this.length <= 0) return false;
147 | if (key) {
148 | let index = hashFunction(key);
149 | if (this.#table[index].data.key === key) {
150 | return console.log(this.#table[index]);
151 | }
152 | if (this.#table[index] && this.#table[index].next) {
153 | let currentNode = this.#table[index].next;
154 | while(currentNode){
155 | if (currentNode.data.key === key) {
156 | return console.log(currentNode);
157 | }
158 | currentNode = currentNode.next;
159 | }
160 | }
161 | }
162 | for(let cell in this.#table){
163 | console.log(this.#table[cell]);
164 | }
165 | }
166 | iterator() {
167 | const iterator = iteratorGenerator(this.#table);
168 | return iterator;
169 | }
170 | }
171 | export { HashTable1 as HashTable };
172 |
--------------------------------------------------------------------------------
/hashTable/hashTable.ts:
--------------------------------------------------------------------------------
1 | import { HashTableApi, DataType } from "./helper.d.ts";
2 | import { hashFunction } from "./hashFunction.ts";
3 | import { HashNode } from "./hashNode.ts";
4 | import {
5 | addGenerator,
6 | removeGenerator,
7 | updateGenerator,
8 | getGenerator,
9 | iteratorGenerator
10 | } from "./generators.ts";
11 |
12 |
13 | export class HashTable implements HashTableApi {
14 | #table: Array
15 | public length: number
16 |
17 | constructor() {
18 | this.#table = new Array()
19 | this.length = 0
20 | }
21 |
22 | static createHT() {
23 | return new this();
24 | }
25 |
26 | add(data: DataType) {
27 | const index = hashFunction(data.key)
28 | const node = new HashNode(data)
29 |
30 | if (this.#table[index] === undefined) {
31 | this.#table[index] = node
32 | this.length++
33 | return true
34 | }
35 | if (this.#table[index].next === null) {
36 | this.#table[index].next = node
37 | return true
38 | }
39 |
40 | let currentNode = this.#table[index].next
41 | // generator function that returns an iterator
42 | const iterator = addGenerator(currentNode, node)
43 | const iteratorNext = iterator.next()
44 | if (iteratorNext.value) {
45 | return true
46 | }
47 |
48 | return false;
49 | }
50 |
51 | remove(key: string) {
52 | let index = hashFunction(key)
53 | if (this.#table[index] === undefined) return false
54 |
55 | if (this.#table[index].data.key === key) {
56 | delete this.#table[index]
57 | this.length--
58 | return true;
59 | }
60 |
61 | if (this.#table[index].next !== null) {
62 | let currentNode = this.#table[index]
63 | // generator function that returns an iterator
64 | const iterator = removeGenerator(key, currentNode)
65 | const iteratorNext = iterator.next()
66 | if (iteratorNext.value) {
67 | return true
68 | }
69 | }
70 |
71 | return false
72 | }
73 |
74 | update(key: string, newValue: any) {
75 | let index = hashFunction(key)
76 | if (this.#table[index] === undefined) return false
77 |
78 | if (this.#table[index].data.key === key) {
79 | this.#table[index].data.value = newValue
80 | return this.#table[index].data.value;
81 | }
82 |
83 | if (this.#table[index].next !== null) {
84 | let currentNode = this.#table[index]
85 | // generator function that returns an iterator
86 | const iterator = updateGenerator(key, newValue, currentNode)
87 | const iteratorNext = iterator.next()
88 | if (iteratorNext.value) {
89 | return true
90 | }
91 | }
92 |
93 | return false
94 | }
95 |
96 | get(key: string) {
97 | let index = hashFunction(key)
98 | if (this.#table[index] === undefined) return false
99 |
100 | if (this.#table[index].data.key === key) {
101 | return this.#table[index].data;
102 | }
103 |
104 | if (this.#table[index].next !== null) {
105 | let currentNode = this.#table[index]
106 | // generator function that returns an iterator
107 | const iterator = getGenerator(key, currentNode)
108 | const iteratorNext = iterator.next()
109 | if (iteratorNext.value) {
110 | return iteratorNext.value
111 | }
112 | }
113 |
114 | return false
115 | }
116 |
117 | log(key?: string) {
118 | if (this.length <= 0) return false
119 |
120 | if (key) {
121 | let index = hashFunction(key)
122 | if (this.#table[index].data.key === key) {
123 | return console.log(this.#table[index])
124 | }
125 | if (this.#table[index] && this.#table[index].next) {
126 | let currentNode = this.#table[index].next
127 | while (currentNode) {
128 | if (currentNode.data.key === key) {
129 | return console.log(currentNode)
130 | }
131 |
132 | currentNode = currentNode.next
133 | }
134 | }
135 | }
136 |
137 | for (let cell in this.#table) {
138 | console.log(this.#table[cell])
139 | }
140 | }
141 |
142 | iterator() {
143 | // generator function that returns an iterator
144 | const iterator = iteratorGenerator(this.#table)
145 | return iterator
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/hashTable/helper.d.ts:
--------------------------------------------------------------------------------
1 |
2 | // data type
3 | export type DataType = {
4 | key: string
5 | value: T
6 | }
7 |
8 | export type NodeType = {
9 | data: DataType
10 | next: null|NodeType
11 | }|null
12 |
13 | export interface HashTableApi {
14 | length: number
15 | add(data: DataType): boolean
16 | remove(key: string): boolean|any[]
17 | update(key: string, newValue: DataType): boolean|DataType
18 | get(key: string): boolean|DataType
19 | log(key?: string): void,
20 | iterator(): Generator
21 | }
22 |
--------------------------------------------------------------------------------
/hashTable/readme.md:
--------------------------------------------------------------------------------
1 | ## Hash Table Api
2 |
3 | ```ts
4 | // data type
5 | DataType = {
6 | key: string
7 | value: T
8 | }
9 |
10 | // static method that creates a instance of `HashTable` class.
11 | HashTable.createHT()
12 |
13 | // Time Complexity: O(1)
14 | length: number
15 |
16 | // *generator function, it returens an iterator.
17 | iterator(): Generator
18 |
19 | // Time Complexity:
20 | // average cases: O(1)
21 | // worst case: O(n)
22 | add(data: DataType): boolean
23 |
24 | // Time Complexity:
25 | // average cases: O(1)
26 | // worst case: O(n)
27 | remove(key: string): boolean|any[]
28 |
29 | // Time Complexity:
30 | // average cases: O(1)
31 | // worst case: O(n)
32 | update(key: string, newValue: DataType): boolean|DataType
33 |
34 | // Time Complexity:
35 | // average cases: O(1)
36 | // worst case: O(n)
37 | get(key: string): boolean|DataType
38 |
39 | // Time Complexity:
40 | // log all items: O(n)
41 | // log by key: avg. O(1), worst: O(n)
42 | log(key?: string): void,
43 | ```
44 |
45 |
46 |
47 |
48 | ## Example
49 | ```ts
50 | import { HashTable } from "https://deno.land/x/datastructure/mod.ts";
51 |
52 | const hashTable = HashTable.createHT()
53 |
54 | // add element in the hash table
55 | // if hash keys are same, then it will create chaining with LinkedList
56 | hashTable.add({key: "abm", value: "Sourav"})
57 | hashTable.add({key: "apple", value: {company: "Apple Inc"}})
58 | hashTable.add({key: "arr", value: [1, 2, 3]})
59 |
60 | // remove element from hastabel
61 | hashTable.remove("arr");
62 |
63 | const iterator = hashTable.iterator()
64 | let iteratorNext = iterator.next()
65 | // console.log(iteratorNext.next(), iteratorNext.next());
66 | while (iteratorNext.done === false) {
67 | console.log(iteratorNext.value);
68 | iteratorNext = iterator.next()
69 | }
70 |
71 | // get element from hash table
72 | hashTable.get('abm')
73 |
74 | // update element's data using the key
75 | hashTable.update('abm', {name: 'AbmSourav'})
76 |
77 | // console all elements
78 | hashTable.log()
79 | // console element by key
80 | hashTable.log('abm')
81 | ```
82 |
--------------------------------------------------------------------------------
/hashTable/test.ts:
--------------------------------------------------------------------------------
1 | import { HashTable } from "./hashTable.ts"
2 | import { assert, assertEquals } from "https://deno.land/std/testing/asserts.ts";
3 |
4 | const hashTable = HashTable.createHT()
5 |
6 | hashTable.add({key: "abm", value: "Sourav"})
7 | hashTable.add({key: "bma", value: {company: "Apple LLC"}})
8 | hashTable.add({key: "appl", value: {product: "iPhone"}})
9 | hashTable.add({key: "microsoft", value: "MicroSoft"})
10 | // hashTable.add({key: "Linux", value: "Ubuntu"})
11 | // hashTable.add({key: "ChromeOS", value: "Google"})
12 | // console.log(hashTable.remove("microsoft"));
13 |
14 | // const it = hashTable.iterator()
15 | // console.log(it.next(), it.next(), it.next(), it.next());
16 |
17 | // console.log(hashTable.update('bma', {name: 'Apple'}));
18 | // console.log(hashTable)
19 |
20 | hashTable.log()
21 |
22 | // Deno.test("Add", function() {
23 | // assertEquals(hashTable.add({key: "abm", value: "Sourav"}), true)
24 | // })
25 |
26 | // Deno.test("remove", function() {
27 | // assertEquals(hashTable.remove("microsoft"), false);
28 | // })
29 |
--------------------------------------------------------------------------------
/linkedList/doubly/doublyLinkedList.bundle.js:
--------------------------------------------------------------------------------
1 | class DoublyNode {
2 | data;
3 | next;
4 | prev;
5 | constructor(data1){
6 | this.data = data1;
7 | this.next = null;
8 | this.prev = null;
9 | }
10 | }
11 | function* addGenerator(currentNode, data1, position) {
12 | let count = 0;
13 | let prevNode = null;
14 | while(currentNode !== null){
15 | if (count === position) {
16 | prevNode.next = new DoublyNode(data1);
17 | prevNode.next.next = currentNode;
18 | yield true;
19 | }
20 | prevNode = currentNode;
21 | currentNode = currentNode.next;
22 | count++;
23 | }
24 | return false;
25 | }
26 | function* updateGenerator(key, currentNode, newValue) {
27 | while(currentNode !== null){
28 | if (currentNode.data.key === key) {
29 | currentNode.data.value = newValue;
30 | yield currentNode.data;
31 | }
32 | currentNode = currentNode.next;
33 | }
34 | return false;
35 | }
36 | function* searchGenerator(key, currentNode) {
37 | while(currentNode !== null){
38 | if (currentNode.data.key === key) {
39 | yield currentNode.data;
40 | }
41 | currentNode = currentNode.next;
42 | }
43 | return false;
44 | }
45 | function* iteratorGenerator(currentNode) {
46 | while(currentNode !== null){
47 | yield currentNode.data;
48 | currentNode = currentNode.next;
49 | }
50 | return false;
51 | }
52 | class DoublyLinkedList1 {
53 | #head;
54 | #tail;
55 | size;
56 | constructor(){
57 | this.#head = null;
58 | this.#tail = null;
59 | this.size = 0;
60 | }
61 | static createDL() {
62 | return new this();
63 | }
64 | prepend(data) {
65 | const newNode = new DoublyNode(data);
66 | if (this.#head === null) {
67 | this.#head = newNode;
68 | this.#tail = newNode;
69 | return true;
70 | }
71 | const currentNode = this.#head;
72 | this.#head = newNode;
73 | this.#head.next = currentNode;
74 | currentNode.prev = newNode;
75 | this.size++;
76 | return true;
77 | }
78 | append(data) {
79 | const newNode = new DoublyNode(data);
80 | if (this.#head === null) {
81 | this.#head = newNode;
82 | this.#tail = newNode;
83 | return true;
84 | }
85 | const currentNode = this.#tail;
86 | this.#tail.next = newNode;
87 | this.#tail = newNode;
88 | this.#tail.prev = currentNode;
89 | this.size++;
90 | return false;
91 | }
92 | add(data, position) {
93 | if (position === 0) {
94 | console.error("Use `prepend()` method to insert data at Head.");
95 | return false;
96 | } else if (position === this.size) {
97 | console.error("Use `append()` method to insert data at Tail.");
98 | return false;
99 | } else if (position > this.size && position != this.size + 1) {
100 | console.error(`Out of range. Size of the linkedList is ${this.size}`);
101 | return false;
102 | }
103 | const iterator = addGenerator(this.#head, data, position);
104 | const iteratorNext = iterator.next();
105 | if (iteratorNext.value) {
106 | this.size++;
107 | return true;
108 | }
109 | return false;
110 | }
111 | getFromHead() {
112 | if (this.#head === null) {
113 | return false;
114 | }
115 | return this.#head.data;
116 | }
117 | getFromTail() {
118 | if (this.#tail === null) {
119 | return false;
120 | }
121 | return this.#tail.data;
122 | }
123 | log() {
124 | let currentNode = this.#head;
125 | while(currentNode !== null){
126 | console.log(currentNode.data);
127 | currentNode = currentNode.next;
128 | }
129 | }
130 | remove(key) {
131 | if (this.#head === null) {
132 | return false;
133 | }
134 | if (this.#head.data.key === key) {
135 | const prevHeadData = this.#head.data;
136 | this.#head = this.#head.next;
137 | if (this.#head === null) this.#tail = this.#head;
138 | this.size--;
139 | if (this.#head === null) this.size = 0;
140 | return prevHeadData;
141 | }
142 | if (this.#tail.data.key === key) {
143 | const prevTailData = this.#tail.data;
144 | this.#tail = this.#tail.prev;
145 | this.#tail.next = null;
146 | return prevTailData;
147 | }
148 | let previousNode = null;
149 | let currentNode = this.#head;
150 | while(currentNode !== null){
151 | if (key === currentNode.data.key) {
152 | const temp = currentNode.data;
153 | previousNode.next = currentNode.next;
154 | currentNode.next.prev = previousNode;
155 | if (currentNode.next === null) this.#tail = previousNode;
156 | this.size--;
157 | return temp;
158 | }
159 | previousNode = currentNode;
160 | currentNode = currentNode.next;
161 | }
162 | return false;
163 | }
164 | update(key, newValue) {
165 | if (this.#head === null) {
166 | return false;
167 | }
168 | if (this.#head.data.key === key) {
169 | this.#head.data.value = newValue;
170 | return this.#head.data;
171 | }
172 | if (this.#tail.data.key === key) {
173 | this.#tail.data.value = newValue;
174 | return this.#tail.data;
175 | }
176 | const iterator = updateGenerator(key, this.#head, newValue);
177 | const iteratorNext = iterator.next();
178 | if (iteratorNext.value) {
179 | this.size++;
180 | return iteratorNext.value;
181 | }
182 | return false;
183 | }
184 | search(key) {
185 | if (this.#head === null) {
186 | return false;
187 | }
188 | if (this.#head.data.key === key) {
189 | return this.#head.data;
190 | }
191 | if (this.#tail.data.key === key) {
192 | return this.#tail.data;
193 | }
194 | const iterator = searchGenerator(key, this.#head);
195 | const iteratorNext = iterator.next();
196 | if (iteratorNext.value) {
197 | this.size++;
198 | return iteratorNext.value;
199 | }
200 | return false;
201 | }
202 | iterator() {
203 | const iterator = iteratorGenerator(this.#head);
204 | return iterator;
205 | }
206 | }
207 | export { DoublyLinkedList1 as DoublyLinkedList };
208 |
--------------------------------------------------------------------------------
/linkedList/doubly/doublyLinkedList.ts:
--------------------------------------------------------------------------------
1 | import { LinkedListApi, NodeType, DataType } from "./helper.d.ts";
2 | import { DoublyNode } from "./doublyNode.ts";
3 | import {
4 | addGenerator,
5 | updateGenerator,
6 | searchGenerator,
7 | iteratorGenerator
8 | } from "./generators.ts"
9 |
10 |
11 | export class DoublyLinkedList implements LinkedListApi {
12 | #head: null|NodeType
13 | #tail: null|NodeType
14 | public size: number
15 |
16 | constructor() {
17 | this.#head = null
18 | this.#tail = null
19 | this.size = 0
20 | }
21 |
22 | static createDL() {
23 | return new this();
24 | }
25 |
26 | // insert in the head
27 | prepend(data: DataType) {
28 | const newNode = new DoublyNode(data);
29 | if (this.#head === null) {
30 | this.#head = newNode
31 | this.#tail = newNode
32 | return true;
33 | }
34 |
35 | const currentNode = this.#head
36 | this.#head = newNode
37 | this.#head.next = currentNode
38 | currentNode.prev = newNode
39 |
40 | this.size++
41 | return true;
42 | }
43 |
44 | // inset in the tail
45 | append(data: DataType) {
46 | const newNode = new DoublyNode(data);
47 | if (this.#head === null) {
48 | this.#head = newNode
49 | this.#tail = newNode
50 | return true;
51 | }
52 |
53 | const currentNode = this.#tail
54 | this.#tail!.next = newNode
55 | this.#tail = newNode
56 | this.#tail.prev = currentNode
57 | this.size++
58 |
59 | return false;
60 | }
61 |
62 | // // add anywhere of the linked list except head & tail
63 | add(data: DataType, position: number) {
64 | if (position === 0) {
65 | console.error("Use `prepend()` method to insert data at Head.");
66 | return false;
67 | } else if (position === this.size) {
68 | console.error("Use `append()` method to insert data at Tail.");
69 | return false;
70 | } else if (position > this.size && position != this.size + 1) {
71 | console.error(`Out of range. Size of the linkedList is ${this.size}`);
72 | return false;
73 | }
74 |
75 | // generator function that returns an iterator
76 | const iterator = addGenerator(this.#head, data, position)
77 | const iteratorNext = iterator.next()
78 | if (iteratorNext.value) {
79 | this.size++
80 | return true
81 | }
82 |
83 | return false;
84 | }
85 |
86 | getFromHead() {
87 | if (this.#head === null) {
88 | return false
89 | }
90 |
91 | return this.#head.data;
92 | }
93 |
94 | getFromTail() {
95 | if (this.#tail === null) {
96 | return false
97 | }
98 |
99 | return this.#tail.data
100 | }
101 |
102 | log() {
103 | let currentNode = this.#head
104 | while (currentNode !== null) {
105 | console.log(currentNode.data);
106 | currentNode = currentNode.next
107 | }
108 | }
109 |
110 | remove(key: string|number) {
111 | if (this.#head === null) {
112 | return false;
113 | }
114 | if (this.#head.data.key === key) {
115 | const prevHeadData = this.#head.data
116 | this.#head = this.#head.next
117 | if (this.#head === null) this.#tail = this.#head
118 |
119 | this.size--
120 | if (this.#head === null) this.size = 0
121 | return prevHeadData;
122 | }
123 | if (this.#tail!.data.key === key) {
124 | const prevTailData = this.#tail!.data
125 |
126 | this.#tail = this.#tail!.prev
127 | this.#tail!.next = null
128 | return prevTailData;
129 | }
130 |
131 | // two pointer approach
132 | let previousNode: NodeType = null;
133 | let currentNode: NodeType = this.#head
134 | while (currentNode !== null) {
135 | if (key === currentNode.data.key) {
136 | const temp = currentNode.data
137 | previousNode!.next = currentNode.next
138 | currentNode.next!.prev = previousNode
139 | if (currentNode!.next === null) this.#tail = previousNode
140 |
141 | this.size--
142 | return temp;
143 | }
144 |
145 | previousNode = currentNode
146 | currentNode = currentNode.next
147 | }
148 |
149 | return false;
150 | }
151 |
152 | update(key: string|number, newValue: any) {
153 | if (this.#head === null) {
154 | return false;
155 | }
156 |
157 | if (this.#head.data.key === key) {
158 | this.#head.data.value = newValue
159 | return this.#head.data;
160 | }
161 | if (this.#tail!.data.key === key) {
162 | this.#tail!.data.value = newValue
163 | return this.#tail!.data;
164 | }
165 |
166 | // generator function that returns an iterator
167 | const iterator = updateGenerator(key, this.#head, newValue)
168 | const iteratorNext = iterator.next()
169 | if (iteratorNext.value) {
170 | this.size++
171 | return iteratorNext.value
172 | }
173 |
174 | return false;
175 | }
176 |
177 | search(key: string|number) {
178 | if (this.#head === null) {
179 | return false;
180 | }
181 |
182 | if (this.#head.data.key === key) {
183 | return this.#head.data;
184 | }
185 | if (this.#tail!.data.key === key) {
186 | return this.#tail!.data
187 | }
188 |
189 | // generator function that returns an iterator
190 | const iterator = searchGenerator(key, this.#head)
191 | const iteratorNext = iterator.next()
192 | if (iteratorNext.value) {
193 | this.size++
194 | return iteratorNext.value
195 | }
196 |
197 | return false;
198 | }
199 |
200 | iterator() {
201 | // generator function that returns an iterator
202 | const iterator = iteratorGenerator(this.#head)
203 | return iterator
204 | }
205 | }
206 |
--------------------------------------------------------------------------------
/linkedList/doubly/doublyNode.ts:
--------------------------------------------------------------------------------
1 | import { DataType, NodeType } from "./helper.d.ts";
2 |
3 | export class DoublyNode {
4 | public data: DataType
5 | public next: NodeType|null
6 | public prev: NodeType|null
7 |
8 | constructor(data: DataType) {
9 | this.data = data;
10 | this.next = null;
11 | this.prev = null;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/linkedList/doubly/generators.ts:
--------------------------------------------------------------------------------
1 | import { NodeType, DataType } from "./helper.d.ts";
2 | import { DoublyNode } from "./doublyNode.ts";
3 |
4 | export function *addGenerator(currentNode: NodeType, data: DataType, position: number) {
5 | let count = 0
6 | let prevNode: NodeType = null;
7 |
8 | while (currentNode !== null) {
9 | if (count === position) {
10 | prevNode!.next = new DoublyNode(data)
11 | prevNode!.next.next = currentNode
12 | yield true
13 | }
14 |
15 | prevNode = currentNode
16 | currentNode = currentNode.next
17 | count++
18 | }
19 | return false
20 | }
21 |
22 | export function *updateGenerator(key: string|number, currentNode: NodeType, newValue: any) {
23 | while (currentNode !== null) {
24 | if (currentNode.data.key === key) {
25 | currentNode.data.value = newValue
26 | yield currentNode.data;
27 | }
28 |
29 | currentNode = currentNode.next
30 | }
31 | return false
32 | }
33 |
34 | export function *searchGenerator(key: string|number, currentNode: NodeType) {
35 | while (currentNode !== null) {
36 | if (currentNode.data.key === key) {
37 | yield currentNode.data;
38 | }
39 |
40 | currentNode = currentNode.next
41 | }
42 | return false
43 | }
44 |
45 | export function *iteratorGenerator(currentNode: NodeType) {
46 | while (currentNode !== null) {
47 | yield currentNode.data;
48 |
49 | currentNode = currentNode.next
50 | }
51 | return false
52 | }
53 |
--------------------------------------------------------------------------------
/linkedList/doubly/helper.d.ts:
--------------------------------------------------------------------------------
1 |
2 | // data type
3 | export type DataType = {
4 | key: string|number
5 | value: T
6 | }
7 |
8 | // singly linked list node class
9 | export type NodeType = {
10 | data: DataType
11 | next: null|NodeType
12 | prev: null|NodeType
13 | }|null
14 |
15 | // singly linked list interface
16 | export interface LinkedListApi {
17 | size: number;
18 | prepend(data: DataType): boolean;
19 | append(data: DataType): boolean;
20 | add(data: DataType, position: number): boolean;
21 | getFromHead(): object|false;
22 | getFromTail(): object|false;
23 | log(): void;
24 | remove(key: string|number): object|boolean;
25 | update(key: string|number, newValue: any): object|boolean;
26 | search(key: string|number): object|boolean
27 | iterator(): Generator
28 | }
29 |
--------------------------------------------------------------------------------
/linkedList/doubly/readme.md:
--------------------------------------------------------------------------------
1 | ## Doubly Linked List Api
2 |
3 | ```ts
4 | DataType = {
5 | key: string|number
6 | value: T
7 | }
8 |
9 | // static method that creates a instance of `DoublyLinkedList` class.
10 | DoublyLinkedList.createDL()
11 |
12 | // Time Complexity: O(1)
13 | size: number;
14 |
15 | // *generator function, it returens an iterator.
16 | iterator(): Generator
17 |
18 | // Time Complexity: O(1)
19 | prepend(data: DataType): boolean;
20 | append(data: DataType): boolean;
21 |
22 | // Time Complexity: O(n)
23 | add(data: DataType, position: number): boolean;
24 |
25 | // Time Complexity: O(1)
26 | getFromHead(): object|false;
27 |
28 | // Time Complexity: O(1)
29 | getFromTail(): object|false;
30 |
31 | // Time Complexity: O(n)
32 | log(): void;
33 |
34 | // Time Complexity:
35 | // head node: O(1), other nodes: O(n)
36 | remove(key: string|number): object|boolean;
37 |
38 | // Time Complexity:
39 | // head node & tail: O(1), other nodes: O(n)
40 | update(key: string|number, newValue: any): object|boolean;
41 |
42 | // Time Complexity:
43 | // head node & tail node: O(1), other nodes: O(n)
44 | search(key: string|number): object|null
45 | ```
46 |
47 |
48 |
49 |
50 | ## Examples
51 | ```ts
52 | import { DoublyLinkedList } from "https://deno.land/x/datastructure/mod.ts";
53 |
54 | const doublyLinkedList = DoublyLinkedList.createDL()
55 |
56 | // iterator method returns a *generator function
57 | const iterator = doublyLinkedList.iterator()
58 | let iteratorNext = iterator.next()
59 | // console.log(iteratorNext.next(), iteratorNext.next());
60 | while (iteratorNext.done === false) {
61 | console.log(iteratorNext.value);
62 | iteratorNext = iterator.next()
63 | }
64 |
65 | // get the size of the linked list
66 | doublyLinkedList.size
67 |
68 | // insert in the head
69 | doublyLinkedList.prepend({key: 'a', value: 'apple'})
70 | doublyLinkedList.prepend({key: 'b', value: [1, 2, 3]})
71 | doublyLinkedList.prepend({key: 11, value: {name: 'Sourav'}})
72 |
73 | // insert in the tail
74 | doublyLinkedList.append({key: 'c', value: 'add to last'})
75 |
76 | // insert anywhere except head & tail
77 | doublyLinkedList.add({key: 'x', value: 'X'}, 1)
78 |
79 | // get data from head or tail of linked list
80 | doublyLinkedList.getFromHead()
81 | doublyLinkedList.getFromTail()
82 |
83 | // console all values
84 | doublyLinkedList.log()
85 |
86 | // remove or delete data from linked list
87 | doublyLinkedList.remove('a')
88 |
89 | // update data in linked list
90 | doublyLinkedList.update('a', [1, 4, 8])
91 |
92 | // search data in the linked list
93 | doublyLinkedList.search('Sourav')
94 |
95 | ```
96 |
--------------------------------------------------------------------------------
/linkedList/doubly/test.ts:
--------------------------------------------------------------------------------
1 | import { DoublyLinkedList } from "./doublyLinkedList.ts";
2 | // import { DoublyLinkedList } from "https://deno.land/x/datastructure/mod.ts"
3 |
4 | const doublyLinkedList = DoublyLinkedList.createDL()
5 |
6 | doublyLinkedList.prepend({key: 'a', value: 'apple'})
7 | doublyLinkedList.prepend({key: 'b', value: [1, 2, 3]})
8 | // doublyLinkedList.prepend({key: 11, value: {val: 'add to head'}})
9 | // doublyLinkedList.append({key: 'c', value: 'add before last'})
10 | doublyLinkedList.append({key: 'd', value: 'add to last'})
11 | // doublyLinkedList.add({key: 'x', value: 'X'}, 1)
12 |
13 | // console.log(doublyLinkedList.getFromHead());
14 | // console.log(doublyLinkedList.getFromTail());
15 | // console.log(doublyLinkedList.remove('c'));
16 |
17 | // console.log(doublyLinkedList.update('b', 'bbbb'));
18 | console.log(doublyLinkedList.search('b'));
19 | console.log('');
20 | // console.log(doublyLinkedList)
21 | doublyLinkedList.log()
22 |
23 | // const it = doublyLinkedList.iterator()
24 | // let itNext = it.next()
25 | // console.log(it.next(), it.next(), it.next());
26 | // while (itNext.done === false) {
27 | // console.log(itNext.value);
28 | // itNext = it.next()
29 | // }
30 |
--------------------------------------------------------------------------------
/linkedList/singly/generators.ts:
--------------------------------------------------------------------------------
1 | import { NodeType, DataType } from "./helper.d.ts";
2 | import { SinglyNode } from "./singlyNode.ts";
3 |
4 | export function *addGenerator(currentNode: NodeType, data: DataType, position: number) {
5 | let count = 0
6 | let prevNode: NodeType = null;
7 |
8 | while (currentNode !== null) {
9 | if (count === position) {
10 | prevNode!.next = new SinglyNode(data)
11 | prevNode!.next.next = currentNode
12 | yield true
13 | }
14 |
15 | prevNode = currentNode
16 | currentNode = currentNode.next
17 | count++
18 | }
19 | return false
20 | }
21 |
22 | export function *updateGenerator(key: string|number, currentNode: NodeType, newValue: any) {
23 | while (currentNode !== null) {
24 | if (currentNode.data.key === key) {
25 | currentNode.data.value = newValue
26 | yield currentNode.data;
27 | }
28 |
29 | currentNode = currentNode.next
30 | }
31 | return false
32 | }
33 |
34 | export function *searchGenerator(key: string|number, currentNode: NodeType) {
35 | while (currentNode !== null) {
36 | if (currentNode.data.key === key) {
37 | yield currentNode.data;
38 | }
39 |
40 | currentNode = currentNode.next
41 | }
42 | return false
43 | }
44 |
45 | export function *iteratorGenerator(currentNode: NodeType) {
46 | while (currentNode !== null) {
47 | yield currentNode.data;
48 |
49 | currentNode = currentNode.next
50 | }
51 | return false
52 | }
53 |
--------------------------------------------------------------------------------
/linkedList/singly/helper.d.ts:
--------------------------------------------------------------------------------
1 |
2 | // data type
3 | export type DataType = {
4 | key: string|number
5 | value: T
6 | }
7 |
8 | // singly linked list node class
9 | export type NodeType = {
10 | data: DataType
11 | next: null|NodeType
12 | }|null
13 |
14 | // singly linked list interface
15 | export interface LinkedListApi {
16 | size: number;
17 | prepend(data: DataType): boolean;
18 | append(data: DataType): boolean;
19 | add(data: DataType, position: number): boolean;
20 | getFromHead(): object|false;
21 | getFromTail(): object|false;
22 | log(): void;
23 | remove(key: string|number): boolean;
24 | update(key: string|number, newValue: any): object|boolean;
25 | search(key: string|number): object|boolean
26 | iterator(): Generator
27 | }
28 |
--------------------------------------------------------------------------------
/linkedList/singly/readme.md:
--------------------------------------------------------------------------------
1 | ## Singly Linked List Api
2 |
3 | ```ts
4 | DataType = {
5 | key: string|number
6 | value: T
7 | }
8 |
9 | // static method that creates a instance of `SinglyLinkedList` class.
10 | SinglyLinkedList.createSL()
11 |
12 | // Time Complexity: O(1)
13 | size: number;
14 |
15 | // *generator function, it returens an iterator.
16 | iterator(): Generator
17 |
18 | // Time Complexity: O(1)
19 | prepend(data: DataType): boolean;
20 | append(data: DataType): boolean;
21 |
22 | // Time Complexity: O(n)
23 | add(data: DataType, position: number): boolean;
24 |
25 | // Time Complexity: O(1)
26 | getFromHead(): object|false;
27 |
28 | // Time Complexity: O(1)
29 | getFromTail(): object|false;
30 |
31 | // Time Complexity: O(n)
32 | log(): void;
33 |
34 | // Time Complexity:
35 | // head node: O(1), other nodes: O(n)
36 | remove(key: string|number): object|boolean;
37 |
38 | // Time Complexity:
39 | // head & tail node: O(1), other nodes: O(n)
40 | update(key: string|number, newValue: any): object|boolean;
41 |
42 | // Time Complexity:
43 | // head node & tail node: O(1), other nodes: O(n)
44 | search(key: string|number): object|boolean
45 | ```
46 |
47 |
48 |
49 |
50 | ## Examples
51 | ```ts
52 | import { SinglyLinkedList } from "https://deno.land/x/datastructure/mod.ts";
53 |
54 | const singlyLinkedList = SinglyLinkedList.createSL()
55 |
56 | // iterator method returns a *generator function
57 | const iterator = singlyLinkedList.iterator()
58 | let iteratorNext = iterator.next()
59 | // console.log(iteratorNext.next(), iteratorNext.next());
60 | while (iteratorNext.done === false) {
61 | console.log(iteratorNext.value);
62 | iteratorNext = iterator.next()
63 | }
64 |
65 | // linked list size
66 | singlyLinkedList.size
67 |
68 | // adding data at the head of linked list
69 | singlyLinkedList.prepend({key: 'a', value: 'apple'})
70 | singlyLinkedList.prepend({key: 'b', value: [1, 2, 3]})
71 | singlyLinkedList.prepend({key: 11, value: 'add to head'})
72 |
73 | // adding data at the tail of linked list
74 | singlyLinkedList.append({key: 'c', value: 'add to last'})
75 |
76 | // insert anywhere except head & tail
77 | singlyLinkedList.add({key: 'x', value: 'X'}, 1)
78 |
79 | // get data from head or tail
80 | singlyLinkedList.getFromHead()
81 | singlyLinkedList.getFromTail()
82 |
83 | // console all values
84 | singlyLinkedList.log()
85 |
86 | // remove or delete data from linked list
87 | singlyLinkedList.remove('a')
88 |
89 | // update data in linked list
90 | singlyLinkedList.update('a', [1, 4, 8])
91 |
92 | // searching data in linked list
93 | singlyLinkedList.search('Sourav')
94 |
95 | ```
96 |
--------------------------------------------------------------------------------
/linkedList/singly/singlyLinkedList.bundle.js:
--------------------------------------------------------------------------------
1 | class SinglyNode {
2 | data;
3 | next;
4 | constructor(data1){
5 | this.data = data1;
6 | this.next = null;
7 | }
8 | }
9 | function* addGenerator(currentNode, data1, position) {
10 | let count = 0;
11 | let prevNode = null;
12 | while(currentNode !== null){
13 | if (count === position) {
14 | prevNode.next = new SinglyNode(data1);
15 | prevNode.next.next = currentNode;
16 | yield true;
17 | }
18 | prevNode = currentNode;
19 | currentNode = currentNode.next;
20 | count++;
21 | }
22 | return false;
23 | }
24 | function* updateGenerator(key, currentNode, newValue) {
25 | while(currentNode !== null){
26 | if (currentNode.data.key === key) {
27 | currentNode.data.value = newValue;
28 | yield currentNode.data;
29 | }
30 | currentNode = currentNode.next;
31 | }
32 | return false;
33 | }
34 | function* searchGenerator(key, currentNode) {
35 | while(currentNode !== null){
36 | if (currentNode.data.key === key) {
37 | yield currentNode.data;
38 | }
39 | currentNode = currentNode.next;
40 | }
41 | return false;
42 | }
43 | function* iteratorGenerator(currentNode) {
44 | while(currentNode !== null){
45 | yield currentNode.data;
46 | currentNode = currentNode.next;
47 | }
48 | return false;
49 | }
50 | class SinglyLinkedList1 {
51 | #head;
52 | #tail;
53 | size;
54 | constructor(){
55 | this.#head = null;
56 | this.#tail = null;
57 | this.size = 0;
58 | }
59 | static createSL() {
60 | return new this();
61 | }
62 | prepend(data) {
63 | if (this.#head === null) {
64 | const newNode = new SinglyNode(data);
65 | this.#head = newNode;
66 | this.#tail = newNode;
67 | return true;
68 | }
69 | const currentNode = this.#head;
70 | this.#head = new SinglyNode(data);
71 | this.#head.next = currentNode;
72 | if (currentNode.next === null) {
73 | this.#tail = currentNode;
74 | }
75 | this.size++;
76 | return true;
77 | }
78 | append(data) {
79 | const newNode = new SinglyNode(data);
80 | if (this.#head === null) {
81 | this.#head = newNode;
82 | this.#tail = newNode;
83 | return true;
84 | }
85 | this.#tail.next = newNode;
86 | this.#tail = newNode;
87 | this.size++;
88 | return false;
89 | }
90 | add(data, position) {
91 | if (position === 0) {
92 | console.error("Use `prepend()` method to insert data at Head.");
93 | return false;
94 | } else if (position === this.size) {
95 | console.error("Use `append()` method to insert data at Tail.");
96 | return false;
97 | } else if (position > this.size && position != this.size + 1) {
98 | console.error(`Out of range. Size of the linkedList is ${this.size}`);
99 | return false;
100 | }
101 | let currentNode = this.#head;
102 | const iterator = addGenerator(currentNode, data, position);
103 | const iteratorNext = iterator.next();
104 | if (iteratorNext.value) {
105 | this.size++;
106 | return true;
107 | }
108 | return false;
109 | }
110 | getFromHead() {
111 | if (this.#head === null) {
112 | return false;
113 | }
114 | return this.#head.data;
115 | }
116 | getFromTail() {
117 | if (this.#tail === null) {
118 | return false;
119 | }
120 | return this.#tail.data;
121 | }
122 | log() {
123 | let currentNode = this.#head;
124 | while(currentNode !== null){
125 | console.log(currentNode.data);
126 | currentNode = currentNode.next;
127 | }
128 | }
129 | remove(key) {
130 | if (this.#head === null) {
131 | return false;
132 | } else if (this.#head.next === null) {
133 | this.#tail = null;
134 | }
135 | if (this.#head.data.key === key) {
136 | const prevHeadData = this.#head.data;
137 | this.#head = this.#head.next;
138 | if (this.#head.next === null) this.#tail = this.#head;
139 | this.size--;
140 | if (this.#head === null) this.size = 0;
141 | return true;
142 | }
143 | let previousNode = null;
144 | let currentNode = this.#head;
145 | while(currentNode !== null){
146 | if (key === currentNode.data.key) {
147 | const temp = currentNode.data;
148 | previousNode.next = currentNode.next;
149 | if (currentNode.next === null) this.#tail = previousNode;
150 | this.size--;
151 | return true;
152 | }
153 | previousNode = currentNode;
154 | currentNode = currentNode.next;
155 | }
156 | return false;
157 | }
158 | update(key, newValue) {
159 | if (this.#head === null) {
160 | return false;
161 | }
162 | if (this.#head.data.key === key) {
163 | this.#head.data.value = newValue;
164 | return this.#head.data;
165 | }
166 | if (this.#tail.data.key === key) {
167 | this.#tail.data.value = newValue;
168 | return this.#tail.data;
169 | }
170 | let currentNode = this.#head.next;
171 | const iterator = updateGenerator(key, currentNode, newValue);
172 | const iteratorNext = iterator.next();
173 | if (iteratorNext.value) {
174 | return iteratorNext.value;
175 | }
176 | return false;
177 | }
178 | search(key) {
179 | if (this.#head === null) {
180 | return false;
181 | }
182 | if (this.#head.data.key === key) {
183 | return this.#head.data;
184 | }
185 | if (this.#tail.data.key === key) {
186 | return this.#tail.data;
187 | }
188 | let currentNode = this.#head.next;
189 | const iterator = searchGenerator(key, currentNode);
190 | const iteratorNext = iterator.next();
191 | if (iteratorNext.value) {
192 | return iteratorNext.value;
193 | }
194 | return false;
195 | }
196 | iterator() {
197 | const currentNode = this.#head;
198 | const iterator = iteratorGenerator(currentNode);
199 | return iterator;
200 | }
201 | }
202 | export { SinglyLinkedList1 as SinglyLinkedList };
203 |
--------------------------------------------------------------------------------
/linkedList/singly/singlyLinkedList.ts:
--------------------------------------------------------------------------------
1 | import { LinkedListApi, NodeType, DataType } from "./helper.d.ts";
2 | import { SinglyNode } from "./singlyNode.ts";
3 | import {
4 | addGenerator,
5 | updateGenerator,
6 | searchGenerator,
7 | iteratorGenerator
8 | } from "./generators.ts"
9 |
10 | export class SinglyLinkedList implements LinkedListApi {
11 | #head: null|NodeType
12 | #tail: null|NodeType
13 | public size: number
14 |
15 | constructor() {
16 | this.#head = null
17 | this.#tail = null
18 | this.size = 0
19 | }
20 |
21 | static createSL() {
22 | return new this();
23 | }
24 |
25 | // insert in the head
26 | prepend(data: DataType) {
27 | if (this.#head === null) {
28 | const newNode = new SinglyNode(data);
29 | this.#head = newNode
30 | this.#tail = newNode
31 | return true;
32 | }
33 |
34 | const currentNode = this.#head
35 | this.#head = new SinglyNode(data)
36 | this.#head.next = currentNode
37 | if (currentNode.next === null) {
38 | this.#tail = currentNode
39 | }
40 | this.size++
41 |
42 | return true;
43 | }
44 |
45 | // inset in the tail
46 | append(data: DataType) {
47 | const newNode = new SinglyNode(data);
48 | if (this.#head === null) {
49 | this.#head = newNode
50 | this.#tail = newNode
51 | return true;
52 | }
53 |
54 | this.#tail!.next = newNode
55 | this.#tail = newNode
56 | this.size++
57 |
58 | return false;
59 | }
60 |
61 | // // add anywhere of the linked list except head & tail
62 | add(data: DataType, position: number) {
63 | if (position === 0) {
64 | console.error("Use `prepend()` method to insert data at Head.");
65 | return false;
66 | } else if (position === this.size) {
67 | console.error("Use `append()` method to insert data at Tail.");
68 | return false;
69 | } else if (position > this.size && position != this.size + 1) {
70 | console.error(`Out of range. Size of the linkedList is ${this.size}`);
71 | return false;
72 | }
73 |
74 | let currentNode = this.#head
75 | // generator function that returns an iterator
76 | const iterator = addGenerator(currentNode, data, position)
77 | const iteratorNext = iterator.next()
78 | if (iteratorNext.value) {
79 | this.size++
80 | return true
81 | }
82 |
83 | return false;
84 | }
85 |
86 | getFromHead() {
87 | if (this.#head === null) {
88 | return false
89 | }
90 |
91 | return this.#head.data;
92 | }
93 |
94 | getFromTail() {
95 | if (this.#tail === null) {
96 | return false
97 | }
98 |
99 | return this.#tail.data
100 | }
101 |
102 | log() {
103 | let currentNode = this.#head
104 | while (currentNode !== null) {
105 | console.log(currentNode.data);
106 | currentNode = currentNode.next
107 | }
108 | }
109 |
110 | remove(key: string|number) {
111 | if (this.#head === null) {
112 | return false;
113 | } else if (this.#head!.next === null) {
114 | this.#tail = null
115 | }
116 | if (this.#head.data.key === key) {
117 | const prevHeadData = this.#head.data
118 | this.#head = this.#head.next
119 | if (this.#head!.next === null) this.#tail = this.#head
120 |
121 | this.size--
122 | if (this.#head === null) this.size = 0
123 | return true;
124 | }
125 |
126 | // two pointer approach
127 | let previousNode: NodeType = null;
128 | let currentNode: NodeType = this.#head
129 | while (currentNode !== null) {
130 | if (key === currentNode.data.key) {
131 | const temp = currentNode.data
132 | previousNode!.next = currentNode.next
133 | if (currentNode!.next === null) this.#tail = previousNode
134 |
135 | this.size--
136 | return true;
137 | }
138 |
139 | previousNode = currentNode
140 | currentNode = currentNode.next
141 | }
142 |
143 | return false;
144 | }
145 |
146 | update(key: string|number, newValue: any) {
147 | if (this.#head === null) {
148 | return false;
149 | }
150 |
151 | if (this.#head.data.key === key) {
152 | this.#head.data.value = newValue
153 | return this.#head.data;
154 | }
155 | if (this.#tail!.data.key === key) {
156 | this.#tail!.data.value = newValue
157 | return this.#tail!.data;
158 | }
159 |
160 | let currentNode = this.#head.next
161 | // generator function that returns an iterator
162 | const iterator = updateGenerator(key, currentNode, newValue)
163 | const iteratorNext = iterator.next()
164 | if (iteratorNext.value) {
165 | return iteratorNext.value
166 | }
167 |
168 | return false;
169 | }
170 |
171 | search(key: string|number) {
172 | if (this.#head === null) {
173 | return false;
174 | }
175 |
176 | if (this.#head.data.key === key) {
177 | return this.#head.data;
178 | }
179 | if (this.#tail!.data.key === key) {
180 | return this.#tail!.data
181 | }
182 |
183 | let currentNode = this.#head.next
184 | // generator function that returns an iterator
185 | const iterator = searchGenerator(key, currentNode)
186 | const iteratorNext = iterator.next()
187 | if (iteratorNext.value) {
188 | return iteratorNext.value
189 | }
190 |
191 | return false;
192 | }
193 |
194 | iterator() {
195 | const currentNode = this.#head
196 | // generator function that returns an iterator
197 | const iterator = iteratorGenerator(currentNode)
198 | return iterator
199 | }
200 | }
201 |
--------------------------------------------------------------------------------
/linkedList/singly/singlyNode.ts:
--------------------------------------------------------------------------------
1 | import { NodeType, DataType } from "./helper.d.ts";
2 |
3 | export class SinglyNode {
4 | public data: DataType
5 | public next: NodeType|null
6 |
7 | constructor(data: DataType) {
8 | this.data = data;
9 | this.next = null;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/linkedList/singly/test.ts:
--------------------------------------------------------------------------------
1 | import { SinglyLinkedList } from "./singlyLinkedList.ts";
2 | // import { SinglyLinkedList } from "https://deno.land/x/datastructure/mod.ts"
3 |
4 | const singlyLinkedList = SinglyLinkedList.createSL()
5 |
6 | singlyLinkedList.prepend({key: 'a', value: 'apple'})
7 | singlyLinkedList.prepend({key: 'b', value: [1, 2, 3]})
8 | singlyLinkedList.prepend({key: 11, value: 'add to head'})
9 | // singlyLinkedList.append({key: 'c', value: 'add to last'})
10 | // singlyLinkedList.add({key: 'x', value: 'X'}, 1)
11 | // singlyLinkedList.add({key: 'x', value: 'X'}, 1)
12 |
13 | // console.log(singlyLinkedList.getFromHead());
14 | // console.log(singlyLinkedList.getFromTail());
15 | // console.log(singlyLinkedList.remove('c'));
16 |
17 | // console.log(singlyLinkedList.update('b', 'after head'));
18 | // console.log(singlyLinkedList.search('a'));
19 |
20 | // const it = singlyLinkedList.iterator()
21 | // let itNext = it.next()
22 | // console.log(it.next(), it.next(), it.next());
23 | // while (itNext.done === false) {
24 | // console.log(itNext.value);
25 | // itNext = it.next()
26 | // }
27 |
28 | // console.log(singlyLinkedList);
29 | singlyLinkedList.log()
30 |
--------------------------------------------------------------------------------
/mod.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Data Structure
3 | *
4 | * Different Data Structures implementation API provided by this module.
5 | * API available in TypeScript & JavaScript.
6 | *
7 | * @package dataStructure
8 | * @author Keramot UL Islam [Abm Sourav]
9 | * @email
10 | * @authorUrl https://abmsourav.com
11 | * @sourceCode https://github.com/AbmSourav/dataStructure
12 | * @version 1.2.1
13 | *
14 | * Copyright (c) 2021 Keramot UL Islam
15 | */
16 |
17 | export { BlockChain } from "./blockChain/blockChain.bundle.js";
18 | export { HashTable } from "./hashTable/hashTable.bundle.js";
19 | export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.bundle.js";
20 | export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.bundle.js";
21 | export { Stack } from "./stack/stack.bundle.js"
22 | export { Queue } from "./queue/queue.bundle.js"
23 |
--------------------------------------------------------------------------------
/mod.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Data Structure
3 | *
4 | * Different Data Structures implementation API provided by this module.
5 | * API available in TypeScript & JavaScript.
6 | *
7 | * @package dataStructure
8 | * @author Keramot UL Islam [Abm Sourav]
9 | * @email
10 | * @authorUrl https://abmsourav.com
11 | * @sourceCode https://github.com/AbmSourav/dataStructure
12 | * @version 1.2.1
13 | *
14 | * Copyright (c) 2021 Keramot UL Islam
15 | */
16 |
17 | export { BlockChain } from "./blockChain/blockChain.ts";
18 | export { HashTable } from "./hashTable/hashTable.ts";
19 | export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.ts";
20 | export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.ts";
21 | export { Stack } from "./stack/stack.ts"
22 | export { Queue } from "./queue/queue.ts"
23 |
--------------------------------------------------------------------------------
/queue/generators.ts:
--------------------------------------------------------------------------------
1 | import { QueueType } from "./helper.d.ts";
2 |
3 | export function *searchGenerator(key: string|number, currentNode: QueueType) {
4 | while (currentNode !== null) {
5 | if (key === currentNode.data.key) {
6 | return currentNode.data
7 | }
8 | currentNode = currentNode.next
9 | }
10 | return false
11 | }
12 |
13 | export function *updateGenerator(key: string|number, currentNode: QueueType, newValue: any) {
14 | while (currentNode !== null) {
15 | if (key === currentNode.data.key) {
16 | currentNode.data.value = newValue
17 | return currentNode.data
18 | }
19 |
20 | currentNode = currentNode.next
21 | }
22 | return false
23 | }
24 |
25 | export function *iteratorGenerator(currentNode: QueueType) {
26 | while (currentNode !== null) {
27 | yield currentNode.data;
28 |
29 | currentNode = currentNode.next
30 | }
31 | return false
32 | }
33 |
--------------------------------------------------------------------------------
/queue/helper.d.ts:
--------------------------------------------------------------------------------
1 | // data type
2 | export type DataType = {
3 | key: string|number
4 | value: T
5 | }
6 |
7 | export type QueueType = {
8 | data: DataType
9 | next: null|QueueType
10 | }|null
11 |
12 | // Stack interface
13 | export interface QueueApi {
14 | size: number;
15 | getFront(): null|DataType
16 | getBack(): null|DataType
17 | enqueue(data: DataType): boolean
18 | dequeue(): boolean|DataType
19 | search(key: string|number): boolean|DataType
20 | update(key: string|number, newValue: any): boolean|DataType
21 | log(): void
22 | }
23 |
--------------------------------------------------------------------------------
/queue/queue.bundle.js:
--------------------------------------------------------------------------------
1 | class QueueNode {
2 | data;
3 | next = null;
4 | constructor(data1){
5 | this.data = data1;
6 | this.next = null;
7 | }
8 | }
9 | function* searchGenerator(key, currentNode) {
10 | while(currentNode !== null){
11 | if (key === currentNode.data.key) {
12 | return currentNode.data;
13 | }
14 | currentNode = currentNode.next;
15 | }
16 | return false;
17 | }
18 | function* updateGenerator(key, currentNode, newValue) {
19 | while(currentNode !== null){
20 | if (key === currentNode.data.key) {
21 | currentNode.data.value = newValue;
22 | return currentNode.data;
23 | }
24 | currentNode = currentNode.next;
25 | }
26 | return false;
27 | }
28 | function* iteratorGenerator(currentNode) {
29 | while(currentNode !== null){
30 | yield currentNode.data;
31 | currentNode = currentNode.next;
32 | }
33 | return false;
34 | }
35 | class Queue1 {
36 | #frontNode;
37 | #backNode;
38 | size;
39 | constructor(){
40 | this.#frontNode = null;
41 | this.#backNode = null;
42 | this.size = 0;
43 | }
44 | static createQueue() {
45 | return new this();
46 | }
47 | getFront() {
48 | if (this.#frontNode === null) return null;
49 | return this.#frontNode.data;
50 | }
51 | getBack() {
52 | if (this.#frontNode === null) return null;
53 | return this.#backNode.data;
54 | }
55 | enqueue(data) {
56 | const newNode = new QueueNode(data);
57 | if (this.#frontNode === null) {
58 | this.#frontNode = newNode;
59 | this.#backNode = newNode;
60 | return true;
61 | }
62 | this.#backNode.next = newNode;
63 | this.#backNode = newNode;
64 | this.size++;
65 | return true;
66 | }
67 | dequeue() {
68 | if (this.#frontNode === null) {
69 | return false;
70 | } else if (this.#frontNode.next === null) {
71 | this.#backNode = null;
72 | }
73 | const node = this.#frontNode;
74 | this.#frontNode = node.next;
75 | this.size--;
76 | if (this.#frontNode === null) this.size = 0;
77 | return node.data;
78 | }
79 | search(key) {
80 | if (this.#frontNode === null) {
81 | return false;
82 | }
83 | if (key === this.#frontNode.data.key) {
84 | return this.#frontNode.data;
85 | }
86 | if (key === this.#backNode.data.key) {
87 | return this.#backNode.data;
88 | }
89 | const iterator = searchGenerator(key, this.#frontNode);
90 | const iteratorNext = iterator.next();
91 | if (iteratorNext.value) {
92 | return iteratorNext.value;
93 | }
94 | return false;
95 | }
96 | update(key, newValue) {
97 | if (this.#frontNode === null) {
98 | return false;
99 | }
100 | if (key === this.#frontNode.data.key) {
101 | this.#frontNode.data.value = newValue;
102 | return this.#frontNode.data;
103 | }
104 | if (key === this.#backNode.data.key) {
105 | this.#backNode.data.value = newValue;
106 | return this.#backNode.data;
107 | }
108 | const iterator = updateGenerator(key, this.#frontNode, newValue);
109 | const iteratorNext = iterator.next();
110 | if (iteratorNext.value) {
111 | return iteratorNext.value;
112 | }
113 | return false;
114 | }
115 | log() {
116 | const iterator = this.iterator();
117 | let iteratorNext = iterator.next();
118 | while(iteratorNext.done === false){
119 | console.log(iteratorNext.value);
120 | iteratorNext = iterator.next();
121 | }
122 | }
123 | iterator() {
124 | const iterator = iteratorGenerator(this.#frontNode);
125 | return iterator;
126 | }
127 | }
128 | export { Queue1 as Queue };
129 |
--------------------------------------------------------------------------------
/queue/queue.ts:
--------------------------------------------------------------------------------
1 | import { QueueType, DataType, QueueApi } from "./helper.d.ts"
2 | import { QueueNode } from "./queueNode.ts"
3 | import {
4 | searchGenerator,
5 | updateGenerator,
6 | iteratorGenerator
7 | } from "./generators.ts"
8 |
9 |
10 | export class Queue implements QueueApi {
11 | #frontNode: QueueType
12 | #backNode: QueueType
13 | public size: number
14 |
15 | constructor() {
16 | this.#frontNode = null
17 | this.#backNode = null
18 | this.size = 0
19 | }
20 |
21 | static createQueue() {
22 | return new this();
23 | }
24 |
25 | getFront() {
26 | if (this.#frontNode === null) return null
27 | return this.#frontNode!.data
28 | }
29 |
30 | getBack() {
31 | if (this.#frontNode === null) return null
32 | return this.#backNode!.data
33 | }
34 |
35 | enqueue(data: DataType) {
36 | const newNode = new QueueNode(data)
37 | if (this.#frontNode === null) {
38 | // adding same pointer for frontNode & backNode
39 | this.#frontNode = newNode
40 | this.#backNode = newNode
41 | return true;
42 | }
43 |
44 | // pointer and it's greatness
45 | this.#backNode!.next = newNode
46 | this.#backNode = newNode
47 | this.size++
48 |
49 | return true;
50 | }
51 |
52 | dequeue() {
53 | if (this.#frontNode === null) {
54 | return false;
55 | } else if (this.#frontNode!.next === null) {
56 | this.#backNode = null
57 | }
58 |
59 | const node = this.#frontNode
60 | this.#frontNode = node!.next
61 | this.size--
62 | if (this.#frontNode === null) this.size = 0
63 |
64 | return node!.data;
65 | }
66 |
67 | search(key: string|number) {
68 | if (this.#frontNode === null) {
69 | return false
70 | }
71 |
72 | if (key === this.#frontNode!.data.key) {
73 | return this.#frontNode!.data
74 | }
75 | if (key === this.#backNode!.data.key) {
76 | return this.#backNode!.data
77 | }
78 |
79 | // generator function that returns an iterator
80 | const iterator = searchGenerator(key, this.#frontNode)
81 | const iteratorNext = iterator.next()
82 | if (iteratorNext.value) {
83 | return iteratorNext.value
84 | }
85 |
86 | return false
87 | }
88 |
89 | update(key: string|number, newValue: any) {
90 | if (this.#frontNode === null) {
91 | return false
92 | }
93 |
94 | if (key === this.#frontNode!.data.key) {
95 | this.#frontNode!.data.value = newValue
96 | return this.#frontNode!.data
97 | }
98 | if (key === this.#backNode!.data.key) {
99 | this.#backNode!.data.value = newValue
100 | return this.#backNode!.data
101 | }
102 |
103 | // generator function that returns an iterator
104 | const iterator = updateGenerator(key, this.#frontNode, newValue)
105 | const iteratorNext = iterator.next()
106 | if (iteratorNext.value) {
107 | return iteratorNext.value
108 | }
109 |
110 | return false
111 | }
112 |
113 | log() {
114 | const iterator = this.iterator()
115 | let iteratorNext = iterator.next()
116 |
117 | while (iteratorNext.done === false) {
118 | console.log(iteratorNext.value);
119 | iteratorNext = iterator.next()
120 | }
121 | }
122 |
123 | iterator() {
124 | // generator function that returns an iterator
125 | const iterator = iteratorGenerator(this.#frontNode)
126 | return iterator
127 | }
128 | }
129 |
--------------------------------------------------------------------------------
/queue/queueNode.ts:
--------------------------------------------------------------------------------
1 | import { DataType, QueueType } from "./helper.d.ts"
2 |
3 | export class QueueNode {
4 | public data: DataType
5 | public next: QueueType|null = null
6 |
7 | constructor(data: DataType) {
8 | this.data = data;
9 | this.next = null;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/queue/readme.md:
--------------------------------------------------------------------------------
1 | ## Queue Api
2 |
3 | ```ts
4 | DataType = {
5 | key: string|number
6 | value: T
7 | }
8 |
9 | // static method that creates a instance of Queue class.
10 | Queue.createQueue()
11 |
12 | // Time Complexity: O(1)
13 | size: number;
14 |
15 | // *generator function, it returens an iterator.
16 | iterator(): Generator
17 |
18 | // Time Complexity: O(1)
19 | getFront(): number|DataType
20 |
21 | // Time Complexity: O(1)
22 | getBack(): number|DataType
23 |
24 | // Time Complexity: O(1)
25 | enqueue(data: DataType): boolean
26 |
27 | // Time Complexity: O(1)
28 | dequeue(): boolean|DataType
29 |
30 | // Time Complexity:
31 | // Front Node & Back Node: O(1), other Nodes: O(n)
32 | search(key: string|number): null|DataType
33 |
34 | // Time Complexity:
35 | // Front Node & Back Node: O(1), other Nodes: O(n)
36 | update(key: string|number, newValue: any): boolean|DataType
37 |
38 | // Time Complexity: O(n)
39 | log(): void;
40 | ```
41 |
42 |
43 |
44 |
45 | ## Example
46 | ```ts
47 | import { Queue } from "https://deno.land/x/datastructure/mod.ts";
48 |
49 | const queue = Queue.createQueue()
50 |
51 | // add data in the Queue in a FIFO way
52 | queue.enqueue({key: 'a', value: [1, 2, 5]})
53 | queue.enqueue({key: 'sourav', value: {name: "Sourav"}})
54 |
55 | // get and remove first added data from the Queue
56 | queue.dequeue()
57 |
58 | // get the item which added at first
59 | queue.getFront()
60 |
61 | // get the item which added at last
62 | queue.getBack()
63 |
64 | // search item data in the Queue
65 | queue.search('sourav')
66 |
67 | // update item data
68 | queue.update('sourav', 'Abm Sourav')
69 |
70 | // iterator method returns a *generator function
71 | const iterator = queue.iterator()
72 | let iteratorNext = iterator.next()
73 | // console.log(iteratorNext.next(), iteratorNext.next());
74 | while (iteratorNext.done === false) {
75 | console.log(iteratorNext.value);
76 | iteratorNext = iterator.next()
77 | }
78 |
79 | // console all values
80 | queue.log()
81 |
82 | ```
83 |
--------------------------------------------------------------------------------
/queue/test.ts:
--------------------------------------------------------------------------------
1 | // import { Queue } from "https://deno.land/x/datastructure/mod.ts";
2 | import { Queue } from "./queue.ts";
3 |
4 | const queue = Queue.createQueue()
5 | queue.enqueue({key: 'a', value: 'aa'});
6 | queue.enqueue({key: 'b', value: [1, 2, 5]})
7 | queue.enqueue({key: 'sourav', value: {name: "Sourav"}})
8 |
9 | // console.log(queue.getFront(), queue.getBack());
10 |
11 | // queue.dequeue()
12 | // console.log(queue.update('sourav', 'Abm'));
13 | // console.log(queue.search('b'));
14 |
15 | // const iterator = queue.iterator()
16 | // let iteratorNext = iterator.next()
17 | // console.log(iterator.next(), iterator.next(), iterator.next());
18 | // while (iteratorNext.done === false) {
19 | // console.log(iteratorNext.value);
20 | // iteratorNext = iterator.next()
21 | // }
22 |
23 | // console.log(queue);
24 | queue.log()
25 |
--------------------------------------------------------------------------------
/stack/generators.ts:
--------------------------------------------------------------------------------
1 | import { StackType } from "./helper.d.ts";
2 |
3 | export function *searchGenerator(key: string|number, currentNode: StackType) {
4 | while (currentNode !== null) {
5 | if (key === currentNode.data.key) {
6 | return currentNode.data
7 | }
8 | currentNode = currentNode.next
9 | }
10 | return false
11 | }
12 |
13 | export function *updateGenerator(key: string|number, currentNode: StackType, newValue: any) {
14 | while (currentNode !== null) {
15 | if (key === currentNode.data.key) {
16 | currentNode.data.value = newValue
17 | return currentNode.data
18 | }
19 |
20 | currentNode = currentNode.next
21 | }
22 | return false
23 | }
24 |
25 | export function *iteratorGenerator(currentNode: StackType) {
26 | while (currentNode !== null) {
27 | yield currentNode.data;
28 |
29 | currentNode = currentNode.next
30 | }
31 | return false
32 | }
33 |
--------------------------------------------------------------------------------
/stack/helper.d.ts:
--------------------------------------------------------------------------------
1 | // data type
2 | export type DataType = {
3 | key: string|number
4 | value: T
5 | }
6 |
7 | export type StackType = {
8 | data: DataType
9 | next: null|StackType
10 | }|null
11 |
12 | // Stack interface
13 | export interface StackApi {
14 | size: number;
15 | getTop(): null|DataType
16 | getBottom(): null|DataType
17 | push(data: DataType): boolean
18 | pop(): boolean|DataType
19 | search(key: string|number): boolean|DataType
20 | update(key: string|number, newValue: any): boolean|DataType
21 | log(): void
22 | iterator(): Generator
23 | }
24 |
--------------------------------------------------------------------------------
/stack/readme.md:
--------------------------------------------------------------------------------
1 | ## Stack Api
2 |
3 | ```ts
4 | DataType = {
5 | key: string|number
6 | value: T
7 | }
8 |
9 | // static method that creates a instance of `Stack` class.
10 | Stack.createStack()
11 |
12 | // Time Complexity: O(1)
13 | size: number;
14 |
15 | // *generator function, it returens an iterator.
16 | iterator(): Generator
17 |
18 | // Time Complexity: O(1)
19 | getTop(): null|DataType
20 |
21 | // Time Complexity: O(1)
22 | getBottom(): null|DataType
23 |
24 | // Time Complexity: O(1)
25 | push(data: DataType): boolean
26 |
27 | // Time Complexity: O(1)
28 | pop(): boolean|DataType
29 |
30 | // Time Complexity:
31 | // Top Node & Bottom Node: O(1), other Nodes: O(n)
32 | search(key: string|number): null|DataType
33 |
34 | // Time Complexity:
35 | // Top Node & Bottom Node: O(1), other Nodes: O(n)
36 | update(key: string|number, newValue: any): boolean|DataType
37 |
38 | // Time Complexity: O(n)
39 | log(): void;
40 | ```
41 |
42 |
43 |
44 |
45 | ## Example
46 | ```ts
47 | import { Stack } from "https://deno.land/x/datastructure/mod.ts";
48 |
49 | const stack = Stack.createStack()
50 |
51 | // stack length/size
52 | stack.size
53 |
54 | // iterator method returns a *generator function
55 | const iterator = stack.iterator()
56 | let iteratorNext = iterator.next()
57 | // console.log(iteratorNext.next(), iteratorNext.next());
58 | while (iteratorNext.done === false) {
59 | console.log(iteratorNext.value);
60 | iteratorNext = iterator.next()
61 | }
62 |
63 | // add data in the stack in a LIFO way
64 | stack.push({key: 'a', value: 'apple'})
65 | stack.push({key: 'b', value: [1, 2, 4]})
66 | stack.push({key: 'b', value: {name: 'AbmSourav'}})
67 |
68 | // get data from stack (LIFO)
69 | stack.pop()
70 |
71 | // searching data in the stack
72 | stack.search('a');
73 |
74 | // get the last added item of the stack
75 | stack.getTop()
76 |
77 | // get the item that added at fast
78 | stack.getBottom()
79 |
80 | // update stack item data
81 | stack.update('a', {name: 'Abm Sourav'})
82 |
83 | // console all values
84 | stack.log()
85 | ```
86 |
--------------------------------------------------------------------------------
/stack/stack.bundle.js:
--------------------------------------------------------------------------------
1 | class StackNode {
2 | data;
3 | next = null;
4 | constructor(data1){
5 | this.data = data1;
6 | this.next = null;
7 | }
8 | }
9 | function* searchGenerator(key, currentNode) {
10 | while(currentNode !== null){
11 | if (key === currentNode.data.key) {
12 | return currentNode.data;
13 | }
14 | currentNode = currentNode.next;
15 | }
16 | return false;
17 | }
18 | function* updateGenerator(key, currentNode, newValue) {
19 | while(currentNode !== null){
20 | if (key === currentNode.data.key) {
21 | currentNode.data.value = newValue;
22 | return currentNode.data;
23 | }
24 | currentNode = currentNode.next;
25 | }
26 | return false;
27 | }
28 | function* iteratorGenerator(currentNode) {
29 | while(currentNode !== null){
30 | yield currentNode.data;
31 | currentNode = currentNode.next;
32 | }
33 | return false;
34 | }
35 | class Stack1 {
36 | #topNode;
37 | #bottomNode;
38 | size;
39 | constructor(){
40 | this.#topNode = null;
41 | this.#bottomNode = null;
42 | this.size = 0;
43 | }
44 | static createStack() {
45 | return new this();
46 | }
47 | getTop() {
48 | if (this.#topNode === null) return null;
49 | return this.#topNode.data;
50 | }
51 | getBottom() {
52 | if (this.#topNode === null) return null;
53 | return this.#bottomNode.data;
54 | }
55 | push(data) {
56 | const newNode = new StackNode(data);
57 | if (this.#topNode === null) {
58 | this.#topNode = newNode;
59 | this.#bottomNode = newNode;
60 | return true;
61 | }
62 | newNode.next = this.#topNode;
63 | if (newNode.next === null) {
64 | this.#bottomNode = newNode.next;
65 | }
66 | this.#topNode = newNode;
67 | this.size++;
68 | return true;
69 | }
70 | pop() {
71 | if (this.#topNode === null) {
72 | return false;
73 | } else if (this.#topNode.next === null) {
74 | this.#bottomNode = null;
75 | }
76 | const node = this.#topNode;
77 | this.#topNode = node.next;
78 | this.size--;
79 | if (this.#topNode === null) this.size = 0;
80 | return node.data;
81 | }
82 | search(key) {
83 | if (this.#topNode === null) {
84 | return false;
85 | }
86 | if (key === this.#topNode.data.key) {
87 | return this.#topNode.data;
88 | }
89 | if (key === this.#bottomNode.data.key) {
90 | return this.#bottomNode.data;
91 | }
92 | const iterator = searchGenerator(key, this.#topNode);
93 | const iteratorNext = iterator.next();
94 | if (iteratorNext.value) {
95 | return iteratorNext.value;
96 | }
97 | return false;
98 | }
99 | update(key, newValue) {
100 | if (this.#topNode === null) {
101 | return false;
102 | }
103 | if (key === this.#topNode.data.key) {
104 | this.#topNode.data.value = newValue;
105 | return this.#topNode.data;
106 | }
107 | if (key === this.#bottomNode.data.key) {
108 | this.#bottomNode.data.value = newValue;
109 | return this.#bottomNode.data;
110 | }
111 | const iterator = updateGenerator(key, this.#topNode, newValue);
112 | const iteratorNext = iterator.next();
113 | if (iteratorNext.value) {
114 | return iteratorNext.value;
115 | }
116 | return false;
117 | }
118 | log() {
119 | let currentNode = this.#topNode;
120 | while(currentNode !== null){
121 | console.log(currentNode.data);
122 | currentNode = currentNode.next;
123 | }
124 | }
125 | iterator() {
126 | const iterator = iteratorGenerator(this.#topNode);
127 | return iterator;
128 | }
129 | }
130 | export { Stack1 as Stack };
131 |
--------------------------------------------------------------------------------
/stack/stack.ts:
--------------------------------------------------------------------------------
1 | import { StackType, DataType, StackApi } from "./helper.d.ts"
2 | import { StackNode } from "./stackNode.ts"
3 | import {
4 | searchGenerator,
5 | updateGenerator,
6 | iteratorGenerator
7 | } from "./generators.ts"
8 |
9 |
10 | export class Stack implements StackApi {
11 | #topNode: StackType
12 | #bottomNode: StackType
13 | public size: number
14 |
15 | constructor() {
16 | this.#topNode = null
17 | this.#bottomNode = null
18 | this.size = 0
19 | }
20 |
21 | static createStack() {
22 | return new this();
23 | }
24 |
25 | getTop() {
26 | if (this.#topNode === null) return null;
27 | return this.#topNode!.data
28 | }
29 |
30 | getBottom() {
31 | if (this.#topNode === null) return null;
32 | return this.#bottomNode!.data
33 | }
34 |
35 | push(data: DataType) {
36 | const newNode = new StackNode(data)
37 | if (this.#topNode === null) {
38 | this.#topNode = newNode
39 | this.#bottomNode = newNode
40 | return true;
41 | }
42 |
43 | newNode.next = this.#topNode
44 | if (newNode.next === null) {
45 | this.#bottomNode = newNode.next
46 | }
47 | this.#topNode = newNode
48 | this.size++
49 |
50 | return true;
51 | }
52 |
53 | pop() {
54 | if (this.#topNode === null) {
55 | return false;
56 | } else if (this.#topNode!.next === null) {
57 | this.#bottomNode = null
58 | }
59 |
60 | const node = this.#topNode
61 | this.#topNode = node!.next
62 | this.size--
63 | if (this.#topNode === null) this.size = 0
64 |
65 | return node!.data;
66 | }
67 |
68 | search(key: string|number) {
69 | if (this.#topNode === null) {
70 | return false
71 | }
72 |
73 | if (key === this.#topNode!.data.key) {
74 | return this.#topNode!.data
75 | }
76 | if (key === this.#bottomNode!.data.key) {
77 | return this.#bottomNode!.data
78 | }
79 |
80 | // generator function that returns an iterator
81 | const iterator = searchGenerator(key, this.#topNode)
82 | const iteratorNext = iterator.next()
83 | if (iteratorNext.value) {
84 | return iteratorNext.value
85 | }
86 |
87 | return false
88 | }
89 |
90 | update(key: string|number, newValue: any) {
91 | if (this.#topNode === null) {
92 | return false
93 | }
94 |
95 | if (key === this.#topNode!.data.key) {
96 | this.#topNode!.data.value = newValue
97 | return this.#topNode!.data
98 | }
99 | if (key === this.#bottomNode!.data.key) {
100 | this.#bottomNode!.data.value = newValue
101 | return this.#bottomNode!.data
102 | }
103 |
104 | // generator function that returns an iterator
105 | const iterator = updateGenerator(key, this.#topNode, newValue)
106 | const iteratorNext = iterator.next()
107 | if (iteratorNext.value) {
108 | return iteratorNext.value
109 | }
110 |
111 | return false
112 | }
113 |
114 | log() {
115 | let currentNode = this.#topNode
116 | while (currentNode !== null) {
117 | console.log(currentNode.data);
118 | currentNode = currentNode.next
119 | }
120 | }
121 |
122 | iterator() {
123 | // generator function that returns an iterator
124 | const iterator = iteratorGenerator(this.#topNode)
125 | return iterator
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/stack/stackNode.ts:
--------------------------------------------------------------------------------
1 | import { DataType, StackType } from "./helper.d.ts"
2 |
3 | export class StackNode {
4 | public data: DataType
5 | public next: StackType|null = null
6 |
7 | constructor(data: DataType) {
8 | this.data = data;
9 | this.next = null;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/stack/test.ts:
--------------------------------------------------------------------------------
1 | // import { Stack } from "https://deno.land/x/datastructure/mod.ts";
2 | import { Stack } from "./stack.ts";
3 |
4 | const stack = Stack.createStack()
5 |
6 | stack.push({key: 'a', value: 'apple'})
7 | stack.push({key: 'b', value: [1, 2, 4]})
8 | stack.push({key: 'c', value: [8, 9, 2]})
9 | stack.push({key: 'd', value: 'dd'})
10 |
11 | // stack.pop();
12 | // console.log(stack.update('d', {name: 'Sourav'}));
13 |
14 | // console.log(stack.getTop(), stack.getBottom());
15 | // console.log(stack.search('d'));
16 |
17 | const iterator = stack.iterator()
18 | // let iteratorNext = iterator.next()
19 | console.log(iterator.next(), iterator.next(), iterator.next());
20 | // while (iteratorNext.done === false) {
21 | // console.log(iteratorNext.value);
22 | // iteratorNext = iterator.next()
23 | // }
24 |
25 | // console.log(stack)
26 | // stack.log()
27 |
--------------------------------------------------------------------------------