├── .vscode └── c_cpp_properties.json ├── Binary.java ├── BinarySearch.java ├── README.md ├── RecursionLoop.class ├── RecursionLoop.java ├── bin.asm ├── bin.csx ├── binary.cs ├── binary.js ├── binary_search.py ├── box.js ├── doubly_linkedlist.py ├── fib.py ├── fibonacci.py ├── graph.py ├── hash_table.py ├── inherit.py ├── learning ├── anty.js └── anty.py ├── linearSearch.js ├── linear_search.py ├── linkedlist.js ├── linkedlist.py ├── merge_sort.py ├── queue.js ├── queue_1.py ├── range.dart ├── recursion.c ├── recursion.class ├── recursion.cpp ├── recursion.java ├── recursion.js ├── recursion.py ├── recursive_bst.py ├── stack.py ├── temp.txt ├── tempCodeRunnerFile.js ├── tes ├── test.asm ├── test.c ├── test.js ├── test.py ├── test.sh ├── test_lang.py └── testdata.py /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Win32", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [ 9 | "_DEBUG", 10 | "UNICODE", 11 | "_UNICODE" 12 | ], 13 | "windowsSdkVersion": "10.0.22621.0", 14 | "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx86/x86/cl.exe", 15 | "cStandard": "c23", 16 | "cppStandard": "c++23", 17 | "intelliSenseMode": "windows-msvc-x86" 18 | } 19 | ], 20 | "version": 4 21 | } -------------------------------------------------------------------------------- /Binary.java: -------------------------------------------------------------------------------- 1 | public class Binary { 2 | public int list; 3 | public int value; 4 | 5 | public void search(int list, int value){ 6 | this.list = list; 7 | this.value = value; 8 | 9 | int left = 0; 10 | int right; 11 | 12 | 13 | } 14 | 15 | public void verifyValue(int index){ 16 | 17 | } 18 | 19 | } 20 | 21 | -------------------------------------------------------------------------------- /BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | public static void main(String[] args) { 3 | 4 | 5 | 6 | 7 | 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Structures and Algorithms 2 | 3 | Welcome to my Data Structures and Algorithms project! This repository serves as a comprehensive resource for understanding and implementing various data structures and algorithms. The focus, at the moment, has been on Binary Search Trees (BST), Singly LinkedList, Doubly LinkedList, Queue, Stack, and the Merge Sort algorithm with an exploration of Big O notation, emphasizing O(n) and O(log n) complexities. 4 | 5 | ## Table of Contents 6 | 7 | - [Binary Search Tree (BST)](#binary-search-tree-bst) 8 | - [Overview](#overview) 9 | - [Implementation](#implementation) 10 | - [Iterative BST](#iterative-bst) 11 | - [Recursive BST](#recursive-bst) 12 | - [Singly LinkedList](#singly-linkedlist) 13 | - [Doubly LinkedList](#doubly-linkedlist) 14 | - [Queue](#queue) 15 | - [Stack](#stack) 16 | - [Merge Sort Algorithm](#merge-sort-algorithm) 17 | - [Linear Search Algorithm](#linear-search-algorithm) 18 | - [Recursion Algorithm](#recursion-algorithm) 19 | - [Complexity Analysis](#complexity-analysis) 20 | - [O(n) Complexity](#on-complexity) 21 | - [O(log n) Complexity](#olog-n-complexity) 22 | - [Next Steps](#next-steps) 23 | 24 | ## Binary Search Tree (BST) 25 | 26 | ### Overview 27 | 28 | A Binary Search Tree serves as the cornerstone of this project, embodying a sorted data structure with a hierarchical arrangement. Each node intricately balances its children, providing an elegant framework for efficient data retrieval. 29 | 30 | ### Implementation 31 | 32 | #### Iterative BST 33 | 34 | The Iterative BST implementation in Python lays the foundation for understanding the nuanced procedures of insertion, search, and traversal within a BST. Its structure is deliberately designed for clarity and comprehensibility. 35 | 36 | #### Recursive BST 37 | 38 | The Recursive BST implementation, also in Python, delves into the inherent beauty of recursive methodologies when dealing with tree structures. The code reflects an abstraction that aligns with the intrinsic nature of recursive problem-solving. 39 | 40 | #### Singly LinkedList 41 | 42 | The Singly LinkedList implementation showcases a linear data structure where elements are linked sequentially. Operations such as insertion and traversal are explored in this section. 43 | 44 | #### Doubly LinkedList 45 | 46 | The Doubly LinkedList implementation extends the concept of linked lists by adding backward pointers. This bidirectional linkage enhances traversal and certain manipulation operations. 47 | 48 | #### Queue 49 | 50 | The Queue section introduces the basic operations of a queue, a linear data structure following the First In, First Out (FIFO) principle. Learn about enqueue, dequeue, front, isEmpty, and size operations. 51 | 52 | #### Stack 53 | 54 | The Stack section explores the Last In, First Out (LIFO) data structure. Learn about push, pop, peek, isEmpty, and size operations. Stacks are widely used in various applications, including managing function calls, parsing expressions, and undo functionality. 55 | 56 | #### Merge Sort Algorithm 57 | 58 | The Merge Sort Algorithm section introduces a divide-and-conquer approach to sorting. Learn how this algorithm efficiently sorts a list by recursively dividing it into smaller segments, sorting them, and then merging them back together. 59 | 60 | #### Linear Search Algorithm 61 | 62 | In addition to tree-based structures, this project includes a Linear Search Algorithm, showcasing the simplicity and efficiency of searching in a linear collection. 63 | 64 | #### Recursion Algorithm 65 | 66 | Explore the power of recursion in the included Recursion Algorithm. This section provides a hands-on understanding of recursive principles applied to problem-solving. 67 | 68 | ### Complexity Analysis 69 | 70 | #### O(n) Complexity 71 | 72 | The Iterative BST implementation offers insights into scenarios where the tree devolves into a linear structure, resulting in an O(n) time complexity. This elucidates the importance of optimizing tree structures for better performance. 73 | 74 | #### O(log n) Complexity 75 | 76 | In contrast, the Recursive BST implementation underscores the efficiency of well-balanced trees, showcasing the coveted O(log n) time complexity in search operations. The recursive approach seamlessly aligns with the logarithmic nature of balanced binary search trees. 77 | 78 | ### Next Steps 79 | 80 | As this project unfolds, the exploration will extend beyond the confines of Binary Search Trees, Linked Lists, Queues, and Stacks. Future endeavors include delving into advanced data structures like AVL trees and navigating through algorithms that underpin essential operations such as sorting and searching. Expect continuous updates as the journey into the captivating realm of Data Structures and Algorithms continues! 81 | 82 | Feel free to peruse the codebase, provide constructive feedback, or reach out with any queries or suggestions. 83 | 84 | Happy coding! 85 | 86 | xxxxxxx 87 | -------------------------------------------------------------------------------- /RecursionLoop.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/RecursionLoop.class -------------------------------------------------------------------------------- /RecursionLoop.java: -------------------------------------------------------------------------------- 1 | public class RecursionLoop { 2 | public int num; 3 | 4 | 5 | int doRecursion(int num) 6 | { 7 | this.num = num; 8 | while (this.num > 1) { 9 | return doRecursion(num - 1) * num; 10 | } 11 | return this.num; 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /bin.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | message db 'Hello, Assembly!', 0 3 | 4 | section .text 5 | global _start 6 | 7 | _start: 8 | ; Write the message to stdout 9 | mov eax, 4 10 | mov ebx, 1 11 | mov ecx, message 12 | mov edx, 16 13 | int 0x80 14 | 15 | ; Exit the program 16 | mov eax, 1 17 | xor ebx, ebx 18 | int 0x80 19 | -------------------------------------------------------------------------------- /bin.csx: -------------------------------------------------------------------------------- 1 | Console.WriteLine("Hello, World!"); 2 | 3 | int a = 5; 4 | int b = 10; 5 | int sum = a + b; 6 | 7 | Console.WriteLine($"The sum of {a} and {b} is {sum}."); 8 | -------------------------------------------------------------------------------- /binary.cs: -------------------------------------------------------------------------------- 1 | // binary search 2 | 3 | using System; 4 | 5 | class BinarySearch { 6 | static void Main(string[] args) { 7 | int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 }; 8 | int key = 5; 9 | int index = PerformBinarySearch(arr, key); 10 | Console.WriteLine("Index of {0} is {1}", key, index); 11 | Console.ReadKey(); 12 | } 13 | 14 | static int PerformBinarySearch(int[] arr, int key) { 15 | int min = 0; 16 | int max = arr.Length - 1; 17 | while (min <= max) { 18 | int mid = (min + max) / 2; 19 | if (key == arr[mid]) { 20 | return mid; 21 | } else if (key < arr[mid]) { 22 | max = mid - 1; 23 | } else { 24 | min = mid + 1; 25 | } 26 | } 27 | return -1; // Return -1 if the key is not found 28 | } 29 | } -------------------------------------------------------------------------------- /binary.js: -------------------------------------------------------------------------------- 1 | function bin(list, num){ 2 | let left = 0; 3 | let right = list.length -1; 4 | while (left <= right) { 5 | let mid = Math.floor((right + left)/2); 6 | if (num === list[mid]){ 7 | return mid; 8 | } 9 | else if (num > list[mid]){ 10 | left = mid + 1; 11 | } 12 | else{ 13 | right = mid - 1; 14 | } 15 | } 16 | } 17 | 18 | // console.log(bin([4,5,6,7,8,9,2,1], 1)); 19 | 20 | 21 | function myBin(newList, n) 22 | { 23 | let left = 0; 24 | let right = newList.length -1; 25 | while(left <= right) 26 | { 27 | let mid = Math.floor((left + right)/2); 28 | if(n < newList[mid]) 29 | { 30 | return right -1; 31 | } 32 | else if( n > newList[mid]) 33 | { 34 | return left + 1; 35 | } 36 | else if ( n === newList[mid] ) 37 | { 38 | return mid ; 39 | } 40 | else 41 | { 42 | return "Number doesnt exist"; 43 | } 44 | } 45 | } 46 | 47 | // console.log(myBin([7,5,3,4,8,1,6],8)) 48 | 49 | 50 | function myBin(newList, n) 51 | { 52 | let left = 0; 53 | let right = newList.length - 1; 54 | while (left <= right) 55 | { 56 | let mid = Math.floor((left + right) / 2); 57 | if (n < newList[mid]) 58 | { 59 | right = mid - 1; 60 | return right; 61 | } 62 | else if (n > newList[mid]) 63 | { 64 | left = mid + 1; 65 | return left; 66 | } 67 | else if (n === newList[mid]) 68 | { 69 | return mid; 70 | } 71 | } 72 | return `Number ${n} doesn't exist`; 73 | } 74 | 75 | // console.log(myBin([15, 8, 2, 11, 19, 6, 4, 10, 5, 14, 1, 13, 7, 20, 3, 12, 18, 9, 17, 16], 2)); 76 | 77 | 78 | 79 | 80 | // console.log(bin([7,5,3,4,8,1,6],5)) 81 | 82 | // we had some inefficiency in our previous binary search and now we are to improve it 83 | //we will be sorting our data to improve the efficiency of our data structure 84 | 85 | 86 | function newBin(mylist, n) 87 | { 88 | //sorting the array using quick sort method 89 | let _sorted = mylist.sort(); 90 | let left = 0; 91 | let right = _sorted.length -1; 92 | 93 | while (left <= right) 94 | { 95 | let mid = Math.floor((left + right)/ 2); 96 | if (n === _sorted[mid] ) 97 | { 98 | return mid; 99 | } 100 | else if(n > _sorted[mid]) 101 | { 102 | return right ; 103 | } 104 | else if (n < _sorted[mid]) 105 | { 106 | return left; 107 | } 108 | else 109 | { 110 | return `The number ${n} is not found in the list ${mylist}`; 111 | } 112 | } 113 | 114 | 115 | } 116 | console.log(newBin([7,5,3,4,8,1,6],4)); 117 | 118 | -------------------------------------------------------------------------------- /binary_search.py: -------------------------------------------------------------------------------- 1 | def binary_search(list, value): 2 | left = 0 3 | right = len(list) -1 4 | while (left <= right): 5 | mid = (left + right)//2 6 | if list[mid] == value: 7 | return mid 8 | elif list[mid] < value: 9 | left = mid +1 10 | else: 11 | right = mid -1 12 | return None 13 | 14 | 15 | def verify(index): 16 | if index is not None: 17 | return f"Index found at {index}" 18 | return f"Index not found" 19 | 20 | 21 | numbers = [1,2,3,4,5,6,7,8,11] 22 | result = binary_search(numbers,12) 23 | 24 | print(verify(result)) 25 | 26 | -------------------------------------------------------------------------------- /box.js: -------------------------------------------------------------------------------- 1 | class Box{ 2 | constructor(){ 3 | this.vertices = []; 4 | this.edges = []; 5 | this.faces = []; 6 | this.l1 = null; 7 | this.l2 = null; 8 | this.l3 = null; 9 | this.l4 = null; 10 | 11 | } 12 | 13 | vertices(l1,l2,l3,l4){ 14 | this.l1 = l1; 15 | this.l2 = l2; 16 | this.l3 = l3; 17 | this.l4 = l4; 18 | } 19 | edges(){ 20 | this.edges.push(this.l1); 21 | this.edges.push(this.l2); 22 | this.edges.push(this.l3); 23 | this.edges.push(this.l4); 24 | } 25 | faces(){ 26 | this.faces.push(this.l1); 27 | this.faces.push(this.l2); 28 | this.faces.push(this.l3); 29 | this.faces.push(this.l4); 30 | } 31 | // insert at a given index 32 | insert(index, value){ 33 | if(index >= this.length){ 34 | return this.append(value); 35 | } 36 | const newNode = {value: value, next: null}; 37 | const leader = this.traverseToIndex(index-1); 38 | const holdingPointer = leader.next; 39 | leader.next = newNode; 40 | newNode.next = holdingPointer; 41 | this.length++; 42 | } 43 | // remove at a given index 44 | remove(index){ 45 | if(index >= this.length){ 46 | return; 47 | } 48 | const leader = this.traverseToIndex(index-1); 49 | const unwantedNode = leader.next; 50 | leader.next = unwantedNode.next; 51 | this.length--; 52 | } 53 | // traverse to a given index 54 | traverseToIndex(index){ 55 | let counter = 0; 56 | let currentNode = this.head; 57 | while(counter !== index){ 58 | currentNode = currentNode.next; 59 | counter++; 60 | } 61 | return currentNode; 62 | } 63 | // print the linked list 64 | printList(){ 65 | const array = []; 66 | let currentNode = this.head; 67 | while(currentNode !== null){ 68 | array.push(currentNode.value); 69 | currentNode = currentNode.next; 70 | } 71 | return array; 72 | } 73 | 74 | 75 | 76 | } 77 | 78 | // Path: line.js 79 | 80 | const box = new Box(); 81 | 82 | console.log(box.printList()) 83 | -------------------------------------------------------------------------------- /doubly_linkedlist.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/doubly_linkedlist.py -------------------------------------------------------------------------------- /fib.py: -------------------------------------------------------------------------------- 1 | def fib(n): 2 | prev = 0 3 | next = 1 4 | if n == 1: 5 | return 0 6 | else: 7 | new = prev + next 8 | for i in range(2, n+1): 9 | new = prev + next 10 | prev = next 11 | next = new 12 | return new 13 | 14 | def main(): 15 | # Testing the functions 16 | print("Fibonacci number at position 10 is: ", fib(10)) 17 | 18 | if __name__ == "__main__": 19 | main() -------------------------------------------------------------------------------- /fibonacci.py: -------------------------------------------------------------------------------- 1 | def fib(n): 2 | prev = 0 3 | next = 1 4 | if n == 1: 5 | return 1 6 | else: 7 | new = prev + next 8 | for i in range(2, n+1): 9 | new = prev + next 10 | prev = next 11 | next = new 12 | return new 13 | 14 | 15 | def fib_rec(n): 16 | if n == 1: 17 | return 1 18 | elif n == 2: 19 | return 1 20 | else: 21 | return fib_rec(n-1) + fib_rec(n-2) 22 | # Testing the functions 23 | print("Fibonacci number at position 10 is: ", fib(10)) 24 | print("Fibonacci number at position 10 is: ", fib_rec(10)) 25 | 26 | -------------------------------------------------------------------------------- /graph.py: -------------------------------------------------------------------------------- 1 | class Graph: 2 | def __init__(self): 3 | self.vertices = {} 4 | 5 | def add_vertex(self, vertex): 6 | if vertex not in self.vertices: 7 | self.vertices[vertex] = [] 8 | 9 | def add_edge(self, vertex1, vertex2): 10 | if vertex1 in self.vertices and vertex2 in self.vertices: 11 | self.vertices[vertex1].append(vertex2) 12 | self.vertices[vertex2].append(vertex1) 13 | 14 | def display(self): 15 | for vertex in self.vertices: 16 | print(f"{vertex}: {', '.join(map(str, self.vertices[vertex]))}") 17 | 18 | # Example Usage: 19 | graph = Graph() 20 | graph.add_vertex(1) 21 | graph.add_vertex(2) 22 | graph.add_vertex(3) 23 | graph.add_edge(1, 2) 24 | graph.add_edge(2, 3) 25 | 26 | graph.display() 27 | -------------------------------------------------------------------------------- /hash_table.py: -------------------------------------------------------------------------------- 1 | class HashTable: 2 | def __init__(self): 3 | """ 4 | Initialize a hash table with a default size of 10. 5 | """ 6 | self.size = 10 7 | self.table = [[] for _ in range(self.size)] 8 | 9 | def _hash_function(self, key): 10 | """ 11 | Hashes the given key and returns the index in the hash table. 12 | """ 13 | return hash(key) % self.size 14 | 15 | def insert(self, key, value): 16 | """ 17 | Inserts a key-value pair into the hash table. 18 | """ 19 | index = self._hash_function(key) 20 | self.table[index].append((key, value)) 21 | 22 | def get(self, key): 23 | """ 24 | Retrieves the value associated with the given key from the hash table. 25 | Raises a KeyError if the key is not found. 26 | """ 27 | index = self._hash_function(key) 28 | for k, v in self.table[index]: 29 | if k == key: 30 | return v 31 | raise KeyError(f"Key '{key}' not found in the hash table.") 32 | 33 | def remove(self, key): 34 | """ 35 | Removes the key-value pair associated with the given key from the hash table. 36 | Raises a KeyError if the key is not found. 37 | """ 38 | index = self._hash_function(key) 39 | for i, (k, v) in enumerate(self.table[index]): 40 | if k == key: 41 | del self.table[index][i] 42 | return 43 | raise KeyError(f"Key '{key}' not found in the hash table.") 44 | 45 | 46 | hash_1 = HashTable() 47 | 48 | hash_1.insert("a", 1) 49 | hash_1.insert("b", 2) 50 | hash_1.insert("c", 3) 51 | hash_1.insert("d", 4) 52 | hash_1.insert("e", 5) 53 | hash_1.insert("f", 6) 54 | hash_1.insert("g", 7) 55 | hash_1.insert("h", 8) 56 | hash_1.insert("i", 9) 57 | hash_1.insert("j", 10) 58 | hash_1.insert("k", 11) 59 | hash_1.insert("l", 12) 60 | hash_1.insert("m", 13) 61 | hash_1.insert("n", 14) 62 | hash_1.insert("o", 15) 63 | hash_1.insert("p", 16) 64 | hash_1.insert("q", 17) 65 | 66 | print(hash_1.get("a")) 67 | print(hash_1.get("b")) 68 | print(hash_1.get("c")) 69 | 70 | hash_1.remove("a") 71 | hash_1.remove("b") 72 | hash_1.remove("c") 73 | 74 | print(hash_1.get("a")) 75 | print(hash_1.get("b")) 76 | print(hash_1.get("c")) 77 | 78 | hash_1.insert("a", 1) 79 | hash_1.insert("b", 2) 80 | hash_1.insert("c", 3) 81 | 82 | print(hash_1.get("a")) -------------------------------------------------------------------------------- /inherit.py: -------------------------------------------------------------------------------- 1 | class Test: 2 | def __init__(self): 3 | print("Test init") 4 | self.a = 10 5 | self.b = 20 6 | 7 | def display(self): 8 | print("a = ", self.a) 9 | print("b = ", self.b) 10 | 11 | 12 | class Demo(Test): 13 | def __init__(self): 14 | print("Demo init") 15 | self.c = 30 16 | self.d = 40 17 | 18 | def show(self): 19 | print("c = ", self.c) 20 | print("d = ", self.d) 21 | 22 | 23 | def main(): 24 | obj = Demo() 25 | obj.display() 26 | obj.show() 27 | 28 | 29 | 30 | def run(): 31 | return main() 32 | 33 | 34 | if __name__ == '__main__': 35 | run() -------------------------------------------------------------------------------- /learning/anty.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/learning/anty.js -------------------------------------------------------------------------------- /learning/anty.py: -------------------------------------------------------------------------------- 1 | name = 'anty' 2 | age = 20 3 | 4 | 5 | def say_hello(): 6 | print('Hello') -------------------------------------------------------------------------------- /linearSearch.js: -------------------------------------------------------------------------------- 1 | function linearSearch(list, num){ 2 | for(n in list){ 3 | if (list[n] == num) { 4 | return n; 5 | } 6 | } 7 | } 8 | 9 | console.log(linearSearch([1,4,2,3,7,5],4)) -------------------------------------------------------------------------------- /linear_search.py: -------------------------------------------------------------------------------- 1 | def linear_search(list, value): 2 | for i in range(0, len(list)): 3 | if list[i] == value: 4 | return i 5 | return None 6 | 7 | def verify_search(index): 8 | if index != None: 9 | return f"Index found at: {index}" 10 | else: 11 | return "Index Not Found" 12 | 13 | 14 | list = [1,2,3,4,5,6,7,8] 15 | 16 | result = linear_search(list, 9) 17 | print(verify_search(result)) -------------------------------------------------------------------------------- /linkedlist.js: -------------------------------------------------------------------------------- 1 | class LinkedList{ 2 | constructor(){ 3 | this.head = null; 4 | this.tail = null; 5 | this.length = 0; 6 | 7 | } 8 | // add to the end of the list 9 | append(value){ 10 | const newNode = {value: value, next: null}; 11 | if(this.tail){ 12 | this.tail.next = newNode; 13 | } 14 | this.tail = newNode; 15 | if(!this.head){ 16 | this.head = newNode; 17 | } 18 | this.length++; 19 | } 20 | // add to the beginning of the list 21 | prepend(value){ 22 | const newNode = {value: value, next: this.head}; 23 | this.head = newNode; 24 | if(!this.tail){ 25 | this.tail = newNode; 26 | } 27 | this.length++; 28 | } 29 | // insert at a given index 30 | insert(index, value){ 31 | if(index >= this.length){ 32 | return this.append(value); 33 | } 34 | const newNode = {value: value, next: null}; 35 | const leader = this.traverseToIndex(index-1); 36 | const holdingPointer = leader.next; 37 | leader.next = newNode; 38 | newNode.next = holdingPointer; 39 | this.length++; 40 | } 41 | // remove at a given index 42 | remove(index){ 43 | if(index >= this.length){ 44 | return; 45 | } 46 | const leader = this.traverseToIndex(index-1); 47 | const unwantedNode = leader.next; 48 | leader.next = unwantedNode.next; 49 | this.length--; 50 | } 51 | // traverse to a given index 52 | traverseToIndex(index){ 53 | let counter = 0; 54 | let currentNode = this.head; 55 | while(counter !== index){ 56 | currentNode = currentNode.next; 57 | counter++; 58 | } 59 | return currentNode; 60 | } 61 | // print the list 62 | printList(){ 63 | const array = []; 64 | let currentNode = this.head; 65 | while(currentNode !== null){ 66 | array.push(currentNode.value); 67 | currentNode = currentNode.next; 68 | } 69 | return array; 70 | } 71 | // reverse the list 72 | reverse(){ 73 | if(!this.head.next){ 74 | return this.head; 75 | } 76 | let first = this.head; 77 | this.tail = this.head; 78 | let second = first.next; 79 | while(second){ 80 | const temp = second.next; 81 | second.next = first; 82 | first = second; 83 | second = temp; 84 | } 85 | this.head.next = null; 86 | this.head = first; 87 | } 88 | } 89 | 90 | const myLinkedList = new LinkedList(); 91 | myLinkedList.append(10); 92 | myLinkedList.append(5); 93 | myLinkedList.prepend(1); 94 | myLinkedList.insert(2, 99); 95 | myLinkedList.remove(2); 96 | myLinkedList.reverse(); 97 | console.log(myLinkedList.printList()); 98 | console.log(myLinkedList); 99 | 100 | -------------------------------------------------------------------------------- /linkedlist.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | """Implementation for the Node""" 3 | data = None 4 | next_node = None 5 | 6 | 7 | def __init__(self, data): 8 | self.data = data 9 | 10 | def __repr__(self): 11 | return f"Node: {self.data}" 12 | 13 | 14 | class LinkedList: 15 | """Implementation for the LinkedList""" 16 | def __init__(self): 17 | self.head = None 18 | 19 | 20 | def __repr__(self): 21 | """Return a string representation of the list.""" 22 | nodes = [] 23 | current = self.head 24 | while current: 25 | if current is self.head: 26 | nodes.append(f"[Head: {current.data}]") 27 | elif current.next_node is None: 28 | nodes.append(f"[Tail: {current.data}]") 29 | else: 30 | nodes.append(f"[{current.data}]") 31 | current = current.next_node 32 | return '-> '.join(nodes) 33 | 34 | 35 | def search(self,value): 36 | """Search the linked list for a node with the requested value and return the node.""" 37 | current = self.head 38 | while current: 39 | if current.data == value: 40 | return current 41 | current = current.next_node 42 | return None 43 | 44 | def insert(self, data, index): 45 | """Insert a new node with the given data at the given index.""" 46 | if index == 0: 47 | self.add(data) 48 | if index > 0: 49 | new = Node(data) 50 | position = index 51 | current = self.head 52 | while position > 1: 53 | current = Node.next_node 54 | position -= 1 55 | prev_node = current 56 | next_node = current.next_node 57 | prev_node.next_node = new 58 | new.next_node = next_node 59 | 60 | def is_empty(self): 61 | """Return True if the list is empty.""" 62 | return self.head == None 63 | 64 | def size(self): 65 | """Return the number of nodes in the list.""" 66 | current = self.head 67 | count = 0 68 | while current: 69 | count += 1 70 | current = current.next_node 71 | return count 72 | 73 | def add(self,data): 74 | """Add a new node at the head of the list.""" 75 | new_node = Node(data) 76 | new_node.next_node = self.head 77 | self.head = new_node 78 | return self.head 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | l = LinkedList() 88 | 89 | l.add(20) 90 | l.add(10) 91 | l.add(22) 92 | l.add(90) 93 | print(l.size()) 94 | print(l) 95 | 96 | print(l.search(22)) 97 | print(l.search(100)) -------------------------------------------------------------------------------- /merge_sort.py: -------------------------------------------------------------------------------- 1 | def merge_sort(arr): 2 | if len(arr) <= 1: 3 | return arr 4 | 5 | mid = len(arr) // 2 6 | left_half = arr[:mid] 7 | right_half = arr[mid:] 8 | 9 | left_half = merge_sort(left_half) 10 | right_half = merge_sort(right_half) 11 | 12 | return merge(left_half, right_half) 13 | 14 | def merge(left, right): 15 | merged = [] 16 | left_index = 0 17 | right_index = 0 18 | 19 | while left_index < len(left) and right_index < len(right): 20 | if left[left_index] <= right[right_index]: 21 | merged.append(left[left_index]) 22 | left_index += 1 23 | else: 24 | merged.append(right[right_index]) 25 | right_index += 1 26 | 27 | while left_index < len(left): 28 | merged.append(left[left_index]) 29 | left_index += 1 30 | 31 | while right_index < len(right): 32 | merged.append(right[right_index]) 33 | right_index += 1 34 | 35 | return merged 36 | 37 | 38 | 39 | list = [5,3,1,9,8,4,3,0,5,2] 40 | 41 | print(merge_sort(list)) 42 | 43 | # Output: [0, 1, 2, 3, 3, 4, 5, 5, 8, 9] 44 | -------------------------------------------------------------------------------- /queue.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Represents a Queue data structure. 3 | */ 4 | class Queue{ 5 | 6 | constructor(){ 7 | this.items = []; 8 | } 9 | enqueue(item){ 10 | this.items.push(item); 11 | } 12 | 13 | size(){ 14 | return this.items.length; 15 | } 16 | 17 | isEmpty(){ 18 | return this.items.length == 0; 19 | } 20 | 21 | dequeue(){ 22 | if(this.isEmpty()){ 23 | return "Underflow"; 24 | } 25 | return this.items.shift(); 26 | 27 | } 28 | front(){ 29 | if(this.isEmpty()){ 30 | return "No elements in Queue" 31 | } 32 | return this.items[0] 33 | 34 | } 35 | display(){ 36 | return this.items 37 | } 38 | } 39 | 40 | 41 | q = new Queue(); 42 | 43 | q.enqueue(1); 44 | q.enqueue(2); 45 | q.enqueue(3); 46 | q.enqueue(4); 47 | console.log(q.size()) 48 | console.log(q.display()) 49 | console.log(q.front()) 50 | q.dequeue() 51 | q.enqueue(5); 52 | q.enqueue(6); 53 | q.enqueue(7); 54 | console.log(q.size()) 55 | q.dequeue() 56 | console.log(q.size()) 57 | q.dequeue() 58 | console.log(q.size()) 59 | q.dequeue() 60 | console.log(q.size()) 61 | q.dequeue() 62 | console.log(q.size()) 63 | q.dequeue() 64 | console.log(q.size()) 65 | q.dequeue() 66 | console.log(q.size()) 67 | console.log(q.dequeue()) -------------------------------------------------------------------------------- /queue_1.py: -------------------------------------------------------------------------------- 1 | class Queue: 2 | 3 | def __init__(self): 4 | self.items = [] 5 | 6 | def enqueue(self,item): 7 | """Add an element to the end of the queue""" 8 | self.item = item 9 | self.items.append(self.item) 10 | 11 | 12 | def dequeue(self): 13 | """Remove and return the first item in the queue""" 14 | if len(self.items) != 0: 15 | self.items.pop(-1) 16 | return f"List is empty" 17 | 18 | def size(self): 19 | """Return the number of items in the queue""" 20 | self.count = 0 21 | while self.item != 0: 22 | return len(self.items) 23 | return f"Queue is empty" 24 | 25 | def is_empty(self): 26 | """Check if the queue is empty or not""" 27 | return len(self.items) == 0 28 | 29 | def display(self): 30 | """Print all elements in the queue""" 31 | return self.items 32 | def front(self): 33 | return self.items[-1] 34 | 35 | 36 | 37 | q = Queue() 38 | q.enqueue(10) 39 | q.enqueue(15) 40 | q.enqueue(11) 41 | q.enqueue(100) 42 | q.enqueue(7) 43 | print(q.display()) 44 | print(q.front()) 45 | print(q.size()) 46 | q.dequeue() 47 | q.dequeue() 48 | print(q.display()) 49 | q.dequeue() 50 | print(q.display()) 51 | print(q.size()) 52 | print(q.is_empty()) -------------------------------------------------------------------------------- /range.dart: -------------------------------------------------------------------------------- 1 | void main(List args) { 2 | var list = [1,2,3,4,5]; 3 | for (var i in list) { 4 | for (var j = 0; j < 5; j++) { 5 | print(i); 6 | } 7 | 8 | } 9 | 10 | //let's create our class instance 11 | var person = Person('John', 30); 12 | 13 | person.showOutput(); 14 | 15 | } 16 | 17 | 18 | // Let's create a class 19 | 20 | class Person { 21 | String name; 22 | int age; 23 | Person(this.name, this.age); 24 | void showOutput() { 25 | print(name); 26 | print(age); 27 | } 28 | } -------------------------------------------------------------------------------- /recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("Hello World"); 6 | } -------------------------------------------------------------------------------- /recursion.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/recursion.class -------------------------------------------------------------------------------- /recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // Declare variables of type integer and float. 6 | int num1 = 5; 7 | float num2 = 3.7f; 8 | // Print the value of 'num1' to the console using printf function with format specifier %d. 9 | std::cout<<"Hello"; 10 | // Print the value of 'num2' to the console using printf function with format specifier %.2f. This will display only two decimal 11 | return 0; 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /recursion.java: -------------------------------------------------------------------------------- 1 | 2 | public class recursion { 3 | 4 | public static void main(String[] args) 5 | { 6 | RecursionLoop myrec = new RecursionLoop(); 7 | 8 | System.out.println(myrec.doRecursion(4)); 9 | } 10 | 11 | 12 | } -------------------------------------------------------------------------------- /recursion.js: -------------------------------------------------------------------------------- 1 | function recursion(n) 2 | { 3 | while(n > 1) 4 | { 5 | return recursion(n - 1) * n; 6 | } 7 | return 1; 8 | } 9 | 10 | console.log(recursion(1)) -------------------------------------------------------------------------------- /recursion.py: -------------------------------------------------------------------------------- 1 | def recursion(n): 2 | while n > 1: 3 | return recursion(n -1) * n 4 | return n 5 | 6 | print(recursion(5)) -------------------------------------------------------------------------------- /recursive_bst.py: -------------------------------------------------------------------------------- 1 | def recursive_binary(list, value): 2 | if len(list) == 0: 3 | return False 4 | else: 5 | mid = (len(list))//2 6 | if list[mid] == value: 7 | return True 8 | else: 9 | if list[mid] < value: 10 | return recursive_binary(list[mid+1:],value) 11 | else: 12 | return recursive_binary(list[:mid],value) 13 | return False 14 | 15 | 16 | 17 | def verify(bool): 18 | if bool == True: 19 | return "Index Found" 20 | return "Index Not found" 21 | 22 | 23 | list = [1,2,3,4,5,6,7,8] 24 | 25 | result = recursive_binary(list,1) 26 | 27 | print(verify(result)) 28 | -------------------------------------------------------------------------------- /stack.py: -------------------------------------------------------------------------------- 1 | class Stack: 2 | """A simple implementation of a stack""" 3 | def __init__(self): 4 | self.items = [] 5 | 6 | def push(self, item): 7 | """Pushes an element to the stack""" 8 | self.items.append(item) 9 | 10 | def pop(self): 11 | """Pops an element from the stack""" 12 | return self.items.pop() 13 | 14 | def peek(self): 15 | """Returns the top element of the stack""" 16 | return self.items[-1] 17 | 18 | 19 | def is_empty(self): 20 | """Checks if the stack is empty""" 21 | return self.items == [] 22 | 23 | def size(self): 24 | """Returns the size of the stack""" 25 | return len(self.items) 26 | 27 | 28 | 29 | s1 = Stack() 30 | s1.push(1) 31 | s1.push(2) 32 | s1.push(3) 33 | s1.push(4) 34 | s1.push(5) 35 | s1.push(6) 36 | s1.push(7) 37 | s1.push(8) 38 | s1.push(9) 39 | s1.push(10) 40 | print(s1.items) 41 | s1.pop() 42 | s1.pop() 43 | s1.pop() 44 | print(s1.peek()) 45 | 46 | print(s1.items) 47 | -------------------------------------------------------------------------------- /temp.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/temp.txt -------------------------------------------------------------------------------- /tempCodeRunnerFile.js: -------------------------------------------------------------------------------- 1 | class Queue{ 2 | 3 | constructor(){ 4 | this.items = []; 5 | } 6 | enqueue(item){ 7 | this.items.push(item); 8 | } 9 | 10 | size(){ 11 | return this.items.length; 12 | } 13 | 14 | isEmpty(){ 15 | return this.items.length == 0; 16 | } 17 | 18 | dequeue(){ 19 | if(this.isEmpty()){ 20 | return "Underflow"; 21 | } 22 | return this.items.shift(); 23 | 24 | } 25 | front(){ 26 | if(this.isEmpty()){ 27 | return "No elements in Queue" 28 | } 29 | return this.items[0] 30 | 31 | } 32 | display(){ 33 | return this.items 34 | } 35 | } 36 | 37 | 38 | q = new Queue(); 39 | 40 | q.enqueue(1); 41 | q.enqueue(2); 42 | q.enqueue(3); 43 | q.enqueue(4); 44 | console.log(q.size()) -------------------------------------------------------------------------------- /tes: -------------------------------------------------------------------------------- 1 | console.log("Hello, world!"); 2 | 3 | print() -------------------------------------------------------------------------------- /test.asm: -------------------------------------------------------------------------------- 1 | section .data 2 | message db 'Hello, World!', 0 3 | 4 | section .text 5 | global _start 6 | 7 | _start: 8 | ; write the message to stdout 9 | mov eax, 4 10 | mov ebx, 1 11 | mov ecx, message 12 | mov edx, 13 13 | int 0x80 14 | 15 | ; exit the program 16 | mov eax, 1 17 | xor ebx, ebx 18 | int 0x80 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Syipmong/DSA-Practice/55b8da4c50793162fef7d1f4ac7d812997af9b33/test.c -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | function sayName(name = "Yipmong") 2 | { 3 | return name; 4 | } 5 | 6 | console.log(sayName("Usman")) 7 | 8 | function foo(){ 9 | return true; 10 | } 11 | 12 | function bar(){ 13 | return false; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | def test_range(num): 2 | while num <= 10: 3 | print(num) 4 | num += 1 5 | 6 | 7 | test_range(5) 8 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # code goes here 3 | echo "Enter the number of rows you want in your matrix: " 4 | read numrows 5 | echo "Enter the number of columns you want in your matrix: " 6 | read numcols 7 | if [ $numrows -le 0 ] || [ $numcols -le 0 ]; then 8 | echo "Invalid input. Please enter a positive integer for both dimensions." 9 | else 10 | matrix=$(printf "%*s\n" $((numcols + 1)) | tr ' ' '.') 11 | for ((i=1; i<=numrows; i++)); do 12 | echo "$((RANDOM % 9 + 1))$matrix" | sed "s/\.$//" 13 | done 14 | -------------------------------------------------------------------------------- /test_lang.py: -------------------------------------------------------------------------------- 1 | class Interpreter: 2 | def __init__(self): 3 | self.variables = {} 4 | 5 | def interpret(self, code): 6 | tokens = code.split() 7 | if len(tokens) < 3: 8 | raise SyntaxError("Invalid syntax") 9 | 10 | if tokens[1] != "=": 11 | raise SyntaxError("Invalid assignment") 12 | 13 | variable = tokens[0] 14 | operator = tokens[2] 15 | value = int(tokens[3]) 16 | 17 | if operator == "+": 18 | self.variables[variable] = self.variables.get(variable, 0) + value 19 | elif operator == "-": 20 | self.variables[variable] = self.variables.get(variable, 0) - value 21 | elif operator == "*": 22 | self.variables[variable] = self.variables.get(variable, 0) * value 23 | elif operator == "/": 24 | self.variables[variable] = self.variables.get(variable, 0) / value 25 | else: 26 | raise SyntaxError("Invalid operator") 27 | 28 | return self.variables[variable] 29 | 30 | 31 | interpreter = Interpreter() 32 | code = input("Enter code: ") 33 | result = interpreter.interpret(code) 34 | print("Result:", result) 35 | 36 | -------------------------------------------------------------------------------- /testdata.py: -------------------------------------------------------------------------------- 1 | class TestData: 2 | def __init__(self): 3 | return self 4 | 5 | def setName(self, name, age): 6 | self.name = name 7 | self.age = age 8 | 9 | def getName(self): 10 | return f'My name is {self.name} and i am {self.age} years old' 11 | 12 | def __repr__(self) -> str: 13 | return self.getName() 14 | 15 | 16 | test = TestData() 17 | 18 | test.setName("Shehu", 20) 19 | 20 | print(test()) --------------------------------------------------------------------------------