You are on page 1of 30

Propedutico de

Programacin

Coordinacin de Ciencias
Computacionales
Semana 4,
Tercera Parte
Dra. Pilar Gmez Gil

http://ccc.inaoep.mx/~pgomez/cursos/programacion/
Versin 1.2 26.06.08
Chapter 4
ADT Sorted
List
ADT Sorted List
Remember the difference between an unsorted list and a
sorted list?

Remember the definition of a key?

If the list is a sorted list
of names, what would be the key?
of bank balances, what would be the key?
of grades, what would be the key?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
ADT Unsorted List Operations
Transformers
MakeEmpty
InsertItem
DeleteItem
Observers
IsFull
GetLength
RetrieveItem

Iterators
ResetList
GetNextItem
change state





observe state





process all
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Which member function
specifications and implementations
must change to ensure that any
instance of the Sorted List ADT
remains sorted at all times?

InsertItem

DeleteItem
ADT Sorted List
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
La insercin tendr que ser de tal
manera que la lista siempre se
mantenga ordenada
Linked Implementation
What about passing spot where item
would be if there?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Linked Implementation
Is Inserting as easy? Let's see
Set location to listData
Set moreToSearch to (location != NULL)
while moreToSearch
switch (item.ComparedTo(location->info))
case GREATER :
Set location to location->next
Set moreToSearch to (location != NULL)
case LESS : Set moreToSearch to false

See
the
problem
?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Linked Implementation
We need a trailing pointer
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Inserting S into a Sorted List
C L X
Private data:
length 3

listData

currentPos ?

predLoc location
moreToSearch
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Finding proper position for S
C L X
Private data:
length 3

listData

currentPos ?

predLoc location
NULL
moreToSearch true
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Finding proper position for S
C L X
Private data:
length 3

listData

currentPos ?

predLoc location
moreToSearch true
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Finding Proper Position for S
C L X
Private data:
length 3

listData

currentPos ?

predLoc location
moreToSearch false
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Inserting S into Proper Position
C L X
Private data:
length 4

listData

currentPos

predLoc location
moreToSearch false
S
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Ver sorted.h
Ver sorted.cpp
Linked Implementation
Does DeleteItem have to be
changed?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Object-oriented design decomposes a problem into
classes

Four stages to the decomposition process
Brainstorming
Filtering
Scenarios
Responsibility algorithms

Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Brainstorming
A group problem-solving technique that involves
the spontaneous contribution of ideas from all
members of the group
All ideas are potential good ideas
Think fast and furiously first, and ponder later
A little humor can be a powerful force
Brainstorming is designed to produce a list of
candidate classes

Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Filtering
Determine which are the core classes in
the problem solution
There may be two classes in the list that have
many common attributes and behaviors
There may be classes that really dont belong
in the problem solution
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Scenarios
Sequences of steps that describe an interaction
between a client and an application or program
Simulate class interactions
Ask What if? questions
Assign responsibilities to each class
There are two types of responsibilities
What a class must know about itself (knowledge)
What a class must be able to do (behavior)
Use case
A collection of scenarios related to a common goal
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Role
playing
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Object-Oriented Design Methodology
Responsibility Algorithms
The algorithms must be written for
the responsibilities
Knowledge responsibilities usually
just return the contents of one of an
objects variables
Action responsibilities are a little
more complicated, often involving
calculations
Responsibilities algorithms are often
decomposed using top-down design
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Relationships Between Classes
Containment
part-of
An address class may be part of the definition
of a student class
Inheritance
Classes can inherit data and behavior from
other classes
is-a

Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Computer Example
Lets work through this problem-solving
process by creating an address list
Brainstorming and filtering
Circling the nouns and underlining the verbs is
a good way to begin
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Computer Example
First pass at a list of
classes
list
name
telephone number
email address
list
order
names
list
scraps
paper
cards
Filtered List
list, name, telephone number, email address
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
CRC Cards
Can you think of any other useful responsibilities?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
CRC Cards
Can you think of any other useful responsibilities?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
CRC Cards
How is this class different from Name and Person?
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Responsibility Algorithms
Person Class

Initialize

name.initialize()
Write "Enter phone number; press return."
Get telephone number
Write "Enter email address; press return."
Get email address

Print

name.print()
Write "Telephone number: " + telephoneNumber
Write "Email address: " + emailAddress

Tells name to initialize itself
Tells name to print itself
Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers
Responsibility Algorithms
Name Class

Initialize

"Enter the first name; press return."
Read firstName
"Enter the last name; press return."
Read lastName

Print

Print "First name: " + firstName
Print "Last name: " + lastName

Tomado de: Dale, N. Weems, C++ Plus Data Structures
4th. Ed. 2007 Instructor material, Jones & Barlett Publishers

You might also like