Data Structure and Algorithm: Dynamic Memory Allocation

bhartisalunke1 0 views 27 slides Sep 28, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

Dynamic Memory Allocation


Slide Content

Dynamic Memory Allocation & Recursion

Example program

Final words

Note: Memory allocated in heap will not be released automatically, it’s programmer responsibility to release that memory

Recursion Recursion means functions call itself until some conditions to be satisfy. This technique provides a way to break complicated problems down into simple problems which are easier to solve . Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:

GCD The GCD is a mathematical term for the Greatest Common Divisor of two or more numbers. It is the Greatest common divisor that completely divides two or more numbers without leaving any remainder. Therefore, it is also known as the Highest Common Factor (HCF) of two numbers. For example, the GCD of two numbers, 20 and 28, is 4 because both 20 and 28 are completely divisible by 1, 2, 4 (the remainder is 0), and the largest positive number among the factors 1, 2, and 4 is 4.

GCD # include < stdio.h > int gcd ( int a, int b) { if (b == 0) return a; return gcd (b, a % b); } int main() { int a = 56, b = 98; printf ("GCD of %d and %d = %d\n", a, b, gcd (a, b)); return 0; }

Factorial # include <stdio.h> int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { int num = 5; printf("Factorial of %d = %d\n", num, factorial(num)); return 0; }

Fibonacci series The Fibonacci series is a numerical sequence achieved through specific programming constructs. The series, conventionally, commences from 0 and 1, with subsequent numbers calculated as the sum of the prior two. For instance, a Fibonacci series beginning with 0 and 1 appears as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Tower of Hanoi Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod. obeying the following simple rules: Only one disk can be moved at a time. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. No disk may be placed on top of a smaller disk.
Tags