Professional Documents
Culture Documents
Review
User dened datatype
Structures
Unions
Bitelds
Data structure
Memory allocation
Linked lists
Binary trees
Review: pointers
Review: string.h
Searching
Linear search: O(n)
Binary search: O(logn). The array has to be sorted rst.
Sorting
Insertion sort: O(n2 )
Quick sort: O(n log n)
Review
User dened datatype
Structures
Unions
Bitelds
Data structure
Memory allocation
Linked lists
Binary trees
Structure
Examples:
struct point
{
int x ;
int y ;
};
/ n o t i c e t h e ; a t t h e end /
s t r u c t employee
{
char fname [ 1 0 0 ] ;
char lname [ 1 0 0 ] ;
i n t age ;
};
/ members o f d i f f e r e n t
type /
Structure
members
Variables can be declared like any other built in data-type.
struct point ptA;
Initialization is done by specifying values of every member.
struct point ptA={10,20};
Assignment operator copies every member of the structure
Structure (cont.)
More examples:
struct t r i a n g l e
s t r u c t chain_element
i n t data ;
s t r u c t p o i n t ptA ;
s t r u c t chain_element n e x t ;
s t r u c t p o i n t ptB ;
} ;
s t r u c t p o i n t ptC ;
/ members can be
} ;
/ members can be s t r u c t u r e s / s e l f r e f e r e n t i a l /
Structure (cont.)
s t r u c t p o i n t t l ; / t o p l e f t /
s t r u c t p o i n t b r ; / b o t r i g h t /
} ;
s t r u c t r e c t a n g l e r e c t ;
i n t t l x = r e c t . t l . x ; / nested /
i n t t l y = r e c t . t l . y ;
Structure pointers
-> operator.
s t r u c t p o i n t p = { 1 0 , 2 0 } ;
s t r u c t p o i n t pp=&p ;
pp>x = 1 0 ; / changes p . x /
Arrays of structures
Size of structures
char c ;
/ padding /
int i ;
Why is this an important issue? libraries, precompiled les,
SIMD instructions.
Members can be explicitly aligned using compiler
extensions.
__attribute__ (( aligned(x ))) /gcc/
__declspec((aligned(x))) /MSVC/
10
Union
11
Unions (cont.)
largest element.
Important: The compiler does not test if the data is being
s t r u c t v a r i a n t
union data d ;
enum dtype t ;
} ;
12
Bit elds
unsigned i n t i s _ c o l o r : 1 ;
unsigned i n t has_sound : 1 ;
unsigned i n t i s _ n t s c : 1 ;
} ;
struct ag f ;
f .has_sound=1;f.is_color=1;
f .has_sound=0;f.is_color=0;
if ( f . is_color || f .has_sound)
13
Review
User dened datatype
Structures
Unions
Bitelds
Data structure
Memory allocation
Linked lists
Binary trees
14
void malloc(size_t n)
malloc() allocates blocks of memory
returns a pointer to unitialized block of memory on
success
bytes.
initializes memory to 0
void free(void)
Frees memory allocated my malloc()
Common error: accessing memory after calling free
14
Linked list
the list.
The start (head) of the list is maintained in a separate
variable.
12
99
37
15
Linked list
s t r u c t node
{
i n t data ; / payload /
s t r u c t node n e x t ;
} ;
s t r u c t node head ; / b e g i n n i n g /
size
indexing
inserting
deleting
linked-list
dynamic
O(n)
O(1)
O(1)
array
xed
O(1)
O(n)
O(n)
16
Linked list
r e t u r n p ;
17
Linked list
18
Linked list
Iterating:
/ do something /
f o r ( p=head ; p>n e x t ! =NULL ; p=p>n e x t )
/ do something /
19
Binary trees
20
s t r u c t tnode
{
i n t data ; / payload /
s t r u c t tnode l e f t ;
s t r u c t tnode r i g h t ;
};
right subtree
21
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.