# DATA STRUCTURES
Data structures are the methods or ways data is stored, organised, managed properly for effective manipulation and retrival. Data structures define how data is arranged and how opertions (e.g search, insertion, deletion) are performed on them.
## Types of Data Structures
### Arrays
An **array** is a linear data structure used to store a fixed size of data belonging to the same data type.
#### Example
````go
fruits := [4]string{"Apple", "Orange", "Banana", "Grapes"}
````
### Linked List
A **Linked List** is a linear data structure where each element (node) contains two parts:
- **Data**: The actual value or information stored in the node.
- **Next**: A reference or pointer to the next node in the sequence.
The data structure is suitable when working with dynamic range/amount of data.
#### Types of Linked Lists:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
#### Example
```go=
package main
import (
"fmt"
)
type Node struct {
data any
next *Node
}
type LinkedList struct {
head *Node
}
// Function to add a node at the end of the linked list
func (l *LinkedList) addNode(data any) {
newNode := &Node{data: data, next: nil}
if l.head == nil {
l.head = newNode
return
}
last := l.head
for last.next != nil {
last = last.next
}
last.next = newNode
}
func printList(l *LinkedList) {
current := l.head
for current != nil {
fmt.Println(current.data)
current = current.next
}
}
func main() {
linkedList := LinkedList{}
linkedList.addNode(1)
linkedList.addNode(2)
printList(&linkedList)
}
```
### Trees
A **Tree** is a hierarchical data structure that consists of nodes connected by edges, with a root node and possibly many levels of descendants. Trees are widely used to represent hierarchical relationships.
A Tree consist of the following
- **Root**: The topmost node in the tree.
- **Parent**: A node that has one or more children.
- **Child**: A node that is a descendant of another node.
- **Leaf**: A node that does not have any children.
#### Types of Trees:
- Binary Tre
- Binary Search Tree (BST)
- Balanced Tree
### Graphs
A **Graph** is data structure made up of a collection of nodes (vertices) and edges connecting them. Unlike trees, graphs do not have a hierarchical structure and can represent more complex relationships, such as social networks or transportation systems e.g google maps.
Graphs can have a one direction or bi-directional edges
#### Types of Graphs:
- Directed Acyclic Graph (DAG)
- Weighted Graph
- Complete Graph
# **OBJECT-ORIENTED PROGRAMMING (OOP)**
**OOP** is a programming paradigm based on the concept of **objects** that encapsulate data and behavior. language like python allow using the class keyword to create a template of an object and perform OOP while others employ the use of structures **struct**. It models real-world entities using four key principles:
1. **Encapsulation**: Bundles data and methods into a single unit, hiding implementation details.
2. **Inheritance**: A class can inherit attributes and methods from another class, promoting reuse.
3. **Polymorphism**: Allows methods to behave differently based on the object they are acting upon.
4. **Abstraction**: Hides complex implementation details and provides a simple interface.
# **MEMORY ALLOCATION**
Memory allocation refers to the process of reserving space in the computer's memory to store variables and data. It occurs in two primary areas:
1. **Stack**:
- Stores local variables and function calls.
- Follows a **Last In, First Out (LIFO)** order.
- Memory is automatically managed (allocated and freed when functions return).
2. **Heap**:
- Used for dynamic memory allocation (e.g., objects in OOP).
- Memory must be manually allocated and freed by the programmer (in languages like C++) or managed by garbage collection (in languages like Java or Python).
### **Difference Between Stack and Heap**:
- **Stack**: Fast, limited size, memory is automatically managed.
- **Heap**: Slower, more flexible, but requires manual management.
### **Memory vs Storage**:
- **Memory**: Temporary, fast-access storage used by the CPU (e.g., RAM).
- **Storage**: Permanent storage used to store data long-term (e.g., hard drives, SSDs).
For more code snippets check out my github repos
[Link To Github]('https://github.com/mbragi/DS-Algo')