Data Structure

Implement different Data Structures using TypeScript.


Singly Linked List

import { SinglyLinkedList } from "https://deno.land/x/datastructure/mod.ts";

const singlyLinkedList = new SinglyLinkedList()

// Time Complexity: O(1)
singlyLinkedList.size    // get the size of the linked list

// Time Complexity: O(1)
singlyLinkedList.prepend('a')    // insert in the head

// Time Complexity: O(n)
singlyLinkedList.append('c')    // insert in the tail

// Time Complexity: O(n)
singlyLinkedList.add('x', 2)    // insert anywhere except head & tail

// Time Complexity: O(1)
singlyLinkedList.getFromHead()
// Time Complexity: O(1)
singlyLinkedList.getFromTail()

// Time Complexity: O(n)
singlyLinkedList.print()    // print all values

// Time Complexity:
// head node: O(1), other nodes: O(n)
singlyLinkedList.remove('a')

// Time Complexity:
// head node: O(1), other nodes: O(n)
singlyLinkedList.update('a', 'apple')


Stack

import { Stack } from "https://deno.land/x/datastructure/mod.ts";

const stack = new Stack()

// Time Complexity: O(1)
stack.size

// Time Complexity: O(1)
// return: boolean
stack.push('Sourav')

// Time Complexity: O(1)
// return: string|number|boolean
stack.pop()

// Time Complexity:
//    Top Node & Bottom Node:  O(1), other Nodes: O(n)
// return: null|number [number: position of the item]
stack.search('Sourav');

// Time Complexity: O(1)
// return: boolean
stack.getTop()

// Time Complexity: O(1)
// return: boolean
stack.getBottom()

// Time Complexity:
//    Top Node & Bottom Node:  O(1), other Nodes: O(n)
// return: boolean
stack.update('Sourav', 'Abm Sourav')