# Mastering Data Structures: An Overview of Types and Best Practices

## From arrays to trees: understanding the building blocks of efficient programming

A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently.

# The smallest unit of Data and Data Element:

Data Element or Word: it is a fixed number of bits, it is different from computer to computer, but the same for each device. Compute store information in the form of words.

# Classification of Data Structure:

Warning: Don't worry about code much. I am going to explain each D.S. in the coming articles.

# Array

`# Create an array of integersmy_array = [1, 2, 3, 4, 5]print(my_array)`

`# Node class for the linked listclass Node:    def __init__(self, value):        self.value = value        self.next = None# LinkedList classclass LinkedList:    def __init__(self):        self.head = None    # Method to add a new node to the linked list    def add(self, value):        new_node = Node(value)        new_node.next = self.head        self.head = new_node    # Method to print the values of the nodes in the linked list    def print_list(self):        current = self.head        while current:            print(current.value)            current = current.next# Create a new linked listmy_list = LinkedList()# Add some values to the linked listmy_list.add(1)my_list.add(2)my_list.add(3)# Print the values of the nodes in the linked listmy_list.print_list()# Output: 3, 2, 1`

# Stack

`class Stack:    def __init__(self):        self.items = []            def push(self, item):        self.items.append(item)            def pop(self):        if not self.is_empty():            return self.items.pop()            def peek(self):        if not self.is_empty():            return self.items[-1]               # Initialize an empty stackstack = Stack()# Add items to the stackstack.push(1)stack.push(2)stack.push(3)# Remove items from the stackstack.pop()stack.pop()# Print the remaining item in the stackprint(stack.peek()) # Output: 1            `

# Queue

`class Queue:    def __init__(self):        self.items = []            def enqueue(self, item):        self.items.append(item)            def dequeue(self):        if not self.is_empty():            return self.items.pop(0)            def peek(self):        if not self.is_empty():            return self.items            def is_empty(self):        return len(self.items) == 0`

# Tree

`# A Python class that represents# an individual node in a Binary Tree class Node:    def __init__(self, key):        self.left = None        self.right = None        self.val = key`

# Graphs

`class AdjNode:    def __init__(self, data):        self.vertex = data        self.next = None class Graph:    def __init__(self, vertices):        self.V = vertices        self.graph = [None] * self.V     # Function to add an edge in an undirected graph    def add_edge(self, src, dest):        # Adding the node to the source node        node = AdjNode(dest)        node.next = self.graph[src]        self.graph[src] = node         # Adding the source node to the destination as        # it is the undirected graph        node = AdjNode(src)        node.next = self.graph[dest]        self.graph[dest] = node`

--

--