Sie sind auf Seite 1von 22

Introduction to Data Structures

Topics
 Scalar data types.
 Introduction to Abstract Data Types (ADT)
 Implementation of an ADT
 Advantages of an ADT
Scalar data types.
 Scalar data type
 Consists of data values that are considered
non-decomposable (single). See next slide
 Examples in C++: char, int, unsigned, long, float, double.

 Data Type for a variable determines:


 Legal value that it may assume (int, float ..).
 Amount of storage it may require(1, 2, 4 .. Bytes) .
 Legal Operations that can be performed on it( +, _ , >, ..).
Composite and DeComposite data
 Composite Data Type: can be broken into components
that have meaning, such as a phone number (can be broken
into prefix-exchange).

• DeComposite (Non-Composite) from ‘decomposed’:


data that cannot be further decomposed.

Example: int socsecno. If we decompose it, it becomes


three meaningless fields of 3, 2 and 4 bytes. The
individual numbers do not have the same meaning as the
original number.
Definition of “Abstract”
“Thought of apart from concrete
realities, specific objects, or actual
instances: ex. an abstract idea”

In other words, a
conceptualization of something
at some level of detail
What is abstraction?
 To distill (break down) a complicated system into its
most fundamental components (parts).
And
 To describe these parts in a simple precise language .
 Describing the system parts involves:
 Naming them and describing their functionalities.
 Abstraction is a powerful technique that helps programmers deal
with complex issues in piecemeal fashion.
“In piecemeal fashion" means one piece at a time rather than
all at once.
Examples of Abstractions
 Real-world example

 Data abstraction

 Mathematical concepts abstraction

 Procedural abstraction

 Data Type abstraction (ADT’s)


Abstraction and your car
 You need to know how to use :
 The key to start the engine.
 The accelerator and the breaks to control speed.
 Steering wheel to control directions.

BUT
You do not need to know the details of the car’s
electrical system, braking system or steering
mechanism.
Abstraction and program
variables (Data Abstraction)
 No need to be concerned with the physical structure
of memory in order to declare and use a variable.

 No need to be concerned with the manner in which


data is being stored or represented in memory.

This analogy is known as the logical view of data, as opposed to the


physical view.
The ADT and the mathematical
Model
 Specifies what each operation does, NOT how it is
being done.

total = 9 - 15 + 20 * 6

We understand that the result will be 114, but not


how the machine will perform the calculations at
the hardware level.
Procedural Abstraction
 The philosophy that procedure development should
separate the concern for what is to be achieved by a
function from the details of how is it to be achieved.

 In other words:

If there is a commonly used function, it is only necessary


to be aware of the inputs and outputs. Users of a
function do not need to know how the outputs are
achieved procedurally.
Example of Procedural Abstraction
 you know what is needed to be done, but how it is done is
hidden:
 READ
 WRITE
 COPY

We know the effect of a READ or WRITE instruction, but we


do not need to know:
• How the I/O device operates
• How the I/O controller communicates with the Operating System
to cause an interrupt
• On which cylinder/track/sector the data is stored on the I/O device
• The assembly/machine language instructions being executed
Abstract Data Types (ADT)
A data type that specifies:
– values that can be stored (a collection of
data with defined properties).
– operations that can be done (performed) on
the values
• The user of an abstract data type does not need to
know the implementation of the data type (e.g., how the
data is stored).
• ADTs are created by programmers
• An ADT is also called a Data Structure
ADT
 Very important in Computer Science.

 Very significant in object-oriented programming.

 C++ implements ADT’s (Data Structures) with

 Structures
 Classes
 Arrays
Abstraction and Data Types
• Abstraction: a definition that captures general
characteristics without details
– Ex: An abstract triangle is a 3-sided polygon. A
specific triangle may be scalene, isosceles, or
equilateral

• An ADT is a composite data type created by the


programmer, which is connected to one or more de-
composite data types.
Examples of ADTs
 Ordered pair
 Vector/array
 Matrix
 Set
 Stack
 Waiting line/Queue
 List
Make sure you know the difference between the
mechanisms used to represent the data structures
and the examples used to implement the data
structures
ADT Definitions
 Ordered pair:
 An ordered pair of elements written as (a, b)
Is considered distinct from (b, a) unless a = b.
 Vector:
The Vector ADT extends the notion of array by storing a
sequence of arbitrary objects. An element can be
accessed, inserted or removed by specifying its rank (number of elements preceding
it). V = (a1, a2, … an) 3 5 7
 Matrix: data organized in rows and columns A = 2 8 9
9 9 6
 Set: a collection of objects A = { 2, 3, 8 } order does not matter
 Stack: a linear list in which all addition and deletion are restricted to one end.
 List: like the shopping list, items can be accessed and crossed out in any order.
 Queue: a linear list in which data can only be inserted at one end called the rear
and deleted from one end called the front.
Criteria for selecting a data structure

 Is the data structure a good representation of the


collection of data and its properties?

 Programmer Efficiency: how easy will it be to write


the algorithms/ functions to implement the
operations in the ADT?

 Will it result in an efficient implementation in


terms of storage requirements and execution
time?
The Design and the Implementation process

Real world application

Design abstraction: abstract the


essential properties and operations.

Abstract Data Types Data Structure declaration


and
function definition
Implementation:
in a programming
language
Advantages of creating ADT Implementations
(next 4 slides)
1. The same ADT can be used in different application programs
(Reusability).
 Example: A queue implementation could be used in many
different application programs that use this ADT.

2. Applications can be written and debugged more rapidly


(Programmer Productivity)
 Many of the app details are already written, tested and debugged
within the ADT implementation
 The application program will simply make calls to the functions
that are in the implementation.
Advantages of creating ADT Implementations
3. Application Source code is more readable

– Low level data structure details have been restricted to the


functions in the implementation

– Example: if (empty (waiting_line))


cout<<“ No more customers;

IS MORE READABLE than:

if (waiting_line.front == NULL)
cout<<“ No more customers;
Advantages of creating ADT Implementations
4. The source code of the program is insulated from the
data structure in the implementation of the ADT.

If the data structure in the implementation of the ADT is changed,


the source code for the application program that uses the ADT
does not have to be modified.

ONLY the source code for the function in the implementation of the
ADT will have to be changed.

Das könnte Ihnen auch gefallen