π How to Store Data Efficiently β Data Structures
Push, pop, enqueue and dequeue β see how stacks, queues and other data structures work with animated operations.
Designed with the WJEC specification in mind
CS A-Level Unit 3CS GCSE Β§1.2
π€ What are data structures?
Data structures are different ways to organize and store data in computer memory. Just like organizing your bedroom, some methods work better for different tasks β stacks for undo operations, queues for printer jobs!
π Analogy: A stack is like a pile of dinner plates (take from the top). A queue is like a line at the shop (first person served first). Different structures, different rules!
π¦ Stack
πΆ Queue
π Linked List
π³ Binary Tree
ποΈ Hash Table
π¦
Stack (LIFO β Last In, First Out)
Size: 0 / 8
Ready. Push some values onto the stack.
Stack operations: Push (add to top), Pop (remove from top), Peek (look at top without removing). Real-world examples: Undo button, browser back button, function call stack, reversing a string. Time complexity: Push O(1), Pop O(1), Peek O(1)
πΆ
Queue (FIFO β First In, First Out)
Size: 0 / 8
Ready. Enqueue some values.
Queue operations: Enqueue (add to rear), Dequeue (remove from front), Peek (look at front). Real-world examples: Print queue, customer service queue, message buffers, CPU scheduling. Time complexity: Enqueue O(1), Dequeue O(1), Peek O(1)
π
Linked List
Ready. Add some nodes.
Linked List: Each node stores a value AND a pointer to the next node. Unlike arrays, elements aren't stored contiguously in memory. Advantages: Dynamic size, efficient insertion/deletion at front O(1). Disadvantages: No random access (must traverse from head), extra memory for pointers.
π³
Binary Search Tree (BST)
Insert values to build a Binary Search Tree.
Binary Search Tree (BST): Each node has at most two children. Left child is always smaller, right child is always larger. Traversals: In-order (LeftβRootβRight) gives sorted order. Pre-order (RootβLeftβRight) copies the tree. Post-order (LeftβRightβRoot) deletes safely. Search: O(log n) average β halves the search space each step, like binary search on an array. CS A-Level Unit 3
ποΈ
Hash Table
Insert key-value pairs into the hash table.
Hash Table: Uses a hash function to convert a key into an array index. Allows O(1) average lookup time β much faster than searching a list. Hash Function: Takes the key and produces a number (index). Simple example: sum of character codes mod table size. Collisions: When two keys hash to the same index. Resolved here using linear probing β if the slot is taken, try the next one. Load Factor: Number of items Γ· table size. Higher load factor = more collisions = slower lookups. CS A-Level Unit 3
π
Exam Tips
GCSE: Know the difference between stacks (LIFO) and queues (FIFO)
A-Level: Understand when to use each structure: stacks for undo, queues for scheduling
Key point: Hash tables offer O(1) average lookup time but O(n) worst case
Remember: Stack = Last In First Out, Queue = First In First Out