Data Structure Cheat Sheet
Data Structure Cheat Sheet
1. Arrays
Definition-
Operations-
Access: O(1)
Search: O(n)
Insert: O(n) (worst case, shifting elements)
Delete: O(n) (worst case, shifting elements)
Use Case-
Example Code-
#include <stdio.h>
int main() {
// Access element
int n = 5;
arr[2] = 10;
n++;
// Print array
return 0;
2. Linked Lists
Definition-
A linked list is a collection of nodes where each node contains data and a reference
to the next node.
Types-
Operations-
Access: O(n)
Search: O(n)
Insert: O(1) (if inserting at head)
Delete: O(1) (if deleting at head)
Use Case-
Example Code-
#include <stdio.h>
#include <stdlib.h>
int data;
};
newNode->data = newData;
newNode->next = *head;
*head = newNode;
node = node->next;
printf("NULL\n");
int main() {
insertAtHead(&head, 2);
insertAtHead(&head, 3);
printList(head);
return 0;
3. Stacks
Definition-
A stack is a collection of elements with Last In, First Out (LIFO) access.
Operations-
Use Case-
Example Code-
#include <stdio.h>
#include <stdlib.h>
struct StackNode {
int data;
// Push operation
newNode->data = newData;
newNode->next = *top;
*top = newNode;
// Pop operation
if (*top == NULL) {
printf("Stack underflow\n");
return -1;
*top = (*top)->next;
free(temp);
return popped;
// Peek operation
int peek(struct StackNode* top) {
if (top == NULL) {
return -1;
return top->data;
int main() {
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
return 0;
4. Queues
Definition-
A queue is a collection of elements with First In, First Out (FIFO) access.
Types-
Simple Queue
Circular Queue
Priority Queue
Deque
Operations-
Use Case-
Example Code-
#include <stdio.h>
#include <stdlib.h>
struct QueueNode {
int data;
};
struct Queue {
};
temp->next = NULL;
return temp;
return q;
// Enqueue operation
if (q->rear == NULL) {
return;
q->rear->next = temp;
q->rear = temp;
// Dequeue operation
int dequeue(struct Queue* q) {
if (q->front == NULL) {
return -1;
q->front = q->front->next;
if (q->front == NULL) {
q->rear = NULL;
free(temp);
return data;
int main() {
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
return 0;
}
5. Trees
Definition-
A tree is a hierarchical structure with nodes, with one node as the root and zero or
more child nodes.
Types-
Binary Tree
Binary Search Tree (BST)
AVL Tree
Red-Black Tree
B-trees
Operations (BST)-
Access: O(log n)
Search: O(log n)
Insert: O(log n)
Delete: O(log n)
Use Case-
#include <stdlib.h>
struct Node {
int data;
};
node->data = data;
return node;
if (node == NULL) {
return newNode(data);
return node;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
int main() {
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
6. Heaps
Definition-
Types-
Min-Heap
Max-Heap
Operations-
Insert: O(log n)
Delete (root): O(log n)
Peek (min/max): O(1)
Use Case-
struct MinHeap {
int size;
int array[MAX_HEAP_SIZE];
};
// Function to swap two elements
*x = *y;
*y = temp;
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
smallest = left;
smallest = right;
if (smallest != i) {
swap(&minHeap->array[i], &minHeap->array[smallest]);
minHeapify(minHeap, smallest);
}
}
if (minHeap->size == MAX_HEAP_SIZE) {
return;
minHeap->size++;
int i = minHeap->size - 1;
minHeap->array[i] = key;
i = (i - 1) / 2;
if (minHeap->size <= 0)
return INT_MAX;
if (minHeap->size == 1) {
minHeap->size--;
return minHeap->array[0];
minHeap->size--;
minHeapify(minHeap, 0);
return root;
minHeap->size = 0;
return minHeap;
int main() {
insertKey(minHeap, 3);
insertKey(minHeap, 2);
insertKey(minHeap, 1);
insertKey(minHeap, 15);
insertKey(minHeap, 5);
insertKey(minHeap, 4);
insertKey(minHeap, 45);
return 0;
7. Hash Tables
Definition-
Operations-
Use Case-
Example Code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 10
struct HashNode {
int key;
int value;
};
struct HashTable {
};
// Hash function
newNode->key = key;
newNode->value = value;
newNode->next = NULL;
if (ht->table[hashIndex] == NULL) {
ht->table[hashIndex] = newNode;
} else {
temp = temp->next;
temp->next = newNode;
if (temp->key == key) {
return temp->value;
temp = temp->next;
}
return -1;
// Delete a key
prev = temp;
temp = temp->next;
if (temp == NULL) {
return;
if (prev == NULL) {
ht->table[hashIndex] = temp->next;
} else {
prev->next = temp->next;
}
free(temp);
int main() {
memset(ht->table, 0, sizeof(ht->table));
insert(ht, 1, 10);
insert(ht, 2, 20);
insert(ht, 3, 30);
delete(ht, 2);
return 0;
8. Graphs
Definition-
Types-
Directed
Undirected
Weighted
Unweighted
Representations-
Adjacency List
Adjacency Matrix
Operations-
Use Case-
#include <stdlib.h>
struct AdjListNode {
int dest;
};
struct AdjList {
struct Graph {
int V;
};
newNode->dest = dest;
newNode->next = NULL;
return newNode;
graph->V = V;
graph->array[i].head = NULL;
}
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
while (pCrawl) {
pCrawl = pCrawl->next;
}
printf("\n");
int main() {
int V = 5;
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printGraph(graph);
return 0;
Definition-
A trie is a tree-like data structure that stores a dynamic set of strings, typically used
for searching.
Operations-
Use Case-
Example Code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALPHABET_SIZE 26
struct TrieNode {
int isEndOfWord;
};
pNode->isEndOfWord = 0;
pNode->children[i] = NULL;
return pNode;
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
pCrawl->isEndOfWord = 1;
if (!pCrawl->children[index])
return 0;
pCrawl = pCrawl->children[index];
}
return (pCrawl != NULL && pCrawl->isEndOfWord);
int main() {
char keys[][8] = {"the", "a", "there", "answer", "any", "by", "bye", "their"};
insert(root, keys[i]);
return 0;
Traversal-
Sorting-
Arrays: QuickSort, MergeSort, BubbleSort, etc.
Linked Lists: MergeSort (efficient for linked lists)
Searching-