DSA Ch1 Introduction
DSA Ch1 Introduction
Dr. NGUYEN Ho
Man Rang
Chapter 1
Introduction Basic concepts
Data
Data type
Data structure
Data Structures and Algorithms - CO2003 Abstract data type
Algorithm
Pseudocode
Revision
Data structures
1.1
Introduction
Overview
Dr. NGUYEN Ho
Man Rang
1 Basic concepts
Data
Data type
Data structure
Abstract data type Basic concepts
Data
Algorithm Data type
Data structure
Pseudocode Abstract data type
Algorithm
Pseudocode
2 Revision Revision
Data structures Data structures
Classes
Classes Pointers
Arrays
1.2
Introduction
Sources of Materials
Dr. NGUYEN Ho
Man Rang
2 This document also uses figure, sentences and demo Data type
Data structure
Revision
Algorithms edited by other members in our Department Data structures
• Book entitled Data Structures - A Pseudocode Classes
Pointers
Approach with C++ (first edition, 2001) written by Arrays
1.3
Introduction
Dr. NGUYEN Ho
Man Rang
Basic concepts
Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode
Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes
1.4
Introduction
What is Data?
Dr. NGUYEN Ho
Man Rang
Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode
Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes
Revision
Data structures
Classes
Pointers
• Qualitative data: descriptive information, Arrays
Pointers to structures
• Quantitative data: numerical information (numbers). Pointers to classes
1.6
Introduction
Data type
Dr. NGUYEN Ho
Man Rang
1 A set of values
Basic concepts
2 A set of operations on values Data
Data type
Data structure
Abstract data type
Example Algorithm
Pseudocode
1.7
Introduction
Data structure
Dr. NGUYEN Ho
Man Rang
Example Revision
Data structures
An array is a number of elements of the same type in a Classes
Pointers
specific order. Arrays
Pointers to structures
Pointers to classes
1 2 3 5 8 13 21 34
1.8
Introduction
Abstract data type
Dr. NGUYEN Ho
Man Rang
together with the operations that are meaningful for the Revision
data type. Data structures
Classes
Pointers
Arrays
2 Declaration of operations
3 Encapsulation of data and operations
1.9
Introduction
Abstract data type
Dr. NGUYEN Ho
Man Rang
Basic concepts
Data
Data type
Data structure
Abstract data type
Algorithm
Pseudocode
Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes
1.10
Introduction
Example: List
Dr. NGUYEN Ho
Man Rang
Interface
Revision
Data structures
Classes
Implementation Pointers
Arrays
Pointers to structures
• Linked list
1.11
Introduction
Algorithm
Dr. NGUYEN Ho
Man Rang
What is an algorithm?
1.12
Introduction
Pseudocode
Dr. NGUYEN Ho
Man Rang
Revision
1.13
Introduction
Pseudocode
Dr. NGUYEN Ho
Man Rang
Algorithm Header
• Name
• Parameters and their types
• Purpose: what the algorithm does
• Precondition: precursor requirements for the parameters Basic concepts
Data
Revision
Algorithm Body Data structures
Classes
Pointers
• Statements Arrays
Pointers to structures
• Statement numbers: decimal notation to express levels Pointers to classes
Algorithm average
Pre nothing
Post the average of the input numbers is printed
1 i=0
Basic concepts
2 sum = 0 Data
Data type
3 while all numbers not read do Data structure
Revision
6 sum = sum + number Data structures
7 end Classes
Pointers
10 End average
Algorithm 1: How to calculate the average
1.15
Introduction
Dr. NGUYEN Ho
Man Rang
Basic concepts
Data
Revision
Data structures
Classes
Pointers
Arrays
Pointers to structures
Pointers to classes
1.16
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
Revision
• Where type_name is a name for the structure type, Data structures
Classes
object_names can be a set of valid identifiers for Pointers
Example
struct car_t {
int year ;
string brand ;
Basic concepts
}; Data
Data type
Data structure
car_t toyota ; Abstract data type
car_t mercedes , bmw ; Algorithm
Pseudocode
Revision
Data structures
Example Classes
Pointers
Arrays
struct { Pointers to structures
string brand ;
} toyota , mercedes , bmw ;
1.18
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
1.19
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
Example
1.20
Introduction
Data structures
Dr. NGUYEN Ho
Example Man Rang
struct car_t {
int year ; Basic concepts
string brand ; Data
Data type
} mycar ; Data structure
void printcar ( car_t ); Abstract data type
Algorithm
Pseudocode
int main () { Revision
mycar . brand = " Audi " ; Data structures
Exercise
Basic concepts
Data
• Define a data structure student_t containing a Data type
Data structure
student’s name, firstname and age. Abstract data type
Algorithm
• Write a code in C++ to take input your data and Pseudocode
1.22
Introduction
Data structures
Dr. NGUYEN Ho
Man Rang
Exercise
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s s t r e a m >
u s i n g namespace s t d ;
s t r u c t student_t {
s t r i n g name ;
string firstname ;
i n t a ge ; Basic concepts
}; Data
void i n f o s t u d e n t ( student_t ) ; Data type
Data structure
i n t main ( ) {
Abstract data type
student_t sv ;
Algorithm
string str ;
Pseudocode
c o u t << " E n t e r ␣ y o u r ␣name : ␣ " ;
g e t l i n e ( c i n , s v . name ) ; Revision
c o u t << " E n t e r ␣ y o u r ␣ f i r s t n a m e : ␣ " ; Data structures
g e t l i n e ( cin , sv . firstname ) ;
Classes
c o u t << " E n t e r ␣ y o u r ␣ age : ␣ " ;
Pointers
g e t l i n e ( cin , s t r ) ;
Arrays
s t r i n g s t r e a m ( s t r ) >> s v . age ;
Pointers to structures
i n f o s t u d e n t ( sv ) ;
Pointers to classes
return 0;
}
void i n f o s t u d e n t ( student_t s ) {
c o u t << "My␣name␣ i s ␣ " << s . name << " ␣ " << s . f i r s t n a m e << e n d l ;
c o u t << " I ␣am␣ " << s . age << " ␣ y e a r s ␣ o l d . " << e n d l ;
}
1.23
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
Revision
• Where class_name is a valid identifier for the class, Data structures
Classes
1.24
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
Example
Basic concepts
Data
class Rectangle {
Data type
int width , height ; Data structure
Abstract data type
public :
Algorithm
void set_values ( int , int ); Pseudocode
1.25
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
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 ;
class Rectangle {
i n t width , h e i g h t ;
public :
void set_values ( int , int ) ;
int area ( void ) ; Basic concepts
}; Data
Data type
} Classes
Pointers
i n t main ( ) { Arrays
Rectangle rectA , rectB ; Pointers to structures
rectA . set_values (3 ,4); Pointers to classes
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;
}
1.26
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
Constructors
• Declared with a name that matches the class name and Data type
Data structure
without any return type; not even void. Abstract data type
Algorithm
Pseudocode
Revision
Example Data structures
Classes
Pointers
public :
Rectangle ( int , int );
int area ( void );
};
1.27
Introduction
Classes
Dr. NGUYEN Ho
Man Rang
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 ;
class Rectangle {
i n t width , h e i g h t ;
public :
Rectangle ( int , int ) ; Basic concepts
int area ( void ) ; Data
}; Data type
Data structure
Rectangle : : Rectangle ( int x , int y ) {
Abstract data type
width = x ;
Algorithm
height = y ;
Pseudocode
}
Revision
int Rectangle : : area () {
Data structures
return width∗height ;
Classes
}
Pointers
Arrays
i n t main ( ) {
Pointers to structures
Rectangle rectA (3 ,4);
Pointers to classes
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;
}
1.28
Introduction
Classes
Dr. NGUYEN Ho
Initialization Man Rang
• Member initialization:
class Rectangle {
int width ;
const i n t h e i g h t ; Basic concepts
public : Data
Data type
Rectangle ( int , int ) ; Data structure
}; Pseudocode
} Pointers
Arrays
Pointers to structures
Pointers to classes
i n t main ( ) {
Rectangle rectA (3 ,4);
...
}
1.29
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
Definition
A pointer is a variable whose value is the address of another
variable, i.e., direct address of the memory location.
1.30
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
p
1000
Basic concepts
Data
Data type
value Data structure
Abstract data type
93 Algorithm
Pseudocode
1.31
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
Example
int main ()
{
int v1 = 5 , v2 = 15;
int * p1 , * p2 ;
p1 = & v1 ; Basic concepts
Data
p2 = & v2 ; Data type
p1 = p2 ; Pseudocode
* p1 = 20; Revision
Data structures
cout << " v1 ␣ = ␣ " << v1 << ’\ n ’; Classes
cout << " v2 ␣ = ␣ " << v2 << ’\ n ’; Pointers
Arrays
return 0; Pointers to structures
} Pointers to classes
Exercise
What is the output?
1.32
Introduction
Pointers
Dr. NGUYEN Ho
Man Rang
Exercise
i n t main ( )
{
i n t v1 = 5 , v2 = 1 5 ;
i n t ∗ p1 , ∗ p2 ;
p1 = &v1 ; // p1 = a d d r e s s o f v1 , p1 p o i n t s t o v1 Basic concepts
p2 = &v2 ; // p2 = a d d r e s s o f v2 , p2 p o i n t s t o v2 Data
∗p1 = 1 0 ; // v a l u e p o i n t e d t o by p1 = 1 0 , v1 = 10 Data type
∗p2 = ∗p1 ; // v a l u e p o i n t e d t o by p2 = v a l u e p o i n t e d by p1 , v2 = 10 Data structure
p1 = p2 ; // v a l u e o f p o i n t e r i s c o p i e d , p1 p o i n t s t o v2
Abstract data type
∗p1 = 2 0 ; // v a l u e p o i n t e d by p1 = 2 0 , v2 = 20
Algorithm
c o u t << " v1 ␣=␣ " << v1 << ’ \n ’ ;
Pseudocode
c o u t << " v2 ␣=␣ " << v2 << ’ \n ’ ;
return 0; Revision
} Data structures
Classes
Pointers
Arrays
Output Pointers to structures
Pointers to classes
v1 = 10
v2 = 20
1.33
Introduction
Arrays
Dr. NGUYEN Ho
Man Rang
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. Basic concepts
Data
type var_name [ number_of_elements ]; Data type
Data structure
Abstract data type
Algorithm
Example Pseudocode
Revision
Data structures
int num [8]; Classes
Pointers
0 1 2 3 4 5 6 7 Arrays
Pointers to structures
Pointers to classes
num
1.34
Introduction
Arrays
Dr. NGUYEN Ho
Initializing arrays Man Rang
Revision
int num [] { 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 }; Data structures
Classes
Pointers
Arrays
Exercise Pointers to structures
Pointers to classes
Revision
int myarray [10]; Data structures
int * mypointer ; Classes
Pointers
1.36
Introduction
Pointers and arrays
Dr. NGUYEN Ho
Example Man Rang
p = num + 3; * p = 5; Revision
p = num ; *( p +4) = 8; Data structures
Classes
for ( int n =0; n <5; n ++) Pointers
cout << num [ n ] << " ,␣ " ; Arrays
Pointers to structures
return 0; Pointers to classes
}
Exercise
What is the output? Explain.
1.37
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Structures can be pointed to by its own type of pointers:
struct car_t {
string brand ;
int year ;
};
Basic concepts
Data
car_t mycar ; Data type
car_t * pcar ; Data structure
Abstract data type
Algorithm
Revision
• pcar is a pointer to point to an object of structure type Data structures
Classes
car_t. Pointers
Arrays
Difference: Algorithm
Pseudocode
by pa Revision
Data structures
*a.b *(a.b) Value pointed to by member b Classes
Pointers
of object a Arrays
Pointers to structures
Pointers to classes
1.40
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Exercise
Basic concepts
Data
• Define a data structure student_t containing a Data type
Data structure
student’s name, firstname and age. Abstract data type
Algorithm
• Write a code in C++ using pointers to structures to Pseudocode
1.41
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Exercise
#i n c l u d e <i o s t r e a m >
#i n c l u d e <s s t r e a m >
u s i n g namespace s t d ;
s t r u c t student_t {
s t r i n g name ;
string firstname ;
i n t a ge ;
Basic concepts
};
Data
void i n f o s t u d e n t ( student_t ∗);
Data type
s t u d e n t _ t ∗ p s v = &s v ; Algorithm
string str ; Pseudocode
c o u t << " E n t e r ␣ y o u r ␣name : ␣ " ;
g e t l i n e ( c i n , psv−>name ) ; Revision
c o u t << " E n t e r ␣ y o u r ␣ f i r s t n a m e : ␣ " ; Data structures
g e t l i n e ( c i n , psv−>f i r s t n a m e ) ; Classes
1.42
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
1.43
Introduction
Pointers to structures
Dr. NGUYEN Ho
Man Rang
Revision
Data structures
Classes
bobby . name | pfriend - > name Pointers
1.44
Introduction
Pointers to classes
Dr. NGUYEN Ho
Man Rang
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 ;
class Rectangle {
i n t width , h e i g h t ;
public :
Basic concepts
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 ) {}
Data
i n t area ( void ) { return width ∗ height ; } Data type
}; Data structure
Abstract data type
Algorithm
i n t main ( ) { Pseudocode
Rectangle rectA (3 , 4);
Revision
R e c t a n g l e ∗ r e c t B = &r e c t A ;
Data structures
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 ) ; Classes
Pointers
1.45