Choosing a Data Structure in C

Intermediate
6 min read
Updated: Nov 2026
1 example

How to Choose

List the operations your program performs most, then choose the structure that makes them fast. Indexed access: array. Frequent insert/delete in the middle: linked list. LIFO or FIFO order: stack or queue. Fast lookup by key: hash table. Ordered data with fast search: binary search tree or sorted array. Always get the smallest/largest: heap. Relationships: graph.

Remember
/* need            -> use
   index access      -> array
   LIFO / FIFO       -> stack / queue
   lookup by key     -> hash table
   ordered + search  -> BST / sorted array
   min / max first   -> heap
   connections       -> graph */

Cheat Sheet

Keep these facts handy while you work with Choosing a Data Structure in C in C.

TopicDetail
ArrayAccess O(1), insert O(n)
Linked listInsert O(1) at head, search O(n)
Hash tableAverage O(1) lookup
BST (balanced)O(log n) search
HeapMin/max O(1), insert O(log n)

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Linked Lists.

Example 1 — Linear scan versus binary search

Count comparisons to find 70 in a sorted array.

C
#include <stdio.h>

int main(void) {
    int a[] = {10, 20, 30, 40, 50, 60, 70, 80};
    int key = 70, steps = 0;

    for (int i = 0; i < 8; i++) {
        steps++;
        if (a[i] == key) break;
    }
    printf("linear: %d comparisons\n", steps);

    int lo = 0, hi = 7;
    steps = 0;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        steps++;
        if (a[mid] == key) break;
        if (a[mid] < key) lo = mid + 1;
        else hi = mid - 1;
    }
    printf("binary: %d comparisons\n", steps);
    return 0;
}

How It Works

  1. Linear search checks seven elements.
  2. Binary search checks index 3 (40), index 5 (60), then index 6 (70).
  3. The gap grows quickly with larger arrays.

Edge Cases & Pitfalls

Check these before relying on Choosing a Data Structure in C in real programs.

one

Optimizing the wrong operation

A structure that is fast for lookup may be slow for insertion. Know which you do more.

prem

Premature optimization

Measure first. For small n the simplest structure wins.

Key Takeaways

  • Start from the operations.
  • Know the Big O of each choice.
  • Prefer simple until measured.

One line: List the operations your program performs most, then choose the structure that makes them fast.

Frequently Asked Questions

Identify the operations you need most often and the size of your data, then pick the structure with the best complexity for those operations.
When you need random access, iterate often, or want cache-friendly memory. Linked lists win mainly for frequent insertions and deletions at known positions.
Continue with "C Linked Lists" next. Build singly, doubly, and circular linked lists with malloc: create, traverse, insert, delete, search, and reverse. The C Data Structures hub lists every topic in order.

Next: Linked Lists

Build singly, doubly, and circular linked lists with malloc: create, traverse, insert, delete, search, and reverse.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful