SlideShare a Scribd company logo
1 of 42
COMPUTER SCIENCE II
[DATA STRUCTURES]
UNIT – I
INTRODUCTION
PART I
DATA STRUCTURES
• Organizing or structuring data while storing in a computer
• Mathematical or logical model (we will construct these models in C language)of a particular
organization of data items.
•Ex. Can store a list of items having the same data-type using the array data structure
Data Structure mainly specifies the following
four things:
· Organization of Data ( amount of memory required to store)
· Accessing methods (amount of time required to process)
· Degree of associativity (how the data items are related to each other)
· Processing alternatives for information (only then can we know the best)
Basic Terminology
Data: Data can be defined as an elementary value or the collection of values, for example, student's
name and its id are the data about the student.
Group Items: Data items which have subordinate data items are called Group item, for example,
name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for example, if we talk about
the student entity, then its name, address, course and marks can be grouped together to form the
record for the student.
File: A File is a collection of various records of one type of entity, for example, if there are 60 student
in the class, then there will be 20 records in the related file where each record contains the data
about each student.
Attribute and Entity: An entity represents the class of certain objects. it contains various attributes.
Each attribute represents the particular property of that entity.
Field: Field is a single elementary unit of information representing the attribute of an entity.
Need of Data Structures
Processor speed: To handle very large amount of data, high speed processing is required, but as
the data is growing day by day to the billions of files per entity, processor may fail to deal with
that much amount of data.
Data Search: Consider an inventory size of 106 items in a store, If our application needs to
search for a particular item, it needs to traverse 106 items every time, results in slowing down
the search process.
Multiple requests: If thousands of users are searching the data simultaneously on a web server,
then there are the chances that a very large server can be failed during that process
In order to solve the above problems, data structures are used. Data is organized to form a data
structure in such a way that all items are not required to be searched and required data can be
searched instantly.
Advantage and Disadvantage
Classification of Data Structure
· Primitive data structures
· Non-primitive data structures (linear and non-linear)
· Homogeneous and non-homogeneous data structures
· Static and dynamic data structures
1. Primitive data structures :
 Basic data structures and are directly operated upon by the machine instructions.
 Classified into - integers, floating point numbers, characters, string constants, pointers etc.
Like: int a =10;
The corresponding machine level code will be like:
store the int value in so and so location.
But if I write: int arr[10]=20;
The machine instruction doesn’t know array index 10! So, intermediate steps will be there to
convert this particular instruction to machine level.
2. Non-primitive data structures:
It is advanced data structure emphasizing on structuring of a group of data items.
They cannot be directly operated upon by the machine instructions.
Example: Array, list, files, linked list, trees and graphs fall in this category.
Linear and non-linear data structures :
In a linear data structure, the data items are arranged in a linear sequence.
For example: array.
In a non-linear data structure, the data items are not in sequence.
For Example: trees and graphs.
Cont’
Linear and non-linear data structures :
In a linear data structure, the data items are arranged in a linear sequence.
For example: array.
In a non-linear data structure, the data items are not in sequence.
For Example: trees and graphs.
3. Homogeneous and non-
homogeneous data structures:
In homogeneous data structure, all the elements are of same type.
For Example: arrays.
In non-homogeneous data structure, the elements may or may not be of the same type.
For Example: Records.
4. Static and dynamic data structures:
In Static data structure the size of the structure is fixed.
The content of the data structure can be modified but without changing the memory space
allocated to it.
Example: Array
In Dynamic data structure the size of the structure is not fixed and can be modified during the
operations performed on it.
 Dynamic data structures are designed to facilitate change of data structures in the run time.
Example: Linked List
Operations on Data Structures
1) Create:- The create operation results in reserving memory for program elements. This can be done by declaration statement.
Creation of data structure may take place either during compile-time or run-time.
 Dynamic Memory Allocation in C can be done using malloc(), calloc(), free() and realloc()
2) Destroy:- Destroy operation destroys memory space allocated for specified data structure. free() function of C language is used to
destroy data structure.
3) Selection:- Selection operation deals with accessing a particular data within a data structure.
4) Updation:- It updates or modifies the data in the data structure.
5) Searching:- It finds the presence of desired data item in the list of data items, it may also find the locations of all elements that satisfy
certain conditions.
6) Sorting:- Sorting is a process of arranging all data items in a data structure in a particular order, say for example, either in ascending
order or in descending order.
7) Merging:- Merging is a process of combining the data items of two different non-primitive data structure into a single one.
8) Splitting:- Splitting is a process of partitioning single non-primitive data structure to multiple ones.
9) Traversal:- Traversal is a process of visiting each and every node of a non-primitive data structure in systematic manner.
UNIT – I
Dynamic Memory Allocation
Part II
Memory allocation
Static memory allocation
In Static Memory Allocation the memory for
your data is allocated when the program starts.
The size is fixed when the program is created.
This memory allocation is fixed and cannot be
changed, i.e. increased or decreased after
allocation. So, exact memory requirements
must be known in advance.
Key features:
· Variables get allocated permanently
· Allocation is done before program
execution
· It uses the data structure called stack for
implementing static allocation
· There is no memory reusability
Example:
All the variables in the program below are statically
allocated.
void play
{
int a;
}
int main()
{
int b;
int c[10];
return 1;
}
Static memory allocation
the address can be obtained by using ‘&’ operator and can be assigned to a pointer.
The memory is allocated during compile time.
It uses stack for maintaining the static allocation of memory.
In this allocation, once the memory is allocated, the memory size cannot change.
It is less efficient.
Dynamic memory allocation
 C is a structured language, it has some fixed rules for programming. One of it includes
changing the size of an array. An array is collection of items stored at continuous memory
locations.
Procedure refer as dynamic allocation
a procedure in which the size of a data structure is changed during the runtime.
Dynamic allocation is useful when data structures need to be created whose size is not known until run
time
Dynamic memory
Allocating memory dynamically. While programming, if you are aware of the size of an array,
then it is easy and you can define it as an array.
Example Code
Output. ...
Resizing Memory Locations
Library functions
C provides some functions to achieve these tasks. There are 4 library functions provided by C
defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming.
They are:
1. malloc()
2. calloc()
3. free()
4. realloc()
malloc():
“malloc” or “memory allocation” method in C is used to dynamically allocate a single large block
of memory with the specified size. It returns a pointer of type void which can be cast into a
pointer of any form.
Syntax:
ptr = (cast-type*) malloc(byte-size)
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the
pointer ptr holds the address of the first byte in the allocated memory.
calloc
“calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified
number of blocks of memory of the specified type. It also returns a pointer of type void which
can be cast into a pointer of any form. It initializes each block with a default value ‘0’.
Syntax:
ptr = (cast-type*)calloc(n, element-size);
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the
float.
realloc()
“realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of
a previously allocated memory.
In other words, if the memory previously allocated with the help of malloc or calloc is
insufficient, realloc can be used to dynamically re-allocate memory.
Syntax:
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'
Free
“free” method in C is used to dynamically de-allocate the memory.
The memory allocated using functions malloc() and calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps
to reduce wastage of memory by freeing it.
Syntax:
free(ptr);
UNIT – I
Recursion
Part III
Recursion
Recursion is the process of repeating items in a self-similar way.
In programming languages, if a program allows you to call a function inside the same function,
then it is called a recursive call of the function.
Use - to solve many mathematical problems, such as calculating the factorial of a number,
generating Fibonacci series, etc.
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
Types
Recursion are mainly of two types depending on whether a function calls itself from within
itself or more than one function call one another mutually.
Types:
Direct recursion
Indirect recursion
Direct Recursion:
◦ Tail Recursion
◦ If a recursive function calling itself and that recursive call is the last statement in the function
◦ Head Recursion
◦ If a recursive function calling itself and that recursive call is the first statement in the function
◦ Tree Recursion
◦ First understand linear recursion. If a recursive function calling itself for one time then it’s known as Linear Recursion
◦ Otherwise if a recursive function calling itself for more than one time – tree recursion
◦ Nested Recursion
◦ a recursive function will pass the parameter as a recursive call -“recursion inside recursion
Cont’
Indirect recursion
there may be more than one function and they are calling one another in a circular manner.
Example
Binomial coefficient
Fibanacci series
GCD
Tower of Hanoi
Binomial coefficient
Algorithm
Step1: Start
Step2: Accept two numbers n and r
Step3: If n<r
Print “Invalid input”
Else
Print result
End if
Step4: Stop
Algorithm for BC(x)
Step1: If x=0
return 1
Else
return BC(n-1,r-1) + BC(n-1,r)
End if
Binomial Coefficient
//Program to find Binomial Coefficient
#include<stdio.h>
int main()
{
int n,r;
printf("Enter n value : ");
scanf("%d",&n);
printf("Enter r value : ");
scanf("%d",&r);
if(n<r)
{
printf("n Invalid input n value must be greater than r value");
}
else
{
printf("Binomial coefficientn",BC(n,r));
printf("%dn",BC(n,r));
return 0;
}
}
int BC(int n, int r)
{
if(r==0 || r==n)
return 1;
return BC(n-1,r-1) + BC(n-1,r);
}
Output:
Enter n value : 3
Enter r value :6
Invalid input n value must be greater than r value
Enter n value : 4
Enter r value :2
Binomial coefficient
6
GCD
Program to find GCD of two numbers using recursion
Algorithm
Step1: Start
Step2: Accept two numbers m and n
Step3: Call the function gcd(m,n)
Step4: Print the result
Step5: Stop
Algorithm for gcd(a,b)
Step1: If a==b
return (a)
else
if(a>b)
return(gcd(a-b,b)
else
return (gcd(a,b-a)
End if
#include<stdio.h>
int gcd(int,int);
void main()
{
int m,n;
printf("nGreatest Common Divisorn");
printf("Enter the value for m : ");
scanf("%d",&m);
printf("Enter the value for n : ");
scanf("%d",&n);
printf("The Greatest Common Divisor of %d and %d = %d",m,n,gcd(m,n));
}
int gcd(int a,int b)
{
if(a==b)
{
return(a);
}
else
{
if(a>b)
return(gcd(a-b,b));
else
return(gcd(a,b-a));
}
Fibanacci series
#include<stdio.h>
#include<conio.h>
int recursivefibonacci(int);
int main(){
int m, x;
printf("Please enter the total number of values you want in the
Fibonacci series : n");
scanf("%d",&m);
printf("The Fibonacci series of these numbers would be equal to : n");
for(x=0;x<m;x++) {
printf("%d",recursivefibonacci(x));
}
getch();
}
int recursivefibonacci(int x){
if(x==0) return 0;
else if(x==1) return 1;
else return (recursivefibonacci(x-1) + recursivefibonacci(x-
2));
}
Please enter the total number of values you want in the
Fibonacci series :
4
The Fibonacci series of these numbers would be equal to :
0112
Output:
Please enter the total number of values
you want in the Fibonacci series :
4
The Fibonacci series of these numbers
would be equal to :
0112
Tower of hanoi
Tower of Hanoi, is a mathematical puzzle which
consists of three towers (pegs) and more than one
rings is as depicted − These rings are of different
sizes and stacked upon in an ascending order, i.e. the
smaller one sits over the larger one.
The objective of the puzzle is to move the entire
stack to the last rod, obeying the following rules:
Only one disk may 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 or
on an empty rod.
No disk may be placed on top of a disk that is smaller
than it.
With 3 disks, the puzzle can be solved in 7 moves.
The minimal number of moves required to solve a
Tower of Hanoi puzzle is 2n − 1, where n is the
number of disks.
Exercise 1
Exercise 2
#include<stdio.h>
void TOH(int n,char x,char y,char z)
{
if(n>0)
{
TOH(n-1,x,z,y);
printf("n%c to %c",x,y);
TOH(n-1,z,y,x);
}
}
int main()
{
int n;
scanf("%d",&n);
TOH(n,'A','B','C');
}
Program
#include<stdio.h>
#include<math.h>
void main()
{
int n;
char A,B,C;
void towers(int,char,char,char);
// clrscr();
printf("nTowers of Hanoin");
printf("nEnter the number of disks : ");
scanf("%d",&n);
printf("The number of
moves=%0.fn",(pow(2,n)-1));
printf("nTowers of Hanoi simulation for %d
disksn",n);
towers(n,'A','C','B');
getch();
}
void towers(int n,char source,char dest,char aux)
{
if(n==1)
{
printf("nMove disk %d from %c to
%c",n,source,dest);
return;
}
towers(n-1,source,aux,dest);
printf("nMove disk %d from %c to
%c",n,source,dest);
towers(n-1,aux,dest,source);
}
output
Towers of Hanoi
Enter the number of disks : 3
The number of moves=7
Towers of Hanoi simulation for 3 disks
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Algorithm
Step1: Start
Step2: Input the number of disks
Step3: Print the number of movements 2disk-1
Step4: Call the function towers(n,’A’,’C’,’B’)
Step5:Stop
Algorithm for towers(n,source,dest,aux)
n is the total no. of disks and A, B, and C are the source, destination and intermediate pegs respectively.
Step1: If n=1
Print the movement source to destination
Else
Step a: Call the function towers(n-1,source,aux,dest)
Step b: Print the movement source to destination
Step c: Call the function towers(n-1,aux,dest,source)
End if
THANK YOU..
thenmithu@gmail.com

More Related Content

Similar to DS-UNIT 1 FINAL (2).pptx

Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)Madishetty Prathibha
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxGIRISHKUMARBC1
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structurechouguleamruta24
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptxMumtaz
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptxOnkarModhave
 
Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)MUHAMMAD AAMIR
 
Lecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionLecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionAbirami A
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfAxmedcarb
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
DataStructurePpt.pptx
DataStructurePpt.pptxDataStructurePpt.pptx
DataStructurePpt.pptxDCABCA
 
MD AZAM CA-1-1.pptx
MD AZAM CA-1-1.pptxMD AZAM CA-1-1.pptx
MD AZAM CA-1-1.pptxMyMovies15
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
Unit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptxUnit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptxajajkhan16
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechniclavparmar007
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.pptclassall
 

Similar to DS-UNIT 1 FINAL (2).pptx (20)

UNIT II.docx
UNIT II.docxUNIT II.docx
UNIT II.docx
 
Datastructures Notes
Datastructures NotesDatastructures Notes
Datastructures Notes
 
Introduction to data structures (ss)
Introduction to data structures (ss)Introduction to data structures (ss)
Introduction to data structures (ss)
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
 
Data Structure & Algorithm.pptx
Data Structure & Algorithm.pptxData Structure & Algorithm.pptx
Data Structure & Algorithm.pptx
 
UNIT I - Data Structures.pdf
UNIT I - Data Structures.pdfUNIT I - Data Structures.pdf
UNIT I - Data Structures.pdf
 
Introduction to DS.pptx
Introduction to DS.pptxIntroduction to DS.pptx
Introduction to DS.pptx
 
Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)Chapter 1( intro &amp; overview)
Chapter 1( intro &amp; overview)
 
Lecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionLecture 2 Data Structure Introduction
Lecture 2 Data Structure Introduction
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdf
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
DataStructurePpt.pptx
DataStructurePpt.pptxDataStructurePpt.pptx
DataStructurePpt.pptx
 
Lect 1-2 Zaheer Abbas
Lect 1-2 Zaheer AbbasLect 1-2 Zaheer Abbas
Lect 1-2 Zaheer Abbas
 
MD AZAM CA-1-1.pptx
MD AZAM CA-1-1.pptxMD AZAM CA-1-1.pptx
MD AZAM CA-1-1.pptx
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
Unit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptxUnit-1 DataStructure Intro.pptx
Unit-1 DataStructure Intro.pptx
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
 
Lect 1-2
Lect 1-2Lect 1-2
Lect 1-2
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 

More from prakashvs7

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptxprakashvs7
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxprakashvs7
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxprakashvs7
 
unit 5_Real time Data Analysis vsp.pptx
unit 5_Real time Data Analysis  vsp.pptxunit 5_Real time Data Analysis  vsp.pptx
unit 5_Real time Data Analysis vsp.pptxprakashvs7
 
final Unit 1-1.pdf
final Unit 1-1.pdffinal Unit 1-1.pdf
final Unit 1-1.pdfprakashvs7
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxprakashvs7
 
AI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptxAI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptxprakashvs7
 
AI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxAI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxprakashvs7
 
AI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxAI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxprakashvs7
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxprakashvs7
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxprakashvs7
 

More from prakashvs7 (15)

Python lambda.pptx
Python lambda.pptxPython lambda.pptx
Python lambda.pptx
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptx
 
unit 5_Real time Data Analysis vsp.pptx
unit 5_Real time Data Analysis  vsp.pptxunit 5_Real time Data Analysis  vsp.pptx
unit 5_Real time Data Analysis vsp.pptx
 
unit 4-1.pptx
unit 4-1.pptxunit 4-1.pptx
unit 4-1.pptx
 
unit 3.ppt
unit 3.pptunit 3.ppt
unit 3.ppt
 
final Unit 1-1.pdf
final Unit 1-1.pdffinal Unit 1-1.pdf
final Unit 1-1.pdf
 
PCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docxPCCF-UNIT 2-1 new.docx
PCCF-UNIT 2-1 new.docx
 
AI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptxAI UNIT-4 Final (2).pptx
AI UNIT-4 Final (2).pptx
 
AI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptxAI UNIT-3 FINAL (1).pptx
AI UNIT-3 FINAL (1).pptx
 
AI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptxAI-UNIT 1 FINAL PPT (2).pptx
AI-UNIT 1 FINAL PPT (2).pptx
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Php unit i
Php unit i Php unit i
Php unit i
 
The process
The processThe process
The process
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 

DS-UNIT 1 FINAL (2).pptx

  • 1. COMPUTER SCIENCE II [DATA STRUCTURES] UNIT – I INTRODUCTION PART I
  • 2. DATA STRUCTURES • Organizing or structuring data while storing in a computer • Mathematical or logical model (we will construct these models in C language)of a particular organization of data items. •Ex. Can store a list of items having the same data-type using the array data structure
  • 3. Data Structure mainly specifies the following four things: · Organization of Data ( amount of memory required to store) · Accessing methods (amount of time required to process) · Degree of associativity (how the data items are related to each other) · Processing alternatives for information (only then can we know the best)
  • 4. Basic Terminology Data: Data can be defined as an elementary value or the collection of values, for example, student's name and its id are the data about the student. Group Items: Data items which have subordinate data items are called Group item, for example, name of a student can have first name and the last name. Record: Record can be defined as the collection of various data items, for example, if we talk about the student entity, then its name, address, course and marks can be grouped together to form the record for the student. File: A File is a collection of various records of one type of entity, for example, if there are 60 student in the class, then there will be 20 records in the related file where each record contains the data about each student. Attribute and Entity: An entity represents the class of certain objects. it contains various attributes. Each attribute represents the particular property of that entity. Field: Field is a single elementary unit of information representing the attribute of an entity.
  • 5. Need of Data Structures Processor speed: To handle very large amount of data, high speed processing is required, but as the data is growing day by day to the billions of files per entity, processor may fail to deal with that much amount of data. Data Search: Consider an inventory size of 106 items in a store, If our application needs to search for a particular item, it needs to traverse 106 items every time, results in slowing down the search process. Multiple requests: If thousands of users are searching the data simultaneously on a web server, then there are the chances that a very large server can be failed during that process In order to solve the above problems, data structures are used. Data is organized to form a data structure in such a way that all items are not required to be searched and required data can be searched instantly.
  • 6.
  • 8. Classification of Data Structure · Primitive data structures · Non-primitive data structures (linear and non-linear) · Homogeneous and non-homogeneous data structures · Static and dynamic data structures
  • 9.
  • 10. 1. Primitive data structures :  Basic data structures and are directly operated upon by the machine instructions.  Classified into - integers, floating point numbers, characters, string constants, pointers etc. Like: int a =10; The corresponding machine level code will be like: store the int value in so and so location. But if I write: int arr[10]=20; The machine instruction doesn’t know array index 10! So, intermediate steps will be there to convert this particular instruction to machine level.
  • 11. 2. Non-primitive data structures: It is advanced data structure emphasizing on structuring of a group of data items. They cannot be directly operated upon by the machine instructions. Example: Array, list, files, linked list, trees and graphs fall in this category. Linear and non-linear data structures : In a linear data structure, the data items are arranged in a linear sequence. For example: array. In a non-linear data structure, the data items are not in sequence. For Example: trees and graphs.
  • 12. Cont’ Linear and non-linear data structures : In a linear data structure, the data items are arranged in a linear sequence. For example: array. In a non-linear data structure, the data items are not in sequence. For Example: trees and graphs.
  • 13. 3. Homogeneous and non- homogeneous data structures: In homogeneous data structure, all the elements are of same type. For Example: arrays. In non-homogeneous data structure, the elements may or may not be of the same type. For Example: Records.
  • 14. 4. Static and dynamic data structures: In Static data structure the size of the structure is fixed. The content of the data structure can be modified but without changing the memory space allocated to it. Example: Array In Dynamic data structure the size of the structure is not fixed and can be modified during the operations performed on it.  Dynamic data structures are designed to facilitate change of data structures in the run time. Example: Linked List
  • 15. Operations on Data Structures 1) Create:- The create operation results in reserving memory for program elements. This can be done by declaration statement. Creation of data structure may take place either during compile-time or run-time.  Dynamic Memory Allocation in C can be done using malloc(), calloc(), free() and realloc() 2) Destroy:- Destroy operation destroys memory space allocated for specified data structure. free() function of C language is used to destroy data structure. 3) Selection:- Selection operation deals with accessing a particular data within a data structure. 4) Updation:- It updates or modifies the data in the data structure. 5) Searching:- It finds the presence of desired data item in the list of data items, it may also find the locations of all elements that satisfy certain conditions. 6) Sorting:- Sorting is a process of arranging all data items in a data structure in a particular order, say for example, either in ascending order or in descending order. 7) Merging:- Merging is a process of combining the data items of two different non-primitive data structure into a single one. 8) Splitting:- Splitting is a process of partitioning single non-primitive data structure to multiple ones. 9) Traversal:- Traversal is a process of visiting each and every node of a non-primitive data structure in systematic manner.
  • 16. UNIT – I Dynamic Memory Allocation Part II
  • 18. Static memory allocation In Static Memory Allocation the memory for your data is allocated when the program starts. The size is fixed when the program is created. This memory allocation is fixed and cannot be changed, i.e. increased or decreased after allocation. So, exact memory requirements must be known in advance. Key features: · Variables get allocated permanently · Allocation is done before program execution · It uses the data structure called stack for implementing static allocation · There is no memory reusability Example: All the variables in the program below are statically allocated. void play { int a; } int main() { int b; int c[10]; return 1; }
  • 19. Static memory allocation the address can be obtained by using ‘&’ operator and can be assigned to a pointer. The memory is allocated during compile time. It uses stack for maintaining the static allocation of memory. In this allocation, once the memory is allocated, the memory size cannot change. It is less efficient.
  • 20. Dynamic memory allocation  C is a structured language, it has some fixed rules for programming. One of it includes changing the size of an array. An array is collection of items stored at continuous memory locations. Procedure refer as dynamic allocation a procedure in which the size of a data structure is changed during the runtime. Dynamic allocation is useful when data structures need to be created whose size is not known until run time
  • 21. Dynamic memory Allocating memory dynamically. While programming, if you are aware of the size of an array, then it is easy and you can define it as an array. Example Code Output. ... Resizing Memory Locations
  • 22. Library functions C provides some functions to achieve these tasks. There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming. They are: 1. malloc() 2. calloc() 3. free() 4. realloc()
  • 23. malloc(): “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. Syntax: ptr = (cast-type*) malloc(byte-size) For Example: ptr = (int*) malloc(100 * sizeof(int)); Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.
  • 24. calloc “calloc” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. It also returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with a default value ‘0’. Syntax: ptr = (cast-type*)calloc(n, element-size); For Example: ptr = (float*) calloc(25, sizeof(float)); This statement allocates contiguous space in memory for 25 elements each with the size of the float.
  • 25. realloc() “realloc” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory. Syntax: ptr = realloc(ptr, newSize); where ptr is reallocated with new size 'newSize'
  • 26. Free “free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc() is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it. Syntax: free(ptr);
  • 28. Recursion Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. Use - to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc. void recursion() { recursion(); /* function calls itself */ } int main() { recursion(); }
  • 29. Types Recursion are mainly of two types depending on whether a function calls itself from within itself or more than one function call one another mutually. Types: Direct recursion Indirect recursion Direct Recursion: ◦ Tail Recursion ◦ If a recursive function calling itself and that recursive call is the last statement in the function ◦ Head Recursion ◦ If a recursive function calling itself and that recursive call is the first statement in the function ◦ Tree Recursion ◦ First understand linear recursion. If a recursive function calling itself for one time then it’s known as Linear Recursion ◦ Otherwise if a recursive function calling itself for more than one time – tree recursion ◦ Nested Recursion ◦ a recursive function will pass the parameter as a recursive call -“recursion inside recursion
  • 30. Cont’ Indirect recursion there may be more than one function and they are calling one another in a circular manner.
  • 32. Binomial coefficient Algorithm Step1: Start Step2: Accept two numbers n and r Step3: If n<r Print “Invalid input” Else Print result End if Step4: Stop Algorithm for BC(x) Step1: If x=0 return 1 Else return BC(n-1,r-1) + BC(n-1,r) End if
  • 33. Binomial Coefficient //Program to find Binomial Coefficient #include<stdio.h> int main() { int n,r; printf("Enter n value : "); scanf("%d",&n); printf("Enter r value : "); scanf("%d",&r); if(n<r) { printf("n Invalid input n value must be greater than r value"); } else { printf("Binomial coefficientn",BC(n,r)); printf("%dn",BC(n,r)); return 0; } } int BC(int n, int r) { if(r==0 || r==n) return 1; return BC(n-1,r-1) + BC(n-1,r); } Output: Enter n value : 3 Enter r value :6 Invalid input n value must be greater than r value Enter n value : 4 Enter r value :2 Binomial coefficient 6
  • 34. GCD Program to find GCD of two numbers using recursion Algorithm Step1: Start Step2: Accept two numbers m and n Step3: Call the function gcd(m,n) Step4: Print the result Step5: Stop Algorithm for gcd(a,b) Step1: If a==b return (a) else if(a>b) return(gcd(a-b,b) else return (gcd(a,b-a) End if #include<stdio.h> int gcd(int,int); void main() { int m,n; printf("nGreatest Common Divisorn"); printf("Enter the value for m : "); scanf("%d",&m); printf("Enter the value for n : "); scanf("%d",&n); printf("The Greatest Common Divisor of %d and %d = %d",m,n,gcd(m,n)); } int gcd(int a,int b) { if(a==b) { return(a); } else { if(a>b) return(gcd(a-b,b)); else return(gcd(a,b-a)); }
  • 35. Fibanacci series #include<stdio.h> #include<conio.h> int recursivefibonacci(int); int main(){ int m, x; printf("Please enter the total number of values you want in the Fibonacci series : n"); scanf("%d",&m); printf("The Fibonacci series of these numbers would be equal to : n"); for(x=0;x<m;x++) { printf("%d",recursivefibonacci(x)); } getch(); } int recursivefibonacci(int x){ if(x==0) return 0; else if(x==1) return 1; else return (recursivefibonacci(x-1) + recursivefibonacci(x- 2)); } Please enter the total number of values you want in the Fibonacci series : 4 The Fibonacci series of these numbers would be equal to : 0112 Output: Please enter the total number of values you want in the Fibonacci series : 4 The Fibonacci series of these numbers would be equal to : 0112
  • 36. Tower of hanoi Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted − These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. The objective of the puzzle is to move the entire stack to the last rod, obeying the following rules: Only one disk may 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 or on an empty rod. No disk may be placed on top of a disk that is smaller than it. With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2n − 1, where n is the number of disks.
  • 39. #include<stdio.h> void TOH(int n,char x,char y,char z) { if(n>0) { TOH(n-1,x,z,y); printf("n%c to %c",x,y); TOH(n-1,z,y,x); } } int main() { int n; scanf("%d",&n); TOH(n,'A','B','C'); }
  • 40. Program #include<stdio.h> #include<math.h> void main() { int n; char A,B,C; void towers(int,char,char,char); // clrscr(); printf("nTowers of Hanoin"); printf("nEnter the number of disks : "); scanf("%d",&n); printf("The number of moves=%0.fn",(pow(2,n)-1)); printf("nTowers of Hanoi simulation for %d disksn",n); towers(n,'A','C','B'); getch(); } void towers(int n,char source,char dest,char aux) { if(n==1) { printf("nMove disk %d from %c to %c",n,source,dest); return; } towers(n-1,source,aux,dest); printf("nMove disk %d from %c to %c",n,source,dest); towers(n-1,aux,dest,source); }
  • 41. output Towers of Hanoi Enter the number of disks : 3 The number of moves=7 Towers of Hanoi simulation for 3 disks Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C Algorithm Step1: Start Step2: Input the number of disks Step3: Print the number of movements 2disk-1 Step4: Call the function towers(n,’A’,’C’,’B’) Step5:Stop Algorithm for towers(n,source,dest,aux) n is the total no. of disks and A, B, and C are the source, destination and intermediate pegs respectively. Step1: If n=1 Print the movement source to destination Else Step a: Call the function towers(n-1,source,aux,dest) Step b: Print the movement source to destination Step c: Call the function towers(n-1,aux,dest,source) End if