C Mastery / Data Structure Foundations and Complexity
Part 8 — Data Structures and Algorithms in C

Data Structure Foundations and Complexity

This chapter establishes the foundation for the data-structures part: how to think about data structures in C, and how to reason about their complexity.

Why This Matters

In C, you build your own data structures — there is no standard container library. Choosing the right structure (and understanding its time/space cost) is the difference between a working system and one that collapses under load.

Prerequisites

Core Concept

A data structure is a way of organizing data plus the operations to access and modify it. In C, structures are built from:

Complexity

Big-O describes how an operation's cost grows with input size n:

NotationGrowthExample
O(1)constantarray indexing, hash lookup (avg)
O(log n)logarithmicbinary search, balanced tree lookup
O(n)linearlinear search, array insert (shift)
O(n log n)linearithmicefficient sorts
O(n²)quadraticnested loops, insertion sort

Complexity is about growth rate, not wall-clock time; constant factors matter in practice but are hidden by Big-O.

How It Works

Each structure trades off different costs. Arrays give O(1) random access but O(n) insertion; linked lists give O(1) insertion at a known position but O(n) search; hash tables give O(1) average lookup but poor worst case; balanced trees give O(log n) worst-case operations with ordering.

Variations

Intrusive vs. container

Intrusive structures avoid extra allocations and are common in kernels and embedded code.

Common Mistakes

Undefined Behavior

Portability

allocator and cache behavior.

Under the Hood

Arrays are contiguous (cache-friendly); linked structures chase pointers (cache-unfriendly but flexible). Balanced trees and hash tables trade memory for speed. c.perf.2 covers locality.

Practical Usage

Exercises

1. Classify the complexity of array push (amortized), linked-list prepend, and hash-table lookup. 2. Write a struct for a singly linked node (intrusive style) and a function to create/destroy a list.

Deep Challenge

Explain why a linked list can be slower than a vector even for operations the list is "supposed" to win, citing cache locality and allocation overhead. Give a concrete scenario.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.ds.complexity05