structure-data-collection
v1.0.4
Published
data collection that is easy and efficient to use
Downloads
8
Readme
structure-data-collection
A simple implementation of a linked list in TypeScript.
Table of Contents
Installation
You can install this package via npm:
npm install structure-data-collection
Usage
Collection List
Here is an example of how to use the Collection.List class:
import { Collection } from "structure-data-collection";
const list = new Collection.List<number>();
list.add(10);
list.add(5);
list.add(15);
list.contain(10); // Output: true
list.contain(100); //Output: false
list.indexOf(10); //Output: 1
list.indexOf(100); //Output: -1
list.size(); //Output: 3
list.get(1); //Output: 10
list.get(100); //throw error index is not bound limit
console.log(list.toJSON()) // { data: 15, next: { data: 5, next: { data: 10, next: null } } }
console.log(list.toArray()); // Output: [10, 5, 15]
Collection Linked List
Here is an example of how to use the Collection.LinkedList class:
import { Collection } from "structure-data-collection";
const linkedList = new Collection.LinkedList<number>();
linkedList.appendLast(200);
linkedList.appendFirst(10);
linkedList.appendFirst(20);
linkedList.append(1, 80);
console.log(linkedList.toReverseArray()) //Output: [200, 10, 80, 20]
console.log(linkedList.toArray()) // Output: [20, 80, 10, 200]
linkedList.removeFirst();
console.log(linkedList.toArray()); //// Output: [80, 10, 200]
console.log(linkedList.contain(80)); // Output: true
console.log(linkedList.contain(20)); // Output: false
console.log(linkedList.get(0)) // Output: 80
console.log(linkedList.indexOf(1000)); //Output: -1
Collection Queue
Here is an example of how to use the Collection.Queue class:
import { Collection } from "structure-data-collection";
const queue = new Collection.Queue<string>();
queue.enqueue("Aldo Ratmawan");
queue.enqueue("John Alex");
queue.enqueue("Erwin Clark");
console.log(queue.size()); // Output: 3
console.log(queue.peek()); // Output: Aldo Ratmawan
console.log(queue.isEmpty()) ; // Output: false
console.log(queue.dequeue()) // Output: Aldo Ratmawan
console.log(queue.peek()) // Output: John Alex
console.log(queue.toString()) // Output: John Alex Erwin Clark
queue.clear();
console.log(queue.isEmpty()); //Output: true
Collection Stack
Here is an example of how to use the Collection.Stack class:
import { Collection } from "structure-data-collection";
const stack = new Collection.Stack<number>();
stack.push(100);
stack.push(200);
stack.push(300);
stack.push(400);
console.log(stack.size()); // Output: 4
console.log(stack.peek()); // Output: 100
console.log(stack.pop()); // Output: 100
console.log(stack.size()); // Output: 3
console.log(stack.peek()); // Output: 200
console.log(stack.isEmpty()); // Output: false
stack.clear();
console.log(stack.isEmpty()) ; // Output: true
Collection HasMap
Here is an example of how to use the Collection.HasMap class:
import { Collection } from "structure-data-collection";
const hashMap = new Collection.HasMap<string, number>;
hashMap.put("one", 1);
hashMap.put("two", 2);
hashMap.put("tree", 3);
hashMap.values(); // Output: [1, 2, 3]
hashMap.keys(); // Output: [on, two, three]
hashMap.containKey("one"); //Output: true
Collection TreeNode
Here is an example of how to use the Collection.BinaryTree class:
import { Collection } from "strukture-data-collection";
const treeNode = new Collection.BinaryTree<number>();
treeNode.add(100);
treeNode.add(200);
treeNode.add(300);
treeNode.add(400);
treeNode.height();
treeNode.inorderTraversal((data) => {
console.log(data)
});
treeNode.postorderTraversal((data) => {
console.log(data)
});
treeNode.preorderTraversal((data) => {
console.log(data)
})
API
Collection.List<T>
Methods
add(data: T): void
- Adds a new element to the list.
remove() : void
- Removes the first element from the list
get() : T | undifined
- Retrieves the element at the specified index
size() : number
- Gets the number of elements in the list
indexOf(data: T) : number
- Searches for the index of a particular element
contain(data: T) : boolean
- Checks whether a particular element exists in a list
forEach(callback: (data: T, index: number) => void) : void
- Iterates over each element in the list and runs the callback
toJSON() : object
- Converts a list to JSON format
toArray(): Array<T>
- Converts the list to an array.
Collection.LinkedList<T>
Methods
appendFirst(data: T): void
- Adds a new element to the front of the list
appendLast(data: T): void
- Add a new element to the end of the list
append(index: number, data: T): void
- Add a new element at a specific index
removeFirst() : void
- Remove the first element in the list
removeLast(): void
- Remove the last element in the list
remove(data: T): void
- Remove an element based on its value
removeAt(index: number): void
- Remove an element at a specific index
get(index: number): T | null
- Get an element at a specific index
indexOf(data: T): number
- Find the index of a specific element
contain(data: T): boolean
- Check if a specific element is in the list
size(): number
- Get the number of elements in the lis
toTree(): Node.LinkedList<T> | null
- Return the head of the list
toArray(): T[]
- Convert the list to an array
toReverseArray(): T[]
- Convert the list to a reversed array
Licence
Copyright © 2024 Aldo Ratmawan
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Author
name : Aldo Ratmawan
email: [email protected]