14/3/02 sudeshna sarkar, cse, iit kharagpur1 structures, adt lecture 25 14/3/2002
TRANSCRIPT
![Page 1: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/1.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
1
Structures, ADT
Lecture 2514/3/2002
![Page 2: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/2.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
2
Announcements Lab Test 2 on the week of 18th –
22nd March Syllabus for lab test : arrays,
structures, data types, …..
![Page 3: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/3.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
3
The List ADT A list : <A1, A2, ... , AN> of size N. Special list of size 0 : an empty list Operations:
makenull () : returns an empty list makelist (elem) : makes a list containing a single
element printlist (list) search(elem, list) : searches whether a key is in
the list insert (elem, list) delete (elem, list) findKth (list)
![Page 4: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/4.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
4
Array Implementation of Listtypedef int ETYPE;typedef struct {
ETYPE elements[MAXS];int size;
} LIST;LIST makenull () ;LIST makeList (ETYPE) ;void printList (LIST) ;int IsEmpty (LIST) ;int search (ETYPE, LIST) ;void delete (ETYPE, LIST * );void insert (ETYPE, LIST * )
![Page 5: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/5.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
5
Complex Number ADTtypedef struct {
float real;float imag;
} COMPLEX;COMPLEX makecomplex (float, float) ;COMPLEX addc (COMPLEX, COMPLEX);COMPLEX subc (COMPLEX, COMPLEX);COMPLEX multc (COMPLEX, COMPLEX);COMPLEX divc (COMPLEX, COMPLEX);
![Page 6: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/6.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
6
SET ADT Interface functions (1):SET makenullset () ;int member (ETYPE, SET)
;SET adjoin (ETYPE, SET);SET union (SET, SET) ;SET intersection (SET,
SET);Void printset (SET) ;
Interface functions (2):SET makenullset () ;int member (ETYPE, SET) ;void adjoin(ETYPE, SET *);void union (SET, SET, SET*);void intersection (SET, SET, SET*);Void printset (SET) ;
![Page 7: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/7.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
7
Concrete implementation of SET ADT
typedef struct {ETYPE elem[MAX];int size;
} SET;
Implementation 1 : sorted arrayadjoin : Sorted insertmember : Binary searchdelete : ?union : merge 2 sorted arrays intersection : ?
![Page 8: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/8.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
8
Concrete implementation of SET ADT
typedef struct {ETYPE elem[MAX];int size;
} SET;
Implementation 2 : unsorted arraykeep the elements in the array unsorted.
adjoin : Insert at the endmember : Search till found or till the enddelete : Go through the array sequentially until
element is found, or reach the end.Then left shift the array.
union , intersection ?
![Page 9: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/9.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
9
Arrays of Structures A struct represents a single record. Typically structs are used to deal with
collections of such records Examples : student records, employee
records, book records, ... In each case we will hav multiple instances
of the struct type.Arrays of structs are the natural way to do
this.
![Page 10: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/10.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
10
Arrays of structs : declaration & use
Each declaration below declares an array, where each array element is a structure:point corner_points[10] ;StudentRecord btech01[MAXS] ;
We access a field of a struct in an array by specifying the array element and then the field :btech01[i].namecorner_points[4].x
![Page 11: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/11.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
11
Naming in struct Arrayspoint pentagon[5];
xy
xy
xy
xy
xy
pentagon : an array of points
pentagon[1] : a point structure
pentagon[4].x : a double
![Page 12: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/12.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
12
Using Arrays of structs
StudentRecord class[MAXS];...for (i=0; i<nstudents; i++) {
scanf (“%d%d”, &class[i].midterm, &class[i].final);
class[i].grade = (double)(class[i].midterm+class[i].final)/50.0;
}
![Page 13: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/13.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
13
struct Array elements as parameters
void draw_line (point p1, point p2) { ... }...point pentagon[5];...for (i=0;i<4;i++)
draw_line (pentagon[i], pentagon[i+1]);
draw_line (pentagon[4], pentagon[0]);
![Page 14: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/14.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
14
structs as Parameters
A single struct is passed by value. all of its components are copied from
the argument (actual parameter) to initialize the (formal) parameter.
point set_midpt (point a, point b) { ... }int main (void) {
point p1, p2, m;...m = set_midpt(p1, p2);
}
![Page 15: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/15.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
15
Passing Arrays of structs An array of structs is an array. When any array is an argument (actual parameter), it
is passed by reference, not copied [As for any array] The parameter is an alias of the actual array
argument.int avg (StudentRec class[MAX]) { ... }int main (void) {
StudentRec bt01[MAX];int average;...average = avg_midpt(bt01) ;
}
![Page 16: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/16.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
16
Dynamic Memory Allocation,Structure pointers
Lecture 2614.3.2002.
![Page 17: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/17.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
17
Basic Idea Many a time we face situations where
data is dynamic in nature. Amount of data cannot be predicted
beforehand. Number of data item keeps changing
during program execution. Such situations can be handled more
easily and effectively using dynamic memory management techniques.
![Page 18: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/18.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
18
C language requires the number of elements in an array to be specified at compile time. Often leads to wastage or memory
space or program failure. Dynamic Memory Allocation
Memory space required can be specified at the time of execution.
C supports allocating and freeing memory dynamically using library routines.
![Page 19: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/19.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
19
Memory Allocation Process in C
Local variables
Free memory
Global variables
InstructionsPermanent storage area
Stack
Heap
![Page 20: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/20.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
20
The program instructions and the global variables are stored in a region known as permanent storage area.
The local variables are stored in another area called stack.
The memory space between these two areas is available for dynamic allocation during execution of the program. This free region is called the heap. The size of the heap keeps changing
![Page 21: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/21.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
21
Memory Allocation Functions
malloc: Allocates requested number of bytes and returns a pointer to the first byte of the allocated space.
calloc: Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory.
free : Frees previously allocated space. realloc: Modifies the size of previously
allocated space.
![Page 22: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/22.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
22
Dynamic Memory Allocation used to dynamically create
space for arrays, structures, etc.
int main () { int *a ; int n; .... a = (int *) calloc (n, sizeof(int)); ....}
a = malloc (n*sizeof(int));
![Page 23: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/23.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
23
Space that has been dynamically allocated with either calloc() or malloc() does not get returned to the function upon function exit.
The programmer must use free() explicitly to return the space. ptr = malloc (...) ; free (ptr) ;
![Page 24: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/24.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
24
void read_array (int *a, int n) ;int sum_array (int *a, int n) ;void wrt_array (int *a, int n) ;
int main () { int *a, n; printf (“Input n: “) ; scanf (“%d”, &n) ; a = calloc (n, sizeof (int)) ; read_array (a, n) ; wrt_array (a, n) ; printf (“Sum = %d\n”, sum_array(a, n);}
![Page 25: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/25.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
25
void read_array (int *a, int n) { int i; for (i=0; i<n; i++)
scanf (“%d”, &a[i]) ;}void sum_array (int *a, int n) { int i, sum=0; for (i=0; i<n; i++) sum += a[i] ; return sum;}void wrt_array (int *a, int n) { int i; ........}
![Page 26: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/26.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
26
Arrays of Pointers Array elements can be of any type
array of structures array of pointers
![Page 27: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/27.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
27
int main (void) {char word[MAXWORD];char * w[N]; /* an array of pointers */int i, n; /* n: no of words to sort */
for (i=0; scanf(“%s”, word) == 1); ++i) { w[i] = calloc (strlen(word)+1, sizeof(char)); if (w[i] == NULL) exit(0);
strcpy (w[i], word) ;}
n = i;sortwords (w, n) ;wrt_words (w, n);return 0;
}
![Page 28: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/28.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
28
w
0
1
2
3
17
Input : A is for apple or alphabet pie which all get a slice of come taste it and try
A \0
i s \0
f o r \0
a p p l e \0
t r y \0
![Page 29: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/29.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
29
void sort_words (char *w[], int n) { int i, j; for (i=0; i<n; ++i)
for (j=i+1; j<n; ++j)if (strcmp(w[i], w[j]) > 0) swap (&w[i], &w[j]) ;
}void swap (char **p, char **q) { char *tmp ; tmp = *p; *p = *q; *q = tmp;}
![Page 30: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/30.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
30
w
w[i]
f o r \0
a p p l e \0
Before swapping
w[j]
![Page 31: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/31.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
31
w
w[i]
f o r \0
a p p l e \0
After swapping
w[j]
![Page 32: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/32.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
32
Pointers to Structure
![Page 33: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/33.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
33
Pointers and Structures You may recall that the name of an array
stands for the address of its zero-th element. Also true for the names of arrays of structure
variables. Consider the declaration:
struct stud { int roll; char dept_code[25]; float cgpa; } class[100], *ptr ;
![Page 34: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/34.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
34
The name class represents the address of the zero-th element of the structure array.
ptr is a pointer to data objects of the type struct stud.
The assignmentptr = class ;
will assign the address of class[0] to ptr. When the pointer ptr is incremented by
one (ptr++) : The value of ptr is actually increased
by sizeof(stud). It is made to point to the next record.
![Page 35: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/35.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
35
Once ptr points to a structure variable, the members can be accessed as: ptr –> roll ; ptr –> dept_code ; ptr –> cgpa ;
The symbol “–>” is called the arrow operator.
![Page 36: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/36.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
36
Warning When using structure pointers, we should
take care of operator precedence. Member operator “.” has higher precedence than “*”.
ptr –> roll and (*ptr).roll mean the same thing. *ptr.roll will lead to error.
The operator “–>” enjoys the highest priority among operators.
++ptr –> roll will increment roll, not ptr. (++ptr) –> roll will do the intended thing.
![Page 37: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/37.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
37
Program to add two complex numbers using pointers
typedef struct { float re; float im;} complex;main() { complex a, b, c; scanf (“%f %f”, &a.re, &a.im); scanf (“%f %f”, &b.re, &b.im); add (&a, &b, &c) ; printf (“\n %f %f”, c,re, c.im);}
![Page 38: 14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002](https://reader035.vdocuments.mx/reader035/viewer/2022062517/56649ee85503460f94bf9524/html5/thumbnails/38.jpg)
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur
38
void add (complex * x, complex * y, complex * t) {
t->re = x->re + y->re ;
t->im = x->im + y->im ;
}