Data Structures in Python
1. Introduction
A data structure is a way of organizing and storing data so that it can be
accessed and modified efficiently.
In Python, built-in data structures include lists, tuples, sets, and
dictionaries.
For Class 12 Computer Science (CBSE), we focus mainly on
linear data structures: Stacks and Queues, and also Linked
Lists in conceptual terms.
Efficient use of data structures is critical in solving problems in computer
science, from compiler design to artificial intelligence.
2. Classification of Data Structures
1. Primitive Data Structures → integers, floats, strings, booleans.
2. Non-Primitive Data Structures
o Linear: Arrays, Lists, Stacks, Queues, Linked Lists.
o Non-linear: Trees, Graphs.
In this paper, we’ll focus on Stacks, Queues, and Linked Lists (as per
Class 12 level).
3. Stack (LIFO Structure)
Definition: Stack is a linear data structure that follows the
principle:
LIFO → Last In, First Out\text{LIFO → Last In, First Out}
Real-life example: A stack of plates – last plate placed is the first
to be removed.
Operations
1. Push: Insert an element.
2. Pop: Remove the top element.
3. Peek/Top: View the top element without removing it.
Python Implementation
stack = []
# Push elements
stack.append(10)
stack.append(20)
stack.append(30)
print("Stack:", stack)
# Pop element
print("Popped:", stack.pop())
# Peek at top
print("Top element:", stack[-1])
Output:
Stack: [10, 20, 30]
Popped: 30
Top element: 20
4. Queue (FIFO Structure)
Definition: Queue is a linear structure that follows:
FIFO → First In, First Out\text{FIFO → First In, First Out}
Real-life example: People standing in a line – first person in is
served first.
Operations
1. Enqueue: Insert at rear.
2. Dequeue: Remove from front.
Python Implementation
from collections import deque
queue = deque()
# Enqueue
queue.append("A")
queue.append("B")
queue.append("C")
print("Queue:", queue)
# Dequeue
print("Dequeued:", queue.popleft())
# Peek
print("Front element:", queue[0])
Output:
Queue: deque(['A', 'B', 'C'])
Dequeued: A
Front element: B
5. Types of Queues
1. Simple Queue: Insertion at rear, deletion at front.
2. Circular Queue: Last position is connected back to the first (avoids
wasted space).
3. Double Ended Queue (Deque): Insertion and deletion at both
ends.
4. Priority Queue: Each element has a priority; highest priority
served first.
6. Linked Lists (Conceptual in Class 12)
A linked list is a collection of nodes.
Each node has:
o Data
o Pointer (link) to next node
Types
1. Singly Linked List – Each node points to next node.
2. Doubly Linked List – Nodes have links to both previous and next.
3. Circular Linked List – Last node points back to first.
(Python doesn’t have built-in linked lists, but they can be implemented
using classes.)
Python Example: Singly Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(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
def display(self):
temp = self.head
while temp:
print(temp.data, end=" -> ")
temp = temp.next
print("None")
# Usage
ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.display()
Output:
10 -> 20 -> 30 -> None
7. Applications of Data Structures
1. Stacks
o Undo/Redo in editors.
o Expression evaluation (infix → postfix conversion).
o Function call management in recursion.
2. Queues
o CPU job scheduling.
o Printer task management.
o Ticket booking systems.
3. Linked Lists
o Dynamic memory allocation.
o Music/video playlists.
o Graph and tree implementation.
8. Conclusion
Data structures provide the backbone of efficient computing.
Stacks handle "reverse-order" operations.
Queues ensure fair "first-come-first-serve" order.
Linked Lists offer dynamic, memory-efficient storage.
Together, they form the foundation of advanced topics like trees,
graphs, hash maps, and algorithms that power real-world applications
from search engines to operating systems.