Section 4
Section 4
1. Programming in C
C is a general-purpose, procedural programming language widely used for system programming and
embedded systems. It provides low-level access to memory and offers extensive control over system
resources. Here's an overview of key topics in C:
o Functions: Functions are blocks of code designed to perform a specific task. The
main() function is the entry point for every C program.
o Variables: Data is stored in variables, which must be declared before use (e.g., int x;).
o Control Flow: Includes conditional statements (if, else, switch) and loops (for, while,
do-while).
o Pointers: Pointers hold memory addresses and are essential for dynamic memory
allocation and working with arrays and structures.
o Arrays: Arrays are used to store multiple values of the same type in contiguous
memory locations.
o Structures: Structures are used to define custom data types that group different data
types together.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int sum = a + b;
return 0;
2. Recursion
Recursion is a programming technique where a function calls itself to solve a problem. Recursive
solutions are typically used for problems that can be broken down into smaller, similar subproblems.
• Base Case: Every recursive function needs a base case to stop recursion.
• Recursive Case: The case where the function calls itself.
int factorial(int n) {
if (n == 0 || n == 1)
else
For factorial(5), the function will call factorial(4), factorial(3), and so on, until it reaches the base case
factorial(0) or factorial(1).
3. Arrays
An array is a collection of elements, all of the same type, stored in contiguous memory locations.
Array Operations:
4. Stacks
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. It supports two
main operations:
• Expression evaluation
#include <stdlib.h>
#define MAX 5
int stack[MAX];
void push(int x) {
if (top == MAX - 1)
printf("Stack Overflow\n");
else
stack[++top] = x;
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
return stack[top--];
int main() {
push(10);
push(20);
return 0;
5. Queues
A queue is a linear data structure that follows the First In First Out (FIFO) principle. It supports two
main operations:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int queue[MAX];
void enqueue(int x) {
if (rear == MAX - 1)
printf("Queue Overflow\n");
else {
if (front == -1)
front = 0;
queue[++rear] = x;
int dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return -1;
} else {
int x = queue[front++];
return x;
int main() {
enqueue(10);
enqueue(20);
return 0;
6. Linked Lists
A linked list is a collection of nodes where each node contains data and a reference (or pointer) to
the next node in the sequence.
• Doubly Linked List: Each node points to both the next and previous nodes.
Operations:
• Insertion: Adding nodes to the list (at the beginning, end, or middle).
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
void insert_at_end(struct Node **head, int data) {
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
last = last->next;
last->next = new_node;
head = head->next;
printf("NULL\n");
int main() {
insert_at_end(&head, 10);
insert_at_end(&head, 20);
insert_at_end(&head, 30);
display(head);
return 0;
}
7. Trees
A tree is a hierarchical data structure consisting of nodes, with a single node designated as the root.
Each node has zero or more child nodes.
• Binary Tree: A tree where each node has at most two children, commonly referred to as left
and right children.
• Binary Search Tree (BST): A binary tree where the left subtree of a node contains only nodes
with values smaller than the node’s value, and the right subtree contains only nodes with
values larger than the node’s value.
struct Node {
int data;
};
node->data = data;
return node;
if (node == NULL)
return newNode(data);
else
return node;
}
8. Binary Heaps
A binary heap is a complete binary tree that satisfies the heap property:
• Max-Heap: The value of each node is greater than or equal to the values of its children.
• Min-Heap: The value of each node is less than or equal to the values of its children.
9. Graphs
A graph is a collection of vertices (nodes) and edges (connections between nodes). Graphs can be
directed or undirected.
• Representation:
o Adjacency List: A list where each vertex points to a list of its adjacent vertices.
• Graph Traversal:
o Depth-First Search (DFS): Explores as far as possible down one branch before
backtracking.
o Breadth-First Search (BFS): Explores all vertices at the current depth level before
moving to the next level.
Summary
This section introduces fundamental programming techniques in C and covers key data structures
such as arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, and graphs.
Understanding these concepts is essential for solving algorithmic problems and building efficient
software.