- Primitive Type Declaration
- Linear Static Structure (Array)
- Linear Dynamic Structure (Linked List)
- Non-Linear Structure (Binary Tree)
- Data Structures and Algorithms Relationship
File: primitive_type.c
Primitive Data Type - These are the basic building blocks of all data structures. They are built into the C language and store single values.
- Contiguous Memory: Yes, each primitive variable occupies a fixed, continuous block of memory
- Size: Fixed size (int = 4 bytes, float = 4 bytes, char = 1 byte)
- Storage: Stored directly in memory (usually on the stack)
- Student ID numbers - stored as integers
- Product prices - stored as floats
- Grade letters - stored as characters
- Temperature readings - stored as floats
- Age of a person - stored as integers
Primitive types are the simplest form of data storage. When you declare int val = 42, the computer allocates 4 bytes of memory and stores the number 42 directly in those bytes.
File: linear_static_array.c
Linear Static Data Structure
- Linear: Elements are arranged in a sequential order (one after another)
- Static: Size is fixed at compile time and cannot change during program execution
- Contiguous Memory: YES - All elements are stored in consecutive memory locations
- Memory Layout: If arr[0] is at address 1000, then arr[1] is at 1004, arr[2] at 1008, etc.
- Access Speed: Very fast - O(1) time complexity for accessing any element
- Student ID List - Fixed number of students in a classroom
- Days of the Week - Always 7 days
- Monthly Sales Data - 12 months in a year
- Exam Scores - Fixed number of students taking an exam
- Seat Numbers in a Theater - Fixed number of seats
- Fast access to any element using index
- Simple to use and understand
- Memory efficient (no extra space for pointers)
- Good cache performance due to contiguous memory
- Fixed size - cannot grow or shrink
- Insertion and deletion are expensive (need to shift elements)
- Memory waste if not all slots are used
File: linear_dynamic_linked_list.c
Linear Dynamic Data Structure
- Linear: Elements are arranged in a sequence (node1 → node2 → node3)
- Dynamic: Size can change during program execution (can add/remove nodes anytime)
- Non-Contiguous Memory: YES - Nodes can be scattered anywhere in memory
- Memory Layout: Nodes are connected through pointers, not physical location
- Access Speed: Slower - O(n) time complexity for accessing elements
- Extra Memory: Each node needs extra space for the pointer (address of next node)
- Browser History (Undo/Redo) - Can go back and forward
- Music Playlist - Add or remove songs dynamically
- Image Viewer - Navigate through photos (next/previous)
- Train Carriages - Each carriage linked to the next
- To-Do List Apps - Add or remove tasks anytime
- Dynamic size - can grow or shrink as needed
- Easy insertion and deletion (just change pointers)
- No memory waste - uses exactly what's needed
- No need to declare size upfront
- Slower access - must traverse from the beginning
- Extra memory for pointers
- No random access to elements
- More complex to implement than arrays
Each node contains:
- Data - The actual value (100, 200, 300)
- Next pointer - Address of the next node
Memory layout example:
Node1 (at address 2000): data=100, next=5040
Node2 (at address 5040): data=200, next=7200
Node3 (at address 7200): data=300, next=NULL
File: nonlinear_binary_tree.c
Non-Linear Data Structure
- Non-Linear: Elements are arranged in a hierarchy (parent-child relationship)
- Binary: Each node can have at most 2 children (left and right)
- Non-Contiguous Memory: YES - Nodes are scattered in memory
- Memory Layout: Nodes connected through two pointers (left and right)
- Access Speed: Medium - O(log n) for balanced trees
- Extra Memory: Each node needs space for two pointers
- File System Folders - Folders inside folders (hierarchy)
- Organization Chart - CEO → Managers → Employees
- Family Tree - Parents, children, grandchildren
- Decision Trees - Yes/No questions leading to outcomes
- HTML DOM Structure - Nested HTML elements
- Efficient searching in balanced trees
- Represents hierarchical relationships naturally
- Multiple ways to traverse (inorder, preorder, postorder)
- Good for sorted data storage
- More complex to implement
- Extra memory for two pointers per node
- Can become unbalanced (performance degrades)
- More difficult to understand for beginners
Tree structure:
50 (root)
/ \
30 70
(left) (right)
Each node contains:
- Data - The actual value
- Left pointer - Address of left child
- Right pointer - Address of right child
Data structures are ways to organize and store data in a computer's memory. Think of them as different types of containers:
- Arrays are like numbered boxes in a row
- Linked Lists are like train carriages connected together
- Trees are like family trees with branches
Algorithms are step-by-step instructions to solve a problem or perform a task. Think of them as recipes:
- Searching - Finding a specific item
- Sorting - Arranging items in order
- Inserting - Adding a new item
- Deleting - Removing an item
Different data structures make algorithms faster or slower:
Example: Finding a student's grade
- Array: If you know the position, access is instant (O(1))
- Linked List: Must check each student one by one (O(n))
- Binary Search Tree: Can eliminate half the data each step (O(log n))
Real-World Analogy:
- Array = Finding a book on a numbered shelf (instant if you know the number)
- Linked List = Following a treasure map from clue to clue
- Tree = Playing "20 Questions" to narrow down the answer
Different data structures use different amounts of memory:
Example: Storing 100 student IDs
- Array: 100 × 4 bytes = 400 bytes (just the data)
- Linked List: 100 × (4 bytes data + 8 bytes pointer) = 1200 bytes
- Binary Tree: 100 × (4 bytes data + 16 bytes for 2 pointers) = 2000 bytes
Trade-off:
- Arrays use less memory but are rigid
- Linked lists use more memory but are flexible
- Trees use the most memory but enable fast searching
Scenario 1: Student Grade List (Fixed Class Size)
- Data Structure: Array
- Why: Fixed number of students, need fast access
- Algorithm: Direct access by index
- Trade-off: Fast access, but can't easily add more students
Scenario 2: Browser Undo History
- Data Structure: Linked List
- Why: Unknown number of actions, frequent additions/deletions
- Algorithm: Insert at beginning, remove from beginning
- Trade-off: Flexible size, but slower to access middle items
Scenario 3: File System Search
- Data Structure: Binary Tree
- Why: Hierarchical structure, need fast searching
- Algorithm: Binary search
- Trade-off: Fast searching, but more complex to maintain
-
There's No Perfect Data Structure
- Arrays are fast but inflexible
- Linked Lists are flexible but slower
- Trees are powerful but complex
-
Speed vs Memory Trade-off
- More memory → Faster operations (e.g., hash tables)
- Less memory → Slower operations (e.g., compressed data)
-
Choose Based on Your Needs
- Lots of searching? → Use arrays or trees
- Lots of inserting/deleting? → Use linked lists
- Fixed size data? → Use arrays
- Unknown size? → Use linked lists or trees
Using Array:
- Add task: Must shift all items → Slow
- Remove task: Must shift all items → Slow
- Access task: Direct access → Fast
Using Linked List:
- Add task: Just create new node → Fast
- Remove task: Just change pointers → Fast
- Access task: Must traverse list → Slow
Best Choice: Linked List (because to-do lists change frequently)
| Feature | Array | Linked List | Binary Tree |
|---|---|---|---|
| Type | Linear Static | Linear Dynamic | Non-Linear |
| Memory | Contiguous | Non-Contiguous | Non-Contiguous |
| Size | Fixed | Flexible | Flexible |
| Access Speed | O(1) Fast | O(n) Slow | O(log n) Medium |
| Insert/Delete | O(n) Slow | O(1) Fast | O(log n) Medium |
| Memory Usage | Low | Medium | High |
| Use Case | Fixed-size data | Changing data | Hierarchical data |
gcc primitive_type.c -o primitive_type
./primitive_typegcc linear_static_array.c -o linear_static_array
./linear_static_arraygcc linear_dynamic_linked_list.c -o linear_dynamic_linked_list
./linear_dynamic_linked_listgcc nonlinear_binary_tree.c -o nonlinear_binary_tree
./nonlinear_binary_tree- Primitive types are the building blocks - they store single values
- Arrays are great when you know the size and need fast access
- Linked Lists are perfect when the size changes frequently
- Trees are ideal for hierarchical data and efficient searching
- Contiguous memory (arrays) is faster but less flexible
- Non-contiguous memory (lists, trees) is flexible but uses more space
- Data structures and algorithms must work together - the right structure makes algorithms faster
Remember: There's no "best" data structure - only the best one for your specific problem!
Good luck with your assignment! 🎓