C
Data Structures &
Algorithms
inC Programming
A comprehensive guide to fundamental data structures with practical C
programming examples and implementations
Arrays &
Strings
Linked
Lists
Stacks &
Queues
Trees Graphs &
Hashing
Made with
Genspark
Presented By
O.MANOJ
2.
Table of Contents
Acomprehensive guide to fundamental data structures in C
programming
1 Arrays & Strings
Declaration, initialization, indexing, traversal, searching, sorting operations with
practical C examples
2 Linked Lists
Singly, doubly, and circular linked lists with insertion, deletion, and traversal
operations
3 Stacks & Queues
LIFO and FIFO principles, implementation using arrays and linked lists with
applications
4 Trees
Binary trees, BST operations, tree traversals (inorder
, preorder
, postorder),
insertion, deletion
5 Graphs & Hashing
Graph representations, adjacency matrix/list, hash table implementation,
collision handling
Each
3.
Arrays & Strings:Concepts & Operations
Key
Concepts
Arrays
Collection of elements of the same type stored in contiguous memory
locations
Fixed size determined at declaration
Zero-indexed: first element at index
0 Direct access to elements using
indices
Strings
Arrays of characters terminated by a null
character '0'
Character array with null
terminator Can use string.h library
functions Manual memory
management required
Common
Operations
Traversal
Visiting each
element
Searching
Linear & binary
search
Sorting Manipulatio
Visual
Representation
int arr[5] = {10, 25, 8, 15, 42};
Integer
Array
0 1
2
3
10 25 8 15 42
4
char str[6] = "Hello"; // Null-terminated
String (Character
Array)
0 1 2 3
4
H e l l o 0
5
4.
Programming Example: Arrays&
Strings
Example 1: Array Traversal in C
array_traversal.c
/* Program to demonstrate array traversal
*/
#include <stdio.h>
int main() {
int arr[5] = {10,
20, 30, 40, 50};
// Array
traversal
using for
loop
printf("Array
elements: ");
for (int i = 0; i < 5; i++)
{ printf("%d ", arr[i]);
}
printf("n");
return 0;
}
Output:
Array elements: 10 20 30 40 50
Arrays are declared with a fixed
size Indexing starts at 0
Elements are accessed using the
[] operator
Example 2: String Manipulation in C
string_operations.c
5.
Arrays & Strings:Searching & Sorting
Example
Example 1: Linear Search in C
linear_search.c
/* Program to demonstrate linear search in an array */
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
// Traverse array and compare each element with key
for (int i = 0; i < n; i++) { if
(arr[i] == key)
return i; // Return index if key is found
}
return -1; // Return -1 if key is not
found
}
int main() {
int arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170};
int n = sizeof(arr)/sizeof(arr[0]); int key =
110;
int result = linearSearch(arr
, n, key);
if (result == -1)
printf("Element %d not foundn", key);
else
6.
Linked Lists: Types& Concepts
Key
Concepts
Linked
Lists
Dynamic data structure with nodes connected via
pointers
Dynamic size - grows/shrinks at
runtime Non-contiguous memory
allocation Efficient insertion and
deletion
Types of Linked
Lists
Singly Linked List
Each node contains data and pointer to next
node
Doubly Linked List
Each node contains data and pointers to both previous and next
nodes
Circular Linked
List
Last node points back to first node forming a
circle
Visual
Representation
Singly Linked
List
struct Node { int data; struct Node* next; };
10 → 20 → 30 ∅
Doubly Linked
List
struct Node { int data; struct Node *prev,
*next; };
∅ 15 → ← 25 ∅
Circular Linked
List
7.
Programming Example: Singly
LinkedList
Node Structure and List Creation
singly_linked_list.c
/* Singly Linked List implementation in C */
#include <stdio.h>
#include <stdlib.h>
// Define the
structure for a
node
struct Node {
int data;
// Data stored
in the node
struct Node* next;
// Pointer to the
next node
};
// Function to
create a new node
struct Node*
createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if
(newNode == NULL) {
printf("Memory allocation failedn"); exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
8.
Programming Example: Doubly& Circular
Linked Lists
Example 1: Doubly Linked List Implementation
doubly_linked_list.c
/* Program to implement a doubly linked list */
#include <stdio.h>
#include
<stdlib.h>
// Node structure
for doubly linked
list
struct DNode {
int data;
struct DNode* prev;
struct DNode* next;
};
// Function to create
a new node
struct DNode*
createNode(int data) {
struct DNode* newNode = (struct DNode*)malloc(sizeof(struct DNode));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
9.
Stacks & Queues:Concepts & Applications
Key Concepts
Stacks (LIFO)
A linear data structure that follows Last In, First Out principle
Elements added/removed only from one end
(top) Main operations: push (add), pop
(remove) peek/top (access top element
without removing)
Queues (FIFO)
A linear data structure that follows First In, First
Out principle
Elements added at one end (rear) and removed from other end
(front) Main operations: enqueue (add), dequeue (remove)
Front/peek (access front element without removing)
Common Applications
Function Calls
Call stack in
programming
Expression
Evaluation
Infix to postfix
conversion
CPU Scheduling BFS Algorithm
Visual
Representation
Stack (LIFO)
Element 3
(Top)
Element 2
Element 1
Pus
h
Pop
Last In, First Out (LIFO)
Queue
(FIFO)
10.
Programming Example: Stack
Implementation
StackImplementation Using Arrays in C
(LIFO)
stack_implementation.c
/* Program to implement stack operations using array
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX];
int top = -1;
// Array to store the
stack
// Initialize top of
stack
// Function to push an element onto
stack
void push(int item) { if
(top >= MAX-1) {
printf("Stack Overflown"); return;
}
stack[++top] = item;
printf("%d pushed to stackn", item);
}
// Function to pop an element
from stack
int pop() {
if (top < 0) {
printf("Stack Underflown"); return -
1;
11.
Programming Example: Queue
Implementation
Example1: Queue Implementation using
Arrays (FIFO)
queue_implementation.c
/* Queue implementation using arrays in C */
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
// Queue structure
and operations
int queue[MAX];
int front = -1, rear = -
1;
// Function to
check if queue is
empty
int isEmpty() {
return (front == -
1 && rear == -1);
}
// Function to
check if queue is
full
int isFull() {
return (rear ==
MAX - 1);
}
// Function to add
an element to the
queue
void enqueue(int data)
{ if (isFull()) {
printf("Queue is fulln"); return;
}
if (isEmpty()) { front =
rear = 0;
} else {
rear++;
12.
Trees: Binary Trees,BST
Concepts
Key Concepts
Trees
Hierarchical data structure with a root node and child
nodes
Non-linear data structure with hierarchical
relationships Collection of nodes connected by
edges
Exactly one path between any two nodes
Binary Trees
Trees where each node has at most two children
Every node has at most 2 children (left and
right) Efficient for searching and sorting
Common types: full, complete, perfect,
balanced
Binary Search Trees (BST)
Binary trees with ordered nodes for efficient
searching
Left subtree contains nodes with values <
Common Operations
Traversals: Inorder
, Preorder
, Postorder
Visual
Representation
Binary Search Tree
Leaf Leaf Leaf Leaf
A balanced BST with 7 nodes and height 2
Roo
t
50
30 70
20 40 60 80
13.
Programming Example: BST
Implementationin C
BST Node Structure and Insertion
bst_implementation.c
/* Binary Search Tree implementation in C */
#include <stdio.h>
#include
<stdlib.h>
// Structure for
BST node
struct Node {
int data;
struct Node
*left, *right;
};
// Function to
create a new node
struct Node*
newNode(int item) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a new node with given key
14.
Graphs & Hashing:Concepts &
Representations
Key
Concepts
Graphs
Non-linear data structure consisting of vertices (nodes)
and edges
Directed vs. Undirected graphs
Weighted vs. Unweighted graphs
Connected vs. Disconnected
graphs
Hashing
Technique to map data of arbitrary size to fixed-size
values
Hash functions convert keys to array indices
O(1) average time complexity for search, insert,
delete Requires collision handling strategies
Representatio
ns
Adjacency
Matrix
2D array of V×V
size
Adjacency List
Array of linked
lists
Visual
Representation
Graph
Structure
A
B C
Undirected graph with 4 vertices and 4
edges
D
Hash
Table
0 42 → Value X
1
2 13 → Value Y
3 24 → Value Z
15.
Programming Example: Graphs& Hash
Tables
Example 1: Graph Adjacency List in C
graph_adjacency_list.c
/* Graph implementation using adjacency list */
#include <stdio.h>
#include <stdlib.h>
// Structure for
an adjacency list
node
struct AdjListNode
{ int dest;
struct AdjListNode*
next;
};
// Structure for an
adjacency list
struct AdjList {
struct AdjListNode*
head;
};
// Structure for a
graph
struct Graph
{ int V;
struct AdjList*
array;