Data Structure with C -Part-2 ADT,Array, Strucure and Union

33
1 Prof. A. Syed Mustafa (Ph.D)

description

third semester Engineering CSE/ISE

Transcript of Data Structure with C -Part-2 ADT,Array, Strucure and Union

Page 1: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

1 Prof. A. Syed Mustafa (Ph.D)

Page 2: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

2 Prof. A. Syed Mustafa (Ph.D)

Page 3: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Unit -2

Prof. A. Syed Mustafa (Ph.D)

3

ARRAYS AND STRUCTURES

Page 4: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

4

Topics :

Arrays

Dynamically Allocated Arrays

Structures and Unions

Polynomials

Sparse Matrices

Representation of Multidimensional Arrays

ARRAYS AND STRUCTURES

Page 5: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

5

ARRAYS AND STRUCTURES

Arrays

Page 6: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

6

ARRAYS AND STRUCTURES

Arrays

Page 7: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

7

ARRAYS AND STRUCTURES

Arrays

Page 8: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

8

ARRAYS AND STRUCTURES

Arrays

Page 9: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

9

ARRAYS AND STRUCTURES

Arrays

Page 10: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

10

ARRAYS AND STRUCTURES

Arrays

Page 11: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

11

ARRAYS AND STRUCTURES

Structures

Page 12: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

12

ARRAYS AND STRUCTURES

Structures

Page 13: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

13

ARRAYS AND STRUCTURES

Structures

Page 14: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

14

ARRAYS AND STRUCTURES

Structures

Page 15: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

15

ARRAYS AND STRUCTURES

Structures

Page 16: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

16

ARRAYS AND STRUCTURES

Structures

Page 17: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

17

ARRAYS AND STRUCTURES

Structures

Page 18: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

18

ARRAYS AND STRUCTURES

Structures

Page 19: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

19

ARRAYS AND STRUCTURES

Structures

• Structure variables may be initialized:struct {

char name[25];

int id, age;

char sex;

} s1 = { "Smith, John", 2813, 25, 'M'},

s2 = { "Smith, Mary", 4692, 23, 'F'};

Page 20: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

20

ARRAYS AND STRUCTURES

Structures• A structure can be given a name (a structure tag) for later use:struct student {

char name[25];

int id, age;

char sex;

};

struct student s1, s2;

The two declarations can be combined if desired, with the same effect:struct student {

char name[25];

int id, age;

char sex;

} s1, s2;

Page 21: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

21

ARRAYS AND STRUCTURES

Structures

• As an alternative, we can use typedef to name a structure:

typedef struct {

char name[25];

int id, age;

char sex;

} Student;

Student s1, s2;

Page 22: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

22

ARRAYS AND STRUCTURES

Structures

Page 23: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

23

ARRAYS AND STRUCTURES

Structures

• The members of a structure are accessed by writing first

the name of the structure, then a period, then the name of

the member:

struct student {

char name[25];

int id, age;

char sex;

} s;

strcpy(s.name, “Syed");

s.id = 18193;

s.age = 18;

s.sex = 'M';

Page 24: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

24

ARRAYS AND STRUCTURES

Unions

Page 25: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

25

ARRAYS AND STRUCTURES

Unions

Page 26: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

26

ARRAYS AND STRUCTURES

UnionsSimilar to struct, but only one field is active.

Example: Add fields for male and female.

typedef struct sex_type {

enum tag_field {female, male} sex;

union {

int children;

int beard;

} u;

};

typedef struct human_being {

char name[10];

int age;

float salary;

date dob;

sex_type sex_info;

}

Page 27: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

27

ARRAYS AND STRUCTURES

Unions

Page 28: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

28

ARRAYS AND STRUCTURES

Polynomials

Page 29: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

Prof. A. Syed Mustafa (Ph.D)

29

ARRAYS AND STRUCTURES

Polynomials

Page 30: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

30

Some of the problems require lot of zeros to be stored

as a part of solution.

Hence storing more number of zeros is a waste of

memory.

Matrices with relatively high proportion of zero entries

are called sparse matrices.

Matrix that contains more number of zero elements are

called sparse matrices.

Sparse matrix is used in almost all areas of the natural

sciences Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

Page 31: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

31

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

Page 32: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

32

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix

Page 33: Data Structure with C -Part-2 ADT,Array, Strucure and  Union

33

Prof. A. Syed Mustafa (Ph.D)

ARRAYS AND STRUCTURES

Sparse Matrix