0% found this document useful (0 votes)
17 views

Section 4

This document provides an overview of programming in C and essential data structures including arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, and graphs. It covers key programming concepts such as recursion, syntax, and control flow, along with examples for each data structure. Mastery of these concepts is crucial for solving algorithmic problems and developing efficient software.

Uploaded by

sinhakishan718
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Section 4

This document provides an overview of programming in C and essential data structures including arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, and graphs. It covers key programming concepts such as recursion, syntax, and control flow, along with examples for each data structure. Mastery of these concepts is crucial for solving algorithmic problems and developing efficient software.

Uploaded by

sinhakishan718
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Section 4

Programming and Data Structures

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:

• Syntax and Structure: The basic structure of a C program includes:

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.

Example of a simple C program:

#include <stdio.h>

int main() {

int a = 5;

int b = 10;

int sum = a + b;

printf("Sum is: %d\n", sum);

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.

Example: Calculating the factorial of a number using recursion:

int factorial(int n) {

if (n == 0 || n == 1)

return 1; // Base case

else

return n * factorial(n - 1); // Recursive case

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.

• One-Dimensional Arrays: A simple list of elements.

• int arr[5] = {1, 2, 3, 4, 5};

• Multi-Dimensional Arrays: Arrays of arrays (e.g., matrices).

• int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

Array Operations:

• Accessing elements: arr[i]

• Traversing: Looping through array elements using loops.

• Modifying elements: arr[i] = value;

4. Stacks

A stack is a linear data structure that follows the Last In First Out (LIFO) principle. It supports two
main operations:

• Push: Add an element to the top of the stack.

• Pop: Remove the top element from the stack.

Stacks are typically used for:

• Expression evaluation

• Function call management (call stack)

• Undo mechanisms in applications

Example of stack operations:


#include <stdio.h>

#include <stdlib.h>

#define MAX 5

int stack[MAX];

int top = -1;

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);

printf("Popped: %d\n", pop());

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:

• Enqueue: Add an element to the back of the queue.

• Dequeue: Remove an element from the front of the queue.

Queues are often used in scenarios such as:

• Scheduling tasks (e.g., CPU scheduling)

• Handling requests in a server

• Breadth-First Search (BFS) in graphs

Example of queue operations:

#include <stdio.h>

#include <stdlib.h>

#define MAX 5

int queue[MAX];

int front = -1, rear = -1;

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++];

if (front > rear)

front = rear = -1;

return x;

int main() {

enqueue(10);

enqueue(20);

printf("Dequeued: %d\n", dequeue());

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.

• Singly Linked List: Each node points to the next node.

• 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).

• Deletion: Removing nodes from the list.

• Traversal: Visiting each node in the list.

Example of singly linked list:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};
void insert_at_end(struct Node **head, int data) {

struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));

struct Node *last = *head;

new_node->data = data;

new_node->next = NULL;

if (*head == NULL) {

*head = new_node;

return;

while (last->next != NULL)

last = last->next;

last->next = new_node;

void display(struct Node *head) {

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

int main() {

struct Node *head = NULL;

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.

Example of a binary search tree:

struct Node {

int data;

struct Node *left, *right;

};

struct Node* newNode(int data) {

struct Node *node = (struct Node*)malloc(sizeof(struct Node));

node->data = data;

node->left = node->right = NULL;

return node;

struct Node* insert(struct Node* node, int data) {

if (node == NULL)

return newNode(data);

if (data < node->data)

node->left = insert(node->left, data);

else

node->right = insert(node->right, data);

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.

Heaps are typically used in priority queues and heap sort.

9. Graphs

A graph is a collection of vertices (nodes) and edges (connections between nodes). Graphs can be
directed or undirected.

• Representation:

o Adjacency Matrix: A 2D array where matrix[i][j] is 1 if there is an edge between


vertex i and vertex j, otherwise 0.

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.

You might also like