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…


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


Linked List

Representation of Linked List Data Structure
# Node class for the linked list
class Node:
def __init__(self, value):
self.value = value = 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) = 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:
current =

# Create a new linked list
my_list = LinkedList()

# Add some values to the linked list

# Print the values of the nodes in the linked list
# Output: 3, 2, 1


Representation of Stack Data Structure
Credit — GeeksforGeeks
class Stack:
def __init__(self):
self.items = []

def push(self, 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

# Remove items from the stack

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


Representation of Queue Data Structure
class Queue:
def __init__(self):
self.items = []

def enqueue(self, 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


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


Representation of Graph Data Structure
class AdjNode:
def __init__(self, data):
self.vertex = data = 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) = self.graph[src]
self.graph[src] = node

# Adding the source node to the destination as
# it is the undirected graph
node = AdjNode(src) = 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