Data Structure and Algorithms (CO2003) : Chapter 1 - Introduction
Data Structure and Algorithms (CO2003) : Chapter 1 - Introduction
Chapter 1 - Introduction
1. Basic concepts
2. Revision
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 1 / 38
Basic concepts
What is Data?
(Source: datorama.com)
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 2 / 38
What is Data?
Data
Data is information that has been translated into a form that is more convenient to calculate,
analyze.
Example
• Numbers, words, measurements, observations or descriptions of things.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 38
Data type
Example
Type Values Operations
integer −∞, ..., −2, −1, ∗, +, −, %, /,
0, 1, 2, ..., ∞ ++, −−, ...
floating point −∞, ..., 0.0, ..., ∞ ∗, +, −, /, ...
character \0, ..., ‘A’, ‘B’, ..., <, >, ...
‘a’, ‘b’, ..., ∼
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 4 / 38
Data structure
Example
An array is a number of elements of the same type in a specific order.
1 2 3 5 8 13 21 34
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 5 / 38
Abstract data type
Definition
An abstract data type is a data declaration packaged together with the operations that are
meaningful for the data type.
1. Declaration of data
2. Declaration of operations
3. Encapsulation of data and operations
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 6 / 38
Abstract data type
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 7 / 38
Example: List
Interface
• Data: sequence of elements of a particular data type
• Operations: accessing, insertion, deletion
Implementation
• Array
• Linked list
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 8 / 38
Algorithm
What is an algorithm?
The logical steps to solve a problem.
What is a program?
Program = Data structures + Algorithms (Niklaus Wirth)
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 9 / 38
Pseudocode
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 10 / 38
Pseudocode
Algorithm Header
• Name, Parameters and their types
• Purpose: what the algorithm does
• Precondition: precursor requirements for the parameters
• Postcondition: taken action and status of the parameters
• Return condition: returned value
Algorithm Body
• Statements
• Statement numbers: decimal notation to express levels
• Variables: important data
• Algorithm analysis: comments to explain salient points
• Statement constructs: sequence, selection, iteration
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 11 / 38
Pseudocode: Example
Algorithm average
Pre nothing
Post the average of the input numbers is printed
i=0
sum = 0
while all numbers not read do
i=i+1
read number
sum = sum + number
end
average = sum / i
print average
End average
Algorithm 1: How to calculate the average
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 12 / 38
Revision
Data structures
• Where type_name is a name for the structure type, object_names can be a set of valid
identifiers for objects that have the type of this structure.
• Within braces { }, there is a list with the data members, each one is specified with a type
and a valid identifier as its name.
• struct requires either a type_name or at least one name in object_names, but not
necessarily both.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 13 / 38
Data structures
Example
s t r u c t car_t {
int year ;
s t r i n g brand ;
};
car_t t o y o t a ;
c a r _ t m e r c e d e s , bmw ;
Example
struct {
int year ;
s t r i n g brand ;
} t o y o t a , m e r c e d e s , bmw ;
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 14 / 38
Data structures
A member of an object can be accessed directly by a dot (.) inserted between the object name
and the member name.
Example
toyota . year
toyota . brand
mercedes . year
mercedes . brand
bmw . y e a r
bmw . b r a n d
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 15 / 38
Data structures
Example
// e x a m p l e a b o u t s t r u c t u r e s
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
s t r u c t car_t {
int year ;
s t r i n g brand ;
} mycar ;
i n t main ( ) {
mycar . b r a n d = " A u d i " ;
mycar . y e a r = 2 0 1 1 ;
c o u t << "My␣ f a v o r i t e ␣ c a r ␣ i s : " << e n d l ;
c o u t << mycar . b r a n d << " ␣ ( " << mycar . y e a r << " ) " ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 16 / 38
Data structures
Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
s t r u c t car_t {
int year ;
s t r i n g brand ;
} mycar ;
v o i d p r i n t c a r ( car_t ) ;
i n t main ( ) {
mycar . b r a n d = " A u d i " ;
mycar . y e a r = 2 0 1 1 ;
p r i n t c a r ( mycar ) ;
return 0;
}
v o i d p r i n t c a r ( car_t c ) {
c o u t << "My␣ f a v o r i t e ␣ c a r ␣ i s : " << e n d l ;
c o u t << c . b r a n d << " ␣ ( " << c . y e a r << " ) " ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 17 / 38
Data structures
Exercise
• Define a data structure student_t containing a student’s name, firstname and age.
• Write a code in C++ to take input your data and display it.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 18 / 38
Classes
Classes are defined using keyword class, with the following syntax:
c l a s s class_name {
a c c e s s _ s p e c i f i e r _ 1 : member1 ;
a c c e s s _ s p e c i f i e r _ 2 : member2 ;
...
} object_names ;
• Where class_name is a valid identifier for the class, object_names is an optional list of
names for objects of this class.
• The body of the declaration can contain members, which can either be data or function
declarations, and optionally access_specifiers.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 19 / 38
Classes
Example
c l a s s Rectangle {
i n t width , h e i g h t ;
public :
void set_values ( int , int ) ;
int area ( void ) ;
} rect ;
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 20 / 38
Classes
Example
# i n c l u d e <i o s t r e a m >
u s i n g namespace std ;
c l a s s Rectangle {
i n t width , h e i g h t ;
public :
void set_values ( int , i n t ) ;
i n t area ( void ) ;
};
int main ( ) {
Rectangle rectA , rectB ;
rectA . set_values ( 3 , 4 ) ;
rectB . set_values ( 5 , 6 ) ;
c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ;
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B . a r e a ( ) << e n d l ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 21 / 38
Classes
Constructors
• Automatically called whenever a new object of a class is created.
• Initializing member variables or allocate storage of the object.
• Declared with a name that matches the class name and without any return type; not even
void.
Example
c l a s s Rectangle {
i n t width , h e i g h t ;
public :
Rectangle ( int , int ) ;
int area ( void ) ;
};
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 22 / 38
Classes
Example
# i n c l u d e <i o s t r e a m >
u s i n g namespace std ;
c l a s s Rectangle {
i n t width , h e i g h t ;
public :
Rectangle ( int , i n t ) ;
i n t area ( void ) ;
};
int main ( ) {
Rectangle rectA ( 3 , 4 ) ;
Rectangle rectB ( 5 , 6 ) ;
c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ;
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B . a r e a ( ) << e n d l ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 23 / 38
Classes
Initialization
• Member initialization:
c l a s s Rectangle {
i n t width ;
const i n t height ;
public :
Rectangle ( int , int ) ;
...
};
Rectangle ( int x , int y ) : height (y) {
width = x ;
}
i n t main ( ) {
Rectangle rectA (3 ,4);
...
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 24 / 38
Pointers
Definition
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 25 / 38
Pointers
1000
value
93
999 1000 1001
p = &v a l u e ;
v a l u e = ∗p ;
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 26 / 38
Pointers
Example
i n t main ( ) {
i n t v1 = 5 , v2 = 1 5 ;
i n t ∗ p1 , ∗ p2 ;
p1 = &v1 ;
p2 = &v2 ;
∗ p1 = 1 0 ;
∗ p2 = ∗ p1 ;
p1 = p2 ;
∗ p1 = 2 0 ;
c o u t << " v1 ␣=␣ " << v1 << ’ \ n ’ ;
c o u t << " v2 ␣=␣ " << v2 << ’ \ n ’ ;
return 0;
}
Exercise
What is the output?
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 27 / 38
Arrays
Definition
An array is a series of elements of the same type placed in contiguous memory locations that
can be individually referenced by a unique identifier with an index.
t y p e var_name [ number_of_elements ] ;
Example
i n t num [ 8 ] ;
0 1 2 3 4 5 6 7
num
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 28 / 38
Arrays
Initializing arrays
int num [ 8 ] ;
int num [ 8 ] = { } ;
int num [ 8 ] = { 1 , 2 , 3 , 5 , 8 , 1 3 , 2 1 , 34 } ;
int num [ 8 ] = { 1 , 2 , 3 , 5 , 8 } ;
int num [ ] = { 1 , 2 , 3 , 5 , 8 , 1 3 , 2 1 , 34 } ;
int num [ ] { 1 , 2 , 3 , 5 , 8 , 1 3 , 2 1 , 34 } ;
Exercise
For each declaration of num, what is the output?
f o r ( i n t i =0; i <8; i ++) {
c o u t << num [ i ] << e n d l ;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 29 / 38
Pointers and arrays
The concept of arrays is related to that of pointers. Arrays work very much like pointers to
their first elements, and, actually, an array can always be implicitly converted to the pointer of
the proper type.
For example, consider these two declarations:
i n t myarray [ 1 0 ] ;
int ∗ mypointer ;
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 30 / 38
Pointers and arrays
Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
i n t main ( ) {
i n t num [ 5 ] ;
int ∗ p;
p = num ; ∗p = 1 ;
p++; ∗p = 2 ;
p = &num [ 2 ] ; ∗p = 3 ;
p = num + 3 ; ∗p = 5 ;
p = num ; ∗ ( p+4) = 8 ;
f o r ( i n t n =0; n <5; n++)
c o u t << num [ n ] << " , ␣ " ;
return 0;
}
Exercise
What is the output? Explain.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 31 / 38
Pointers to structures
The value of the pointer pcar would be assigned the address of object mycar.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 32 / 38
Pointers to structures
Difference:
• Two expressions pcar->year and (*pcar).year are equivalent, and both access the member
year of the data structure pointed by a pointer called pcar.
• Two expressions *mycar.year or *(mycar.year) are equivalent. This would access the value
pointed by a hypothetical pointer member called year of the structure object mycar (which
is not the case, since year is not a pointer type).
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 33 / 38
Pointers to structures
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 34 / 38
Pointers to structures
Exercise
• Define a data structure student_t containing a student’s name, firstname and age.
• Write a code in C++ using pointers to structures to take input your data and display it.
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 35 / 38
Pointers to structures
Structures can also be nested in such a way that an element of a structure is itself another
structure:
Example
s t r u c t car_t {
s t r i n g brand ;
int year ;
};
struct friends_t {
s t r i n g name ;
s t r i n g email ;
car_t f a v o r i t e _ c a r ;
} bobby , tommy ;
f r i e n d s _ t ∗ p f r i e n d = &bobby ;
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 36 / 38
Pointers to structures
After the previous declarations, all of the following expressions would be valid:
Example
tommy . name
tommy . e m a i l
tommy . f a v o r i t e _ c a r . b r a n d
tommy . f a v o r i t e _ c a r . y e a r
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 37 / 38
Pointers to classes
Example
#i n c l u d e <i o s t r e a m >
u s i n g namespace s t d ;
c l a s s Rectangle {
i n t width , h e i g h t ;
public :
R e c t a n g l e ( i n t x , i n t y ) : w i d t h ( x ) , h e i g h t ( y ) {}
i n t area ( void ) { r et u r n width ∗ height ; }
};
i n t main ( ) {
Rectangle rectA (3 , 4);
R e c t a n g l e ∗ r e c t B = &r e c t A ;
R e c t a n g l e ∗ r e c t C = new R e c t a n g l e ( 5 , 6 ) ;
c o u t << " r e c t A ␣ a r e a : ␣ " << r e c t A . a r e a ( ) << e n d l ;
c o u t << " r e c t B ␣ a r e a : ␣ " << r e c t B −>a r e a ( ) << e n d l ;
c o u t << " r e c t C ␣ a r e a : ␣ " << r e c t C −>a r e a ( ) << e n d l ;
delete rectB ;
delete rectC ;
return 0;
}
Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 38 / 38