Mastering Data Structures: An Overview of Types and Best Practices

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

Arrangement of stuff in a closet referencing to storage and structure.
Image by the author using stable-diffusion

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:

Classification of Data Structure

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

……Some of the Commonly used Data Structure…

Array

Representation of Array Data Structure.
# Create an array of integers
my_array = [1, 2, 3, 4, 5]

print(my_array)

Linked List

Representation of Linked List Data Structure
# Node class for the linked list
class Node:
def __init__(self, value):
self.value = value
self.next = None

# LinkedList class
class 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 list
my_list = LinkedList()

# Add some values to the linked list
my_list.add(1)
my_list.add(2)
my_list.add(3)

# Print the values of the nodes in the linked list
my_list.print_list()
# Output: 3, 2, 1

Stack

Representation of Stack Data Structure
Credit — GeeksforGeeks
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 stack
stack = Stack()

# Add items to the stack
stack.push(1)
stack.push(2)
stack.push(3)



# Remove items from the stack
stack.pop()
stack.pop()



# Print the remaining item in the stack
print(stack.peek()) # Output: 1

Queue

Representation of Queue Data Structure
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[0]

def is_empty(self):
return len(self.items) == 0

Tree

Representation of Tree Data Structure

# 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

Representation of Graph Data Structure
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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store