CH1 Upload-2023.03.02
CH1 Upload-2023.03.02
資料結構
Chih-Wei Lin(林志瑋)
Email:[email protected]
Electrical Engineering, College of Electrical Engineering and Computer Science
National Taiwan Ocean University
Data Structures
Basic Concepts
Chih-Wei Lin(林志瑋)
Email:[email protected]
Electrical Engineering, College of Electrical Engineering and Computer Science
National Taiwan Ocean University
Chapter 1 Basic Concepts
• Introduction
• Algorithm Specification
• Data Abstraction
• Performance Analysis
• Performance Measurement
Introduction
7
Data
8
Information
• The results after ”Data processing" are
information.
Comparison table - characteristics of data and Information
9
Information
• Data processing will organize, calculate and analyze
the original data, and make it become useful
information (including total score, average, and
ranking).
Student's complete transcript
10
Information
• Useful information is the data that decision makers
need to use when thinking about a certain problem
• It is subjective.
• For example:
• The class tutor (decision maker) wants to reward the
students according to their test scores after the students
have finished the mid-term exam.
11
Example
12
Example
13
Example
14
Data Structures
16
What is a Program?
17
What is a Program?
19
Name Phone number
Judy 0910235588
Example Amy
。。。
0952611999
。。。
Table 1-1 Linear table structure
• EX1: Telephone Number Inquiry System
A telephone directory, which records the names of N
individuals and their corresponding telephone numbers.
Assume that it is arranged in the following form:
(a1, b1), (a2, b2), ... (an, bn),
New
Moscow York
Paris
London
23
Queue List 24
Example
Sort Search 25
Example
26
Analysis
• Ex: When the number of people is 50, the program codes in line
numbers 03 and 04 will become very complex, which makes errors..
27
Improvement
• Use "array" data structure: It is used to store grades
of classmates.
• Algorithm: using for loop
28
Comparison
29
Basic Concepts and Terminology
• Data
• A symbolic representation of objective things
• Data Element
• Basic unit
• A data element can consist of several Data Items.
Basic Concepts and Terminology
• Data Item
• The indivisible smallest unit of data
• A data description of a certain aspect of an objective thing.
• Data Object
• A collection of data elements with the same properties,
• A subset of the data.
• Such as character set C={‘A’, ‘B’, ‘C’…}
Basic Concepts and Terminology
• Data Structure
• collection of data elements that have (exist) a certain
connection (relationship) with each other.
• The interrelationships (relationships) between elements
are called logical structures.
• There are four basic types of logical structure between
data elements
Basic Concepts and Terminology
• Data Structure
• four basic types of logical structure
• Set: relationship is "belonging to the same set".
• Linear structure: one-to-one relationship
• Tree structure: One-to-many relationship.
• Graph structure or network structure: Many-to-many relationship
• Summarization
• The relationship between data elements can be a
natural relationship, or an artificially defined
relationship.
• This natural or artificially defined "relationship" is
called a logical relationship between data elements,
and the corresponding structure is called a logical
structure.
Storage of Data Structure
• Summarization
• The logical structure and physical structure of data are
inseparable.
• The design of an algorithm depends on the chosen logical
structure
• The realization of the algorithm depends on the adopted
storage structure.
Storage of Data Structure
• Summarization
• In C language
• One-dimensional array is used to represent a sequential
storage structure
• Structure type is used to represent a linked storage
structure
Three components of a data
structure
• Logical structure:
• A description of the logical relationship between data
elements
• Storage structure:
• The storage of data elements in the computer and the
representation of their logical relationships
• Called data storage structures or physical structures.
• Data operation:
• The operation to be performed on the data.
Three components of a data
structure
• Three logical structures and their storage structures
Logical Structure Physical Structure
linear List sequential storage structure
V-model
System life cycle
V-model
System life cycle
V-model
System life cycle
• Testing
• Debugging
System life cycle
Both assignment statements above store the integer value 10 in i. The * sign before pi in the
second assignment statement is a reference, and 10 is not stored in the pointer, but stored in
the storage unit pointed by the pointer pi.
Pointers and D y n a m i c M e m o r y
Distribution
The original storage space is now no longer accessible. This is an example of a dangling reference. As
long as the pointer to the dynamic storage area is lost, the original storage area is lost for the program.
Pointers and D y n a m i c M e m o r y
Distribution
• Vulnerability of pointers
• In the C program, we assign the NULL value to the
pointer, if the pointer has not yet pointed to the actual
target.
• It is possible to can avoid accessing an unapplied space, or
accessing a space that we do not have permission to access. 。
Pointers and D y n a m i c M e m o r y
Distribution
• Vulnerability of pointers
• When converting pointer types, use explicit casts
• for example:
Algorithm
66
Algorithm Specification
•1.3.1 Introduction
• An algorithm is a finite set of instructions that
accomplishes a particular task.
• Criteria
• input: zero or more quantities that are externally supplied
• output: at least one quantity is produced
• definiteness: clear and unambiguous
• finiteness: terminate after a finite number of steps
• effectiveness: instruction is basic enough to be carried out
• A program does not have to satisfy the finiteness
criteria.
Algorithm Specification
• Representation
• A natural language
• like English or Chinese.
• A graphic
• like flowcharts.
• A computer language
• like C.
Algorithm Specification
• Examples: (To illustrate the conversion process from problem description to
algorithm formation.)
• Solution process:
• Finding the smallest from the existing unsort integers
• Input
• Definiteness
• Finiteness
• Correctness
• Output
70
Input
71
Definiteness
• Each instruction must be clear, without ambiguous.
• Ex: A number is even or old
• Algorithm 1 W/O
• (1). Enter a positive integer Definiteness
• (2). Execute modulo operation and identify whether the result is 0 or not
• (3). 0 is an even number
• Point 2 does not qualify for clarity
• It must state how to calculate the ”modulo operation"
• Algorithm 2
• (1). Enter a positive integer N W/.
• (2). If N is divided by 2, the remainder is 0 Definiteness
• (3). Then N is an even number
72
Finiteness
•Definition
• Algorithms cannot have infinite loops
• They must be completed in finite steps.
• Ex:
• (1). Enter a positive integer N
• (2). If N is divided by 2, the remainder is 0
• (3). Then N is an odd number =>應該改為偶數 符合明確性,但不正確
74
Output
• at least one output
75
Three types of Algorithms
• Text
• Flowchart
• Pseudo Code
76
Text
• Please use "text " to describe the process of a user logging in with the
account and password.
• Step 1: Enter user ID and password
• Step 2: Determine whether the account number and password are correct
• Step 3: If it is correct, you can log in to the system, otherwise you cannot log
in.
77
Flowchart
• Flowchart: Use graphics to express the steps to solve
the problem..
• Please use "Flowchart" to describe the process of a user
logging in with the account and password.
流程圖可以協助程式設計者設計程式.可以增加程式的可讀性 78
Pseudo Code
79
Pseudo Code
• Macro statement
• #define SWAP(x,y,t)((t)=(x),(x)=(y),(y)=t)
• COMPARE(Param1, Param2)
• Comparison operations can be implemented with functions or
macros.
• Both implementation include three conditions: “less than”,
“equal to”, and “greater than”.
• COMPARE(Param1, Param2)-Implementation
• Function
• Macro (#define)
Algorithm Specification
• Recursive algorithms
• Junior programmers take a function as something that is
called by another function
• It executes the code and then returns control to the calling function.
Algorithm Specification
對應找成功的情形,程序無需改動;但
while語句應替換成等價的if語句。
Note that, the calling method is the same between recursive and non-
recursive functions.
• Example 1.4 [Permutations]:
Call Stack:
main
0 is start index
2 is end index
SWAP
SWAP
SWAP((((list[1],list[2],
SWAP list[1],list[1],
list[0],list[0],
list[0],list[0],temp)
list[0],list[1],
list[1],list[1],
list[1],list[2], temp)
temp)
temp)
SWAP
SWAP
SWAP‘b’
SWAP
SWAP ‘a’
‘b’
‘a’ ó
‘a’óó ‘b’
‘c’
ó‘b’‘a’
‘c’
‘a’
I=0 J=3
I=1 J=0 N=2
J=1
J=2 Call
Call :: perm
perm (( list,2,
list,1,
list,1, 2)
2)
lv0 perm: i=0, n=2 abc
lv0 SWAP: i=0, j=0 abc
lv1 perm: i=1, n=2 abc
lv1 SWAP: i=1, j=1 abc
lv2 perm: i=2, n=2 abc
• Example 1.4 [Permutations]: print: abc
lv1 SWAP: i=1, j=1 abc
lv1 SWAP: i=1, j=2 abc
lv2 perm: i=2, n=2 acb
print: acb
lv1 SWAP: i=1, j=2 acb
lv0 SWAP: i=0, j=0 abc
lv0 SWAP: i=0, j=1 abc
lv1 perm: i=1, n=2 bac
lv1 SWAP: i=1, j=1 bac
lv2 perm: i=2, n=2 bac
print: bac
lv1 SWAP: i=1, j=1 bac
lv1 SWAP: i=1, j=2 bac
lv2 perm: i=2, n=2 bca
print: bca
lv1 SWAP: i=1, j=2 bca
lv0 SWAP: i=0, j=1 bac
lv0 SWAP: i=0, j=2 abc
lv1 perm: i=1, n=2 cba
lv1 SWAP: i=1, j=1 cba
lv2 perm: i=2, n=2 cba
print: cba
lv1 SWAP: i=1, j=1 cba
lv1 SWAP: i=1, j=2 cba
lv2 perm: i=2, n=2 cab
print: cab
lv1 SWAP: i=1, j=2 cab
lv0 SWAP: i=0, j=2 cba
Data Abstraction
• Data Type
definition
A data type is a collection of objects and a set of
operations that act on those objects.
• For example, the data type int consists of the objects {0,
+1, -1, +2, -2, …, INT_MAX, INT_MIN} and the
operations +, -, *, /, and %.
INT_MAX and INT_MIN are the largest and smallest integers that the
machine can represent (these two values are defined in the header file
limists.h of the C language)
Data Abstraction
::= is defined as
Concept of Programming
100
Step 1: Analyzing the problem
(Requirement)
• First, you need to understand the requirements and
conditions of the problem.
101
Step 2: Designing the steps to solve
the problem (Algorithm)
• Text
• Flowchart
• Pseudocode
102
Step 3: Writing the program
(Coding)
• Choose an appropriate programming language and
then write a program according to the steps of the
algorithm.
103
Step 4: Testing on the machine to
detect errors (debugging)
• A useful and easy-to-use program must go through
multiple tests.
104
Step 5: Writing the program
manual (executable)
• Users will be willing to use a powerful and complete
program
105
Difference Between Algorithms and
Programs
• Algorithms’ subjects are "human”
• That is, codes that can be read by anyone.
• Therefore, readability is the most
important issue.
107
Conditions for a good program
• Correctness
• Performance
• Maintainable
108
Correctness
• Correctness is the basic requirement of a good
program
• Ex: Design a program to identify parity
109
Performance
• Performance refers to the time it takes for the program to
actually execute.
• Generally, the evaluation of execution time is to calculate the
total number of times that execute the code which refers to
frequency.
• The higher the frequency, the longer the required execution time
• Ex: Calculate the number of executing times of the variable Count in
the following program ?
110
Maintainable
• Ex:
• Modularization
• Annotation
111
Tips of Maintainable
• Layout
• Annotation
• Name
112
Layout
• W/. Indentation
• W/O Indentation
113
Annotation
• It is a non-executable statement
• That is, it is read by humans, and the computer will not
execute it.
• Annotation explains the function and goal of a
specific statement in code.
• ”//”: It can be written at the end of the code or as a single
line comment.
114
Naming variables and function
names
• Good Declaration
• The variable name can express the meaning of the variable
• Ex: string stud_name;
• Bad Declaration
• It is impossible to know the meaning of the variable
immediately (Thinking).
• Ex: string A, B;
115
Structured Programming
• Use the "top-down" technique to decompose the
program into modules with independent functions,
as shown in the figure below.
結構化程式設計示意圖
116
Structured Programming
• Each independent functional module is composed of
three basic structures of structured programming.
請分別撰寫一個程式
117
Sequential Structure
• The program is executed one by one in order from
top to bottom.
118
Selection Structure
• The paths are selected to execute based on the
conditions.
• Ex: Calculate the average score of two subjects, and
judge whether the average score is passed.
119
Repetition Structure
• It refers to the statement that a program is repeatedly
executed multiple times.
• Use the loop to calculate the average score of the two
subjects.
120
Efficiency Evaluation of
Algorithms
• It is used to calculate the time required for the actual
execution of a program after compilation.
121