Sunteți pe pagina 1din 15

DATA

STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com

(C) GOYANI MAHESH 1


PROGRAMMING
METHODOLOGY

(C) GOYANI MAHESH 2


TERMINOLOGY

 Data Structure is structural representation of logical relationship between


elements of Data.
 DS is used for utilizing the maximum efficiency of Memory

 Organized Data + Operation = Data Structure


 Algorithm + Data Structure = Program.

 ALGORITHM : It’s a step by step finite sequence of instruction to solve a well


defined 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.

(C) GOYANI MAHESH 3


STEP WISE REFINEMENT

Mathematical Formal Data


Model Language Structure

Informal Psedo– C / C++


Algorithm Language Program
Program

(C) GOYANI MAHESH 4


MODULAR PROGRAMMING

 Modular Programming is an act of writing programs as functions, that each


one perform a single well defined task, and which have minimum interaction
between 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)

(C) GOYANI MAHESH 5


PROGRAMMING APPROCH

Main

Function 1 Function 2 Function 3

Function a Function b Function c Function c Function b Function d

Functions called
Functions called By Function 1 Functions called By Function 3
By Function 1

(C) GOYANI MAHESH 6


STRUCTURED PROGRAMMING

 Sequence of sequentially executed statements

 Conditional Execution (if)

 Looping or iteration (for, while, do-while)

 Structured Subroutine (Function)

(C) GOYANI MAHESH 7


SOME D.S.
ARRAYS
 An array is a collection of homogeneous
data elements described by a single name.
 Insertion and Deletion can not be made.
i. 1-D Array

VECTOR
ii. 2-D Array
10 35 52 12 11
iii. M-D Array
12
iv. Sparse Arrays
 1-D ordered collection of number
13
i. Row Vector
14
ii. Column Vector
15

11 12 13 14 15

LISTS
 A list is an ordered set consisting of a varying
number of elements to which insertion and
deletion can be made

START

(C) GOYANI MAHESH 8


FILES & RECORDS

 File is typically large list that is stored in the


external memory of computer.

 A record is collection of information about some


particular 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)

(C) GOYANI MAHESH 9


CLASSIFICATION OF D.S.

Data
Structure

Primitive Non-Primitive
Data Structure Data Structure

Array list File


int char float pointer

Linear Non - Linear


list list

Stack Queue Graph Tree

(C) GOYANI MAHESH 10


MEMORY MANAGEMENT

 Dynamic or Run Time memory


allocation (Linked list)
 Static or Compile Time
memory allocation (Array)  malloc ()
 Allocates 1 block of memory
ptr=(Data_Type *)malloc(n * sizeof(Data_Type));
int a, array[10];
char str[80];
 callloc ()
 Allocates n block of memory
ptr=(Data_Type *)calloc(n , sizeof(Data_Type));

Dynamic memory  realloc ()


allocation in C++  Resize the original block of memory
ptr=realloc(ptr, NewSize);
int *var1 = new int;  free ()
float *var2 = new float;
 Free up the memory block
delete var1;
free (ptr);
delete var2;

(C) GOYANI MAHESH 11


MEMORY MANAGEMENT

 Free Storage List

 Garbage Collection

 Dangling Reference

 Reference Counter

(C) GOYANI MAHESH 12


BUDDY SYSTEM

 Keep 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 2 m

 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 2 k, 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<=2 i

 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 another
will 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 location
p 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.

(C) GOYANI MAHESH 13


0 0

Free Size 29
Free Size 29

512 512

Free Size 28
Free Size 28

768 768 Free Size 26


Free Size 28
832
B2 Allocated Size 26

896 896
B1 Allocated Size 27 B1 Allocated Size 27

9:0 9:0
8:512 8:512
7:768 6:768

(C) GOYANI MAHESH 14


0 0

Free Size 29 Free Size 29

512 512
Free Size 27

640 Free Size 28


B5 Allocated Size 26

704
B4 Allocated Size 26

768 B3 Allocated Size 26 768 B3 Allocated Size 26

832 832
B2 Allocated Size 26 B2 Allocated Size 26

896 896
B1 Allocated Size 27
B1 Allocated Size 27

9:0
9:0
8:512
8:512
7:768
6:768
(C) GOYANI MAHESH 15

S-ar putea să vă placă și