Upload
francis-hutchinson
View
214
Download
1
Embed Size (px)
Citation preview
Data Structure
Lecture 3Abstract Data Type
Sandy Ardianto & Erick Pranata© Sekolah Tinggi Teknik Surabaya
1
Motives» Problem Solving is different from coding˃ Algorithm˃ Documentation˃ Code
» Algorithm and Documentation is Abstract Layer
» Code is Implementation Layer
2
© Sekolah Tinggi Teknik Surabaya
Motives» Abstraction is important to˃ Improve Readablity˃ Organize Data through Encapsulation
» Abstract Data Type (ADT)˃ an object with a generic description
independent of implementation details˃ we think about what can be done with the
data, not how it is done
3
© Sekolah Tinggi Teknik Surabaya
ADT is A Set of Operation» ADT abstracts away from a specific representation to
focus on the semantic meaning of the data » In what sense are the following two definitions
different?
» Although the representations differ, the client should instead consider a Point as a set of operations to create and manipulate 2D points on the plane
» By restricting the client to only call operations to access data, the potential for modifying the representation (and supporting algorithms) remains
4
© Sekolah Tinggi Teknik Surabaya
ADT = Objects + Operations» The only operations on objects of the
type are those provided by the abstraction
» The implementation is hidden
5
© Sekolah Tinggi Teknik Surabaya
ADT Example
6
© Sekolah Tinggi Teknik Surabaya
Implementation of ADT» An implementation of ADT consists of
storage structures to store the data items and algorithms for basic operation
7
© Sekolah Tinggi Teknik Surabaya
Another Example» Consider example of an airplane flight with 10
seats to be assigned» Tasks
˃ List available seats˃ Reserve a seat
» How to store, access data?
Another Example» Consider example of an airplane flight with 10
seats to be assigned» Tasks
˃ List available seats˃ Reserve a seat
» How to store, access data?˃ 10 individual variables
Use 10 individual variables
» Algorithm to List available seats
1. If seat1 == ‘ ’:display 1
2. If seat2 == ‘ ’: display 2
.
.
.10. If seat10 == ‘ ’:
display 10
» Algorithm to Reserve a seat
1. Set DONE to false2. If seat1 ==‘ ’:
print “do you want seat #1??”Get answerif answer==‘Y’:
set seat1 to ‘X’set Done to True
3. If seat2 ==‘ ’:print “do you want seat #2??”Get answerif answer==‘Y’:
set seat2 to ‘X’set Done to True
.
.
.
Another Example» Consider example of an airplane flight with 10
seats to be assigned» Tasks
˃ List available seats˃ Reserve a seat
» How to store, access data?˃ 10 individual variables˃ An array of variables
Use Array» Algorithm to List available seats
For number ranging from 0 to max_seats-1, do:If seat[number] == ‘ ’:
Display number
» Algorithm to Reserve a seat
Read in number of seat to be reservedIf seat[number] is equal to ‘ ’:
set seat[number] to ‘X’Else
Display a message that the seat having this number is occupied
Exercise» Develop a USD ADT˃ Properties
+ dollar: int+ cent: int
˃Methods+ Add+ Subtract+ Multiply+ Divide+ ToString
13
© Sekolah Tinggi Teknik Surabaya
Reference» Abstract Data Type, http://
c2.com/cgi/wiki?AbstractDataType, accessed on March 24th 2014.
» Abstract Data Types, http://cse.iitkgp.ac.in/pds/notes/ADT.html, accessed on March 24th 2014.
» Dr. Bernard Chen Ph.D., Data Structures and Abstract Data Type, 2008
14
© Sekolah Tinggi Teknik Surabaya