DSA Unit 1
DSA Unit 1
DSA Unit 1
Code: BCECCE3102
Introduction to course
What are Data Structures?
Definition: Data structures are a way of organizing and storing data to enable
efficient access and modification.
Real-World Applications
Relevance to You:-
Data Structures and Algorithms are crucial for CSE students as they provide a solid academic
foundation, practical skills for software development and system design, enhanced career
opportunities, and personal growth in logical thinking and problem-solving abilities. Mastering DSA
prepares students for successful careers in the dynamic and evolving field of computer science and
engineering.
4. Linked List 9
S.
Text Books: Author Edition Publication
No
Data Structures with C (Schaum's Seymour Latest McGraw Hill
1. Education.
Outline Series) Lipschutz
Data Structures and program Robert Kruse Latest Pearson
2. Education
designing using ‘C’
Reference Book
1. Introduction to Data Structures in C by - Kamthane Pearson Education 2005
2. Data Structures Using C by- Bandyopadhyay Pearson Education
Online Resources
1. https://www.gatevidyalay.com/data-structures/
2. https://www.tutorialspoint.com/data_structures_algorithms/index.htm
Introduction to Data Structures
Types:-
• Integer: Whole numbers.
Types:-
Types:-
Non-Primitive:-
• Built using primitive types.
• Can be dynamically sized and more complex.
• Linked Lists:
• Definition: Collection of elements where each element points to the
next.
• Types: Singly, doubly, and circular linked lists.
• Operations: Insert, delete, traverse.
Arrays
• Advantages:
• Fast access via index.
• Simple and easy to use.
• Disadvantages:
• Fixed size.
• Costly insertion and deletion operations.
Linked Lists
• Advantages:
• Dynamic size.
• Efficient insertions / deletions.
• Disadvantages:
• Higher memory usage due to pointers.
• Sequential access only (no random access).
Stacks
• Definition: Collection of elements with LIFO (Last In, First Out) access.
• Definition: Collection of elements with FIFO (First In, First Out) access.
• Examples:
• O(1): Constant time/space
• O(n): Linear time/space
• O(n²): Quadratic time/space
Time Complexity: Examples
int** createMatrix(int n)
{
int **matrix = malloc(n * sizeof(int *));
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(n * sizeof(int));
}
return matrix;
}
• Recursive Fibonacci
int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
• Introduction to Pointers
• Key Uses:
• Dynamic memory allocation
• Array manipulation
• Function arguments (pass-by-reference)
• Syntax: &variableName
• Example Code:
• Output:
Understanding the Address-of Operator (&)
• Example:
int num = 20;
int *ptr = #
• Example Code:
int var = 5;
int *ptr = &var;
printf("Pointer address: %p\n", ptr);
printf("Pointer value: %d\n", *ptr);
printf("Pointer value: %p\n", &var);
Accessing a Variable through Its Pointer
• Syntax: *pointerName
• Example Code:
int num = 100;
int *ptr = #
printf("Value of num: %d\n", *ptr);
• Output:100
Modifying Values via Pointers
• Example Code:-
#include <stdio.h>
int main()
{
int x = 25;
int *p = &x;
printf("Original value of x: %d \n", x);
*p = 50; // Change value through pointer
printf("Modified value of x: %d \n", x);
return 0;
}
• Example Code:
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 10, y = 20;
swap(&x, &y);
printf("x: %d, y: %d \n", x, y);
return 0;
}
• Output: x: 20, y: 10
Practical Applications of Pointers
• Example Code:-
Outline
• Introduction
• Characteristics:
• Fixed size.
• Efficient in terms of speed.
• Limited flexibility.
• Example:
int array[10];
Dynamic Memory Allocation
• Characteristics:
• Flexible size.
• Can grow or shrink.
• Slightly slower due to runtime overhead.
• Example:
int *array = (int*) malloc(10 * sizeof(int));
Memory Allocation Functions in C
• Syntax:-
ptr = (cast-type*) malloc(byte-size)
• Example:-
int *ptr = (int*) malloc(10 * sizeof(int));
• Syntax:-
ptr = (cast-type*)calloc(n, element-size);
• Example:-
int *ptr = (int*)calloc(10, sizeof(int));
free()
• Function: Frees dynamically allocated memory.
• Syntax:-
free(pointer);
• Example:-
free(ptr);
realloc()
• Syntax:-
ptr = (int*) realloc(ptr, newSize);
• Example:-
ptr = (int*) realloc(ptr, 20 * sizeof(int));
Initializatio
Function Use Case Example
n
calloc Allocate and initialize Yes (to zero) ptr = calloc(10, sizeof(int));
Resize allocated
realloc N/A ptr = realloc(ptr, new_size);
memory
Conclusion
• Static Memory Allocation: Fixed size, compile-time.
• Advantages of Recursion
• Components:
• Base Case: Condition where recursion stops.
• Recursive Case: Part of the function that calls itself.
• Example:-
Factorial of n (n!) = n * (n-1)!
How Recursion Works
• Function Call Stack: Each recursive call pushes a new frame onto the
stack.
• Reduction of Code: Reduces the need for complex loops and additional
variables.
• Definition: Sequence where each number is the sum of the two preceding
ones.
• Series: 0, 1, 1, 2, 3, 5, 8, ...
• Mathematical Formula:
F(n)=F(n−1)+F(n−2)
Explanation: Each call computes Fibonacci of n-1 and n−2 until base
cases are met.
Greatest Common Divisor (GCD)
• Definition: Largest number that divides two numbers without a remainder.
GCD(a,b)=GCD(b, a % b)