Title: Structures, ADT
1Structures, ADT
2Announcements
- Lab Test 2 on the week of 18th 22nd March
- Syllabus for lab test arrays, structures, data
types, ..
3The List ADT
- A list ltA1, A2, ... , ANgt 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)
4Array Implementation of List
- typedef int ETYPE
- typedef struct
- ETYPE elementsMAXS
- 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 )
5Complex Number ADT
- typedef 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)
6SET 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)
7Concrete implementation of SET ADT
- typedef struct
- ETYPE elemMAX
- int size
- SET
Implementation 1 sorted array adjoin Sorted
insert member Binary search delete ? union
merge 2 sorted arrays intersection ?
8Concrete implementation of SET ADT
- typedef struct
- ETYPE elemMAX
- int size
- SET
Implementation 2 unsorted array keep the
elements in the array unsorted. adjoin Insert
at the end member Search till found or till
the end delete Go through the array
sequentially until element is found, or reach
the end. Then left shift the array. union ,
intersection ?
9Arrays 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.
10Arrays of structs declaration use
- Each declaration below declares an array, where
each array element is a structure - point corner_points10
- StudentRecord btech01MAXS
- We access a field of a struct in an array by
specifying the array element and then the field - btech01i.name
- corner_points4.x
11Naming in struct Arrays
pentagon an array of points
x
y
pentagon1 a point structure
x
y
x
y
pentagon4.x a double
x
y
x
y
12Using Arrays of structs
- StudentRecord classMAXS
- ...
- for (i0 iltnstudents i)
- scanf (dd, classi.midterm,
classi.final) - classi.grade (double)(classi.midtermclass
i.final)/50.0
13struct Array elements as parameters
- void draw_line (point p1, point p2) ...
- ...
- point pentagon5
- ...
- for (i0ilt4i)
- draw_line (pentagoni, pentagoni1)
- draw_line (pentagon4, pentagon0)
14structs 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)
15Passing 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 classMAX) ... int main
(void) StudentRec bt01MAX int
average ... average avg_midpt(bt01)
16Dynamic Memory Allocation,Structure pointers
17Basic 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.
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.
19Memory Allocation Process in C
Local variables
Stack
Free memory
Heap
Global variables
Permanent storage area
Instructions
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
21Memory 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.
22Dynamic 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 (nsizeof(int))
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)
24void 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)
25void read_array (int a, int n) int i
for (i0 iltn i) scanf (d,
ai) void sum_array (int a, int n)
int i, sum0 for (i0 iltn i)
sum ai return
sum void wrt_array (int a, int n)
int i ........
26Arrays of Pointers
- Array elements can be of any type
- array of structures
- array of pointers
27int main (void) char wordMAXWORD char
wN / an array of pointers / int i, n
/ n no of words to sort / for
(i0 scanf(s, word) 1) i)
wi calloc (strlen(word)1,
sizeof(char)) if (wi
NULL) exit(0) strcpy (wi, word)
n i sortwords (w, n) wrt_words (w,
n) return 0
28Input A is for apple or alphabet pie which
all get a slice of come taste it and try
w
0
A
\0
1
i
s
\0
2
f
o
r
\0
3
a
p
p
l
e
\0
17
t
r
y
\0
29void sort_words (char w, int n)
int i, j for (i0 iltn i) for
(ji1 jltn j) if (strcmp(wi, wj) gt
0) swap (wi, wj) void swap
(char p, char q) char tmp
tmp p p q q
tmp
30Before swapping
w
wi
f
o
r
\0
a
p
p
l
e
\0
wj
31After swapping
w
wi
f
o
r
\0
a
p
p
l
e
\0
wj
32Pointers to Structure
33Pointers 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_code25
- float cgpa
- class100, ptr
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 assignment
- ptr class
- will assign the address of class0 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.
35- Once ptr points to a structure variable, the
members can be accessed as - ptr gt roll
- ptr gt dept_code
- ptr gt cgpa
- The symbol gt is called the arrow operator.
36Warning
- When using structure pointers, we should take
care of operator precedence. - Member operator . has higher precedence than
. - ptr gt roll and (ptr).roll mean the same thing.
- ptr.roll will lead to error.
- The operator gt enjoys the highest priority
among operators. - ptr gt roll will increment roll, not ptr.
- (ptr) gt roll will do the intended thing.
37Program 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)
38void add (complex x, complex y, complex
t) t-gtre x-gtre y-gtre t-gtim
x-gtim y-gtim