Object Oriented Python – Data Structures
Data Structures are a fundamental part of programming that help organize, store, and manage data efficiently. In Object-Oriented Python, we can implement data structures using classes and objects, making them reusable, modular, and easy to maintain.
In this tutorial, you will learn how to implement common data structures using Object-Oriented Programming in Python.
1. What are Data Structures?
Data structures are ways of organizing data so that it can be used efficiently.
They help in:
- Storing data
- Managing data
- Processing data efficiently
- Optimizing algorithms
2. Why Use OOP for Data Structures?
Using OOP makes data structures:
- Reusable
- Easy to maintain
- Modular
- Scalable
- Real-world friendly
Instead of using simple variables, we use classes to represent complex structures.
3. List as an Object
Python lists are built-in data structures.
Example:
numbers = [10, 20, 30, 40]
print(numbers)
But in OOP, we can create a custom list structure.
4. Stack Data Structure (LIFO)
Stack follows Last In First Out principle.
OOP Implementation:
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()
return "Stack is empty"
def is_empty(self):
return len(self.items) == 0
def display(self):
print(self.items)
Using Stack:
stack = Stack()
stack.push(10)
stack.push(20)
stack.push(30)
stack.display()
print(stack.pop())
5. Queue Data Structure (FIFO)
Queue follows First In First Out principle.
OOP Implementation:
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)
return "Queue is empty"
def is_empty(self):
return len(self.items) == 0
def display(self):
print(self.items)
Using Queue:
q = Queue()
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.display()
print(q.dequeue())
6. Linked List (Singly Linked List)
A linked list is made of nodes.
Each node contains:
- Data
- Reference to next node
Node Class
class Node:
def __init__(self, data):
self.data = data
self.next = None
Linked List Class
class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = new_node
Display Linked List
def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")
Using Linked List:
ll = LinkedList()
ll.insert(10)
ll.insert(20)
ll.insert(30)
ll.display()
7. Tree Data Structure (Basic Concept)
A tree is a hierarchical structure.
Node Class:
class TreeNode:
def __init__(self, data):
self.data = data
self.children = []
Adding Child Nodes:
def add_child(self, child):
self.children.append(child)
Display Tree:
def display(self, level=0):
print(" " * level * 2 + str(self.data))
for child in self.children:
child.display(level + 1)
Example:
root = TreeNode("A")
b = TreeNode("B")
c = TreeNode("C")
root.add_child(b)
root.add_child(c)
b.add_child(TreeNode("D"))
b.add_child(TreeNode("E"))
root.display()
8. Comparison of Data Structures
| Data Structure | Type | Principle |
|---|---|---|
| Stack | Linear | LIFO |
| Queue | Linear | FIFO |
| Linked List | Linear | Node-based |
| Tree | Non-linear | Hierarchical |
9. When to Use Each Structure
Stack
- Undo operations
- Browser history
- Function calls
Queue
- Task scheduling
- Print queue
- CPU scheduling
Linked List
- Dynamic memory allocation
- Insertion/deletion-heavy apps
Tree
- File systems
- Databases
- AI decision trees
10. Advantages of OOP Data Structures
- Easy to extend
- Better organization
- Real-world modeling
- Code reuse
- Better debugging
11. Common Mistakes
❌ Not handling empty cases
✔ Always check before pop/dequeue
❌ Using lists for everything
✔ Use proper data structure for efficiency
❌ Ignoring encapsulation
✔ Keep data inside classes
12. Best Practices
✔ Use classes for all structures
✔ Keep methods small and focused
✔ Handle edge cases
✔ Use meaningful variable names
✔ Test each structure separately
Conclusion
Object-Oriented Python makes it easier to implement and understand data structures like stacks, queues, linked lists, and trees. By using classes and objects, we can create reusable and scalable implementations that closely match real-world systems.
Mastering these OOP-based data structures is essential for coding interviews, competitive programming, and software development.


0 Comments