5. Data Structures

5.1 Linked Lists

  • A linked list is a collection of nodes, where each node contains data and a pointer to the next node.
Node Class:
class Node:
    def __init__(self, data):

self.data = data

self.next = None

Example Traversal:

head = Node(1)

second = Node(2)

head.next = second

current = head

while current is not None:
    print(current.data)

current = current.next

5.2 Stacks (Last-In-First-Out)

  • Operations:
    • push: Add item to the top
    • pop: Remove item from the top
    • peek: View the top item without removing
    • isEmpty: Check if stack is empty
Example (using list):

stack = []

stack.append(10)

stack.append(20)

print(stack.pop())  # 20

5.3 Queues (First-In-First-Out)

  • Operations:
    • enqueue: Add to the rear
    • dequeue: Remove from the front
Example (using list):
from collections import deque

queue = deque()

queue.append(1) # enqueue

queue.append(2)

print(queue.popleft())  # dequeue: 1

5.6 Stack and Queue Implementations

  • Stack and Queue can be implemented using both arrays and linked structures.
  • Arrays offer fast indexing but fixed size.
  • Linked structures use dynamic memory and are flexible in size.

This concludes the detailed beginner-friendly computing notes for topics 1 to 5. Let me know if you’d like practice questions, concept maps, or a printable version!