CodeShare-DSA: Collaborative Learning Hub for Data Structures and Algorithms π
Welcome to CodeShare-DSA! This repository serves as a collaborative learning hub for exploring various data structures and algorithms. Dive into code snippets, detailed explanations, and insightful discussions to enhance your understanding of fundamental concepts in computer science.
Join our community to share your knowledge, collaborate with fellow developers, and embark on a journey of continuous learning and improvement. Let's code, learn, and grow together in the fascinating world of data structures and algorithms!
π©βπ» Happy Coding! π¨βπ»
Table of Contents
- Introduction
- Arrays/String
- Linked Lists
- Stacks
- Queues
- Sorting Algorithms
- Searching Algorithms
- Graph Algorithms
- Dynamic Programming
- Hashing
- Tree and Tree Traversal
- Bit Manipulation
- Miscellaneous
Problem Solving Techniques
- Divide and Conquer
- Two Pointers
- Sliding Window
- Binary Search
- Graph Traversal
- Greedy Algorithms
- Dynamic Programming
- Backtracking
- Memoization
- Bit Manipulation
Introduction
Welcome to CodeShare-DSA!
CodeShare-DSA is a collaborative learning hub for Data Structures and Algorithms (DSA). This repository is dedicated to providing a comprehensive collection of DSA resources, including theory, code snippets, problem-solving techniques, and algorithm implementations.
Whether you're a beginner looking to learn the fundamentals of DSA or an experienced developer seeking to enhance your skills, you'll find valuable resources here to support your journey. From arrays to dynamic programming, this repository covers a wide range of topics essential for mastering DSA.
Feel free to explore the repository, learn from the resources available, and contribute your own knowledge and expertise to help others on their learning path. Together, let's build a vibrant community of DSA enthusiasts and empower each other to become better programmers.
Arrays/String
In programming, arrays and strings are fundamental data structures used to store and manipulate collections of values. Arrays are ordered collections of elements, while strings are sequences of characters. Both arrays and strings are commonly used in JavaScript for various purposes.
1. Array Basics
Arrays in JavaScript are dynamic, meaning they can grow or shrink in size as needed. They can store elements of any data type and support various operations such as insertion, deletion, and traversal.
JavaScript Example:
// Create an array
let arr = [1, 2, 3, 4, 5];
// Accessing elements
console.log(arr[0]); // Output: 1
// Insertion
arr.push(6); // Append 6 to the end of the array
// Deletion
let index = arr.indexOf(3);
if (index !== -1) {
arr.splice(index, 1); // Remove element 3 from the array
}
// Traversal
arr.forEach(num => console.log(num)); // Output: 1, 2, 4, 5, 6
# Sorting
arr.sort() # Sort the array in ascending order
# Searching
index = arr.index(4) # Find the index of element 4
# Reversing
arr.reverse() # Reverse the order of elements in the array
//String
# Create a string
my_string = "Hello, World!"
# Concatenation
new_string = my_string + " How are you?"
# Slicing
substring = my_string[7:12] # Extract "World" from the string
//String Manipulation
# Splitting
words = my_string.split(",") # Split the string into a list of words
# Joining
new_string = "-".join(words) # Join the words with hyphens
# Formatting
formatted_string = "My name is {} and I am {} years old".format("Alice", 30)
//Advanced Techniques
# Two-pointer approach
left, right = 0, len(arr) - 1
while left < right:
# Swap elements at left and right pointers
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
Linked Lists
Linked lists are a fundamental data structure consisting of a sequence of elements, each containing a reference to the next element in the sequence. Unlike arrays, linked lists do not have a fixed size and can grow or shrink dynamically. There are various types of linked lists, including singly linked lists, doubly linked lists, and circular linked lists, each with its own advantages and use cases.
1. Singly Linked List
Overview:
In a singly linked list, each node contains a data element and a reference (or link) to the next node in the sequence. The last node points to null, indicating the end of the list. Singly linked lists are simple and efficient in terms of memory usage, but they do not support backward traversal.
JavaScript Example:
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;
}
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
display() {
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
// Example usage:
const singlyLinkedList = new SinglyLinkedList();
singlyLinkedList.append(1);
singlyLinkedList.append(2);
singlyLinkedList.append(3);
singlyLinkedList.display();
2. Doubly Linked List
Overview:
In a doubly linked list, each node contains a data element and two references (or links): one to the next node and one to the previous node in the sequence. This allows for both forward and backward traversal of the list. Doubly linked lists require more memory compared to singly linked lists due to the additional reference for backward traversal.
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
}
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
display() {
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
// Example usage:
const doublyLinkedList = new DoublyLinkedList();
doublyLinkedList.append(1);
doublyLinkedList.append(2);
doublyLinkedList.append(3);
doublyLinkedList.display();
3. Circular Linked List
Overview:
In a circular linked list, the last node points back to the first node in the sequence, forming a circular loop. This means that there is no null reference, and traversal of the list can start from any node. Circular linked lists can be either singly or doubly linked. They are useful in applications where data needs to be continuously processed in a loop.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class CircularLinkedList {
constructor() {
this.head = null;
}
append(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
newNode.next = this.head; // Make the new node point to itself to form a circular loop
return;
}
let current = this.head;
while (current.next !== this.head) {
current = current.next;
}
current.next = newNode;
newNode.next = this.head; // Make the new node point back to the head to form a circular loop
}
display() {
let current = this.head;
do {
console.log(current.data);
current = current.next;
} while (current !== this.head);
}
}
// Example usage:
const circularLinkedList = new CircularLinkedList();
circularLinkedList.append(1);
circularLinkedList.append(2);
circularLinkedList.append(3);
circularLinkedList.display();
Stacks
Overview:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, where elements are inserted and removed from the same end called the top of the stack. It can be visualized as a collection of elements with two main operations: push (to add an element to the top of the stack) and pop (to remove the top element from the stack).
- There are primarily two types of stacks:
- Array-based Stack: This type of stack is implemented using arrays. It utilizes the built-in array methods like push() and pop() to perform stack operations efficiently. The array-based stack is simple to implement and suitable for small-sized stacks.
- Linked List-based Stack: This type of stack is implemented using a linked list data structure. Each node in the linked list represents an element in the stack, and stack operations are performed by manipulating the pointers of the linked list nodes. The linked list-based stack is dynamic in size and can efficiently handle large-sized stacks.
Example: Stack Implementation using Array
Explanation:
In this example, we'll implement a stack using an array in JavaScript. The array's built-in methods, such as push() and pop(), make it convenient to implement the stack operations.
JavaScript Example:
class Stack {
constructor() {
this.items = [];
}
push(element) {
this.items.push(element);
}
pop() {
if (this.isEmpty()) {
return "Underflow";
}
return this.items.pop();
}
peek() {
return this.items[this.items.length - 1];
}
isEmpty() {
return this.items.length === 0;
}
display() {
let result = "";
for (let i = this.items.length - 1; i >= 0; i--) {
result += this.items[i] + " ";
}
console.log(result.trim());
}
}
// Example usage:
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.display(); // Output: 3 2 1
stack.pop();
stack.display(); // Output: 2 1
Queues
[ Details description with example]
Sorting Algorithms
[Explore different sorting algorithms like Bubble Sort, Merge Sort, etc.]
Searching Algorithms
[Discuss searching algorithms such as Linear Search, Binary Search, etc.]
Graph Algorithms
[Cover algorithms related to graphs, such as Depth-First Search (DFS), Breadth-First Search (BFS), etc.]
Dynamic Programming
[Explain dynamic programming concepts and techniques.]
Hashing
[Discuss hashing and its applications in data structures and algorithms.]
Tree and Tree Traversal
[Describe tree data structures and different methods of traversal.]
Miscellaneous
[Include any additional topics or algorithms not covered in the previous sections.]
Contribution Guidelines
Thank you for considering contributing to our project! To make a contribution to the README.md file, please follow these guidelines:
Making Changes
- Fork the repository and clone it locally.
- Open the README.md file in your preferred text editor.
- Make your changes directly to the README.md file.
- Ensure your changes are clear, concise, and relevant to the project.
- Save your changes and commit them with a descriptive commit message.
Submitting Changes
Once you have made your changes to the RE