Upload
akif-vohra
View
222
Download
0
Embed Size (px)
Citation preview
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 1/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 11
DATASTRUCTURES
MAHESH GOYANI
M AHATMAG ANDHI INSTITUE OF TECHNICALEDUCATION & RESEARCH CENTER
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 2/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 22
PROGRAMMING
METHODOLOGY
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 3/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 33
Data Structure is structural representation of logical relationship betweenelements of Data.
DS is used for utilizing the maximum efficiency of Memory
Organized Data + Operation = Data Structure
Algorithm + Data Structure = Program.
ALGORITHM : Its a step by step finite sequence of instruction to solve a welldefined computational problem.
Optimization of program is directly concerned with the algorithm design.
Representation of DS in memory of a computer is called storage structure.
A Storage structure stored in auxiliary memory is known as File Structure.
TERMINOLOGY
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 4/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 44
Informal Algorithm
MathematicalModel
PsedoLanguage
Program
FormalLanguage
C / C++
Program
Data
Structure
STEP WISE REFINEMENT
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 5/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 55
Modular Programming is an act of writing programs as functions, that eachone perform a single well defined task, and which have minimum interactionbetween them.
High cohesion : Related with specific task only.
Low Coupling : Independent from each other.
Two Methods:
(i). TOP DOWN Methodology (eg. C, C++ Programming)
(ii). BOTTOM UP Methodology (eg. VB Project implementation)
MODULAR PROGRAMMING
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 6/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 66
Main
Function 1 Function 2 Function 3
Function a Function b Function c Function c Function b Function d
Functions called By Function 1 Functions called By Function 3Functions called
By Function 1
PROGRAMMING APPROCH
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 7/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 77
Sequence of sequentially executed statements
Conditional Execution (if)
Looping or iteration (for, while, do-while)
Structured Subroutine (Function)
STRUCTURED PROGRAMMING
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 8/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 88
ARRAYS
An array is a collection of homogeneousdata elements described by a single name.
Insertion and Deletion can not be made.
i. 1-D Array
ii. 2-D Array
iii. M-D Array
iv. Sparse Arrays
VECTOR 1-D ordered collection of number
i. Row Vector
ii. Column Vector
LISTS A list is an ordered set consisting of a varying A list is an ordered set consisting of a varyingnumber of elements to which insertion andnumber of elements to which insertion anddeletion can be madedeletion can be made
12523510 11
12
13
14
15
START
11 12 13 14 15
SOME D.S.
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 9/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 99
FILES & RECORDS
File is typically large list that is stored in theexternal memory of computer.
A record is collection of information about someparticular entity.
Record may be collection of heterogeneous data.
File is collection of such a records.
CHARECTERISTICS
OF STRING
Fixed length string
Variable length string
Linear list (Linked List)
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 10/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1010
Data
Structure
PrimitiveData Structure
Non-PrimitiveData Structure
int char float pointer
Linear
list
Graph Tree
list Array File
Non - Linear
list
Stack Queue
CLASSIFICATION OF D.S.
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 11/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1111
malloc ()
Allocates 1 block of memory
ptr=(Data_Type *)malloc(n * sizeof(Data_Type));
callloc ()
Allocates n block of memory
ptr=(Data_Type *)calloc(n , sizeof(Data_Type));
realloc ()
Resize the original block of memory
ptr=realloc(ptr, NewSize);
free ()
Free up the memory block
free (ptr);
Dynamic or Run Time memoryallocation (Linked list)
Static or Compile Timememory allocation (Array)
int a, array[10];
char str[80];
Dynamic memoryallocation in C++
int *var1 = new int;
float *var2 = new float;
delete var1;
delete var2;
MEMORY MANAGEMENT
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 12/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1212
Free Storage List
Garbage Collection
Dangling Reference
Reference Counter
MEMORY MANAGEMENT
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 13/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1313
BUDDY SYSTEM
K eep separate free list for block of different sizes.
e.g. if memory contain 1024 word than it might be divided in 1 block of 256 words, 2 blocks of 128 words,4 blocks of 64 words and 8 blocks of 32 words
This scheme has several drawback, Like fragmentation waste space.
Buddy system contains several free lists consisting of various sized block.
Very efficient in binary computer, where shift operation is performed well.
initially entire memory is considered as free block of size 2m
Block of size i is called i-Block & free list containing i-Block is called i-List.
Block must be allocated only in size of 2k, 0<k<m.
if request for block size n is made, an i-block is reserved where I is the smallest integer such that n<=2i
if i-list is not free then bring block from (i+1)-list and divide in to two i-block, one will be used and anotherwill be added into i-list. Do Recursively.
if an (i+1) block is divided in two i-block b1 & b2, b1 and b2 are buddies of each other. Block at locationp is called i-Buddy of p. buddies can be more than one but i-buddy can not.
if an i-block is freed and its i-buddy is already freed than two buddies are combined into (i+1) block.
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 14/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1414
768
Free Size 29
Free Size 28
Free Size 28
B1 Allocated Size 27
0
512
896
9:0
8:512
7:768
Free Size 29
Free Size 28
Free Size 26
B1 Allocated Size 27
0
512
768
896
9:0
8:512
6:768
B2 Allocated Size 2
6832
8/8/2019 00. Basics of DS
http://slidepdf.com/reader/full/00-basics-of-ds 15/15
(C) GOYANI MAHESH(C) GOYANI MAHESH 1515
Free Size 29
Free Size 28
B3 Allocated Size 26
B1 Allocated Size 27
0
512
768
896
9:0
8:512
7:768
6:768
B2 Allocated Size 2
6832
Free Size 29
Free Size 27
B1 Allocated Size 27
0
512
768
896
9:0
8:512
B2 Allocated Size 2
6 832
B5 Allocated Size 26
B4 Allocated Size 26
B3 Allocated Size 26
640
704