DS Chapter 1
DS Chapter 1
Unit: 01
INTRODUCTION TO DATA STRUCTURES
Chithra T R Page 1
DATA STRUCTURES
Stacks: - it is a linear data structure, it uses the [LEFO] rule in which data added at the last will be
removed at the first.
Queues: - it is a linear data structure, it uses the [FIFO] rule in which element added at the last and
remove at the first.
Linked List: - it is a collection of NODES that are made up of two parts i.e., data and address of
next node.
2. Non-Linear Data Structure: - the elements which are accessed or placed in the non-contagious or
non-sequential way of memory allocation is called as non-linear data structure.
Non-linear data structure or classified into two types:
1. Trees: - it is a collection off nodes where these nodes are arranged hierarchically and form a parent
child relationship. Example: -
Chithra T R Page 2
DATA STRUCTURES
1. Malloc ()
2. Calloc () Memory Allocation Functions
3. Realloc ()
4. Free () De-Allocation Function
Malloc ()
It stands for memory allocation.
The function Malloc reserves a single block of memory that contains the number of bits Specified in
its parameter.
The general syntax of Malloc is-
Ptr=(datatype*) malloc (byte size);
Program To Show Usage of Malloc Function.
#include<stdio.h>
#include<conio.h>
void main ()
{
int n,i,*ptr,sum=0;
clrscr();
printf(“Enter number of elements:”);
Scanf(“%d”,&n);
Chithra T R Page 3
DATA STRUCTURES
ptr=(int*)malloc(n*size of (int));
if(ptr==null)
{
printf(“\n Error ! Memory not allocated”);
exit(0);
}
printf(“\n Total memory allocated for %d elements are %d bytes”,n,n*sizeof(int));
printf(“Enter elements of array:\n”);
for(i=0;i<n;++i)
{
scanf(“%d”,ptr+i);
sum+=*(ptr+i);
}
printf(“sum=%d”,sum);
free(ptr);
getch();
}
Output:
Enter number of elements: 5
Total memory allocation for 5 elements are 10 bytes
Enter elements of array:
10 20 30 40 50
Sum = 15
Calloc ()
It stands for continuous memory allocation.
It is primarily used to allocate the memory for arrays.
The only difference between malloc and calloc is that malloc allocates single
Block of memory whereas calloc allocates multiple blocks of memory.
Chithra T R Page 4
DATA STRUCTURES
#include<stdio.h>
#include<conio.h>
void main ()
{
int n,i,*ptr,sum=0;
clrscr();
printf(“Enter number of elements:”);
scanf(“%d”,&n);
ptr=(int*)calloc(n,size of (int));
if(ptr==null)
{
printf(“Error ! memory not allocated”);
exit(0);
}
printf(“\n Total memory allocated for %d elements is %d bytes”,n,n*size of(int));
printf(“\n Initial memory contents:”);
for(i=0 ;i<n;i++)
{
scanf(“%d”,ptr*i);
Sum+=*(ptr+i);
}
printf(“sum=%d”,sum);
free();
getch();
}
Output: Enter number of elements:4
Total memory allocated for 4 elements is 8 bytes.
Initial memory contents:0 0 0 0
Enter elements of array:10 20 30 40
Sum=10
Chithra T R Page 5
DATA STRUCTURES
Realloc ():
If a previously allocated memory insufficient or more than sufficient then this memory size can be
changed using this function.
The general syntax is:
ptr=realloc(ptr,new size);
Chithra T R Page 6
DATA STRUCTURES
Free ():
It stands for releasing the memory.
It is a memory De-allocation function.
When the memory allocations are allocated by malloc,calloc and realloc are no longer required then
they are freed using pre-defined function free.
The general syntax of free function is:
free(ptr);
Recursion
A Function that calls itself is known as recursion function
The process of calling a function by itself
Syntax: - returntype recursivefunction name(arguments/parameters);
{
Statements;
Recursive function name(arguments/parameters);
}
Ex: - int factorial(int x)
{
if(x = = 0)
{
return 1;
}
else
{
return(x*factorial(x-1));
}
}
Advantages of recursion: -
1. Recursion reduces length of the code
2. Recursion reduces the unnecessary calling of function
3. Recursion is very useful in solving data structure problems
4. We can solve problems by using simple way
5. In recursion code is simple and short than iterative code
Disadvantages of recursion: -
1. Recursion programs are generally slower than iterative
2. Recursive program requires more memory
3. Recursion uses more CPU processing time
4. Recursion programs are a bit difficult to understand and debug
Chithra T R Page 7
DATA STRUCTURES
Applications of recursion: -
1. To compute factorial of a number
2. Towers of Hanoi
3. All searching techniques uses recursive techniques(Linear search and Binary search)
4. Candy crush game uses recursion
5. We use recursion to determine whether a string is palindrome or not
6. Stack uses recursion technique
Chithra T R Page 8
DATA STRUCTURES
Chithra T R Page 9
DATA STRUCTURES
Chithra T R Page 10
DATA STRUCTURES
Types of recursions: -
There are 2 types of recursions, they are
1. Direct Recursion
2. Indirect Recursion
1. Direct Recursion: - If a function explicity call Same function by itself then that type of recursion
is called as direct recursion
2. Indirect recursion: - If a function calls another function which again calls the original function then
that type of recursion is called indirection recursion
int n,fact;
Chithra T R Page 12
DATA STRUCTURES
clrscr();
printf(“Enter the values \n”);
scanf(“%d”,&n);
fact=factorial(n);
printf(“the factorial of given number %d is %d”,n,fact);
getch();
}
int factorial(int n)
{
if(n==0)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}
Output: -
Enter the value:
7
The factorial of given number 7 is 5040
Chithra T R Page 13
DATA STRUCTURES
Chithra T R Page 14
DATA STRUCTURES
if(b==0) 9. Ex:
{ while(condition)
return a; {
} statements(s);
else statements(s);
{ }
return gcd(b,a%b);
}
}
Chithra T R Page 15
DATA STRUCTURES
GCD of 40 and 60 is 20
Chithra T R Page 16
DATA STRUCTURES
2. Binomial Coefficient [n c r]
The binomial coefficient of NCR is also called as pascals triangle. It begins from sum of 2 numbers to power
zero to given values at the runtime. A binomial is a polynomial expression that contains exactly 2 terms. A
binomial can be considered as a sum or difference between two or more monomials.
//Program to display Pascal’s triangle using Binomial function.
#include <stdio.h>
#include <conio.h>
void main ()
{
int rows, a, s, coef = 1, i, j;
clrscr();
printf("\n\t\t\t Pascal’s Triangle ");
printf("\n\nEnter The Number of Rows : ");
scanf("%d", &rows);
for (i = 0; i < rows; i++)
{
for (s = 0; s <= rows - i; s++)
printf(" ");
for (j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
{
coef = 1;
}
else
{
coef = coef * (i - j + 1) / j;
}
printf("%4d", coef);
}
printf("\n\n");
}
Chithra T R Page 17
DATA STRUCTURES
getch();
Chithra T R Page 18
DATA STRUCTURES
}
Output:
3. Towers of Hanoi
The Towers of Hanoi is a mathematical puzzle with set of rules its just replacing a ring into three poles
without placing the small ring on larger ring and even transferring the both ring at once. Each can be placed
only once in each step. At the output of this program, it gives the number of steps to be followed to complete
the puzzle.
(Or)
The Towers of Hanoi is a mathematical puzzle it consists of three rods and n number of disks of different
size which can slide onto any rod. The objective of the puzzle is to move the entire disk stack to another rod,
obeying the following simple rules.
POLES
1) Source pole (from pole)
2) Auxiliary pole
3) Destination pole (to pole)
There are three rules to solve problem of towers of Hanoi they are:
1. Only one disk can be move at a time
2. No disk may be placed on top of a smaller disk
3. We can use an auxiliary tower for temporary storage of disk
Chithra T R Page 20
DATA STRUCTURES
Output:
Enter the number of discs:
3
sequence of moves involved in the Towers of Hanoi problem:
move disc 1 from pole A to pole C
move disc 2 from pole A to pole B
move disc 1 from pole C to pole B
move disc 3 from pole A to pole C
move disc 1 from pole B to pole A
move disc 2 from pole B to pole C
move disc 1 from pole A to pole C
Chithra T R Page 21