0% found this document useful (0 votes)
100 views41 pages

Data Structure and Algorithms (CO2003) : Chapter 1 - Introduction

This document contains an introduction chapter for a course on data structures and algorithms. It includes sections on basic concepts such as data, data types, data structures, abstract data types, algorithms, and pseudocode. It also includes a revision section about defining data structures in C++ using struct and classes. The document provides definitions and examples to explain these fundamental computer science concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views41 pages

Data Structure and Algorithms (CO2003) : Chapter 1 - Introduction

This document contains an introduction chapter for a course on data structures and algorithms. It includes sections on basic concepts such as data, data types, data structures, abstract data types, algorithms, and pseudocode. It also includes a revision section about defining data structures in C++ using struct and classes. The document provides definitions and examples to explain these fundamental computer science concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Data Structure and Algorithms [CO2003]

Chapter 1 - Introduction

Lecturer: Duc Dung Nguyen, PhD.


Contact: [email protected]

Faculty of Computer Science and Engineering


Hochiminh city University of Technology
Contents

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.

• Qualitative data: descriptive information,


• Quantitative data: numerical information (numbers).
• Discrete data can only take certain values (like whole numbers)
• Continuous data can take any value (within a range)

Lecturer: Duc Dung Nguyen, PhD. Contact: [email protected] Data Structure and Algorithms [CO2003] 3 / 38
Data type

Class of data objects that have the same properties.


Data type
1. A set of values
2. A set of operations on values

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

What is a data structure?


1. A combination of elements in which each is either a data type or another data structure
2. A set of associations or relationships (structure) that holds the data together

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

The concept of abstraction:


• Users know what a data type can do.
• How it is done is hidden.

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

Figure 1: Abstract data type model (source: Slideshare)

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

• The most common tool to define algorithms

• English-like representation of the algorithm logic

• Pseudocode = English + code


• English: relaxed syntax being easy to read
• Code: instructions using basic control structures (sequential, conditional, iterative)

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

Data structures can be declared in C++ using the following syntax:


s t r u c t [ type_name ] {
member_type1 member_name1 ;
member_type2 member_name2 ;
member_type3 member_name3 ;
...
} [ object_names ] ;

• 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

• toyota.year, mercedes.year, and bmw.year are of type int.


• toyota.brand, mercedes.brand, and bmw.brand are of type string.

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 ) ;
};

void Rectangle : : set_values ( int x, int y) {


width = x ;
height = y ;
}

int Rectangle : : area () {


r e t u r n width∗ height ;
}

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 ) ;
};

Rectangle : : Rectangle ( int x, int y) {


width = x ;
height = y ;
}

int Rectangle : : area () {


r e t u r n width∗ height ;
}

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.

Address-of operator (&)


The address of a variable can be obtained by preceding the name of a variable with an
ampersand sign (&), known as address-of operator. For example:
p = &v a l u e ;

Dereference operator (*)


To access the variable pointed to by a pointer, we precede the pointer name with the
dereference operator (*).
v a l u e = ∗p ;

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 ;

The following assignment operation would be valid:


mypointer = myarray ;

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

Structures can be pointed to by its own type of pointers:


s t r u c t car_t {
s t r i n g brand ;
int year ;
};
c a r _ t mycar ;
car_t ∗ pcar ;

• mycar is an object of structure type car_t.


• pcar is a pointer to point to an object of structure type car_t.

The following code is valid:


p c a r = &mycar ;

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

arrow operator (->)


The arrow operator (->) is a dereference operator that is used exclusively with pointers to
objects that have members. This operator serves to access the member of an object directly
from its address.
p c a r −>y e a r

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

Combinations of the operators for pointers and for structure members:

Expression Equivalent What is evaluated


a.b Member b of object a
pa->b (*pa).b Member b of object pointed to by
pa
*a.b *(a.b) Value pointed to by member b of
object a

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

bobby . name | p f r i e n d −>name


bobby . e m a i l | p f r i e n d −>e m a i l
bobby . f a v o r i t e _ c a r . b r a n d | p f r i e n d −>f a v o r i t e _ c a r . b r a n d
bobby . f a v o r i t e _ c a r . y e a r | p f r i e n d −>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

You might also like