Propedéutico de Programación Coordinación de Ciencias Computacionales 9/13 Material preparado por: Dra. Pilar Gómez Gil
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?
ADT Unsorted List Operations Transformers MakeEmpty InsertItem DeleteItem Observers IsFull GetLength RetrieveItem Iterators ResetList GetNextItem change state observe state process all
ADT Sorted List 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
La inserción 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?
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?
Linked Implementation We need a trailing pointer
Inserting S into a Sorted List predloc location Private data: length 3 listdata C L X currentpos? moretosearch
Finding proper position for S predloc NULL location Private data: length 3 listdata C L X currentpos? moretosearch true
Finding proper position for S predloc location Private data: length 3 listdata C L X currentpos? moretosearch true
Finding Proper Position for S predloc location Private data: length 3 listdata C L X currentpos? moretosearch false
Inserting S into Proper Position predloc location Private data: length 4 listdata C L X currentpos moretosearch false S
Ver sorted.h Ver sorted.cpp
Linked Implementation Does DeleteItem have to be changed?
Object-Oriented Design Methodology Object-oriented design decomposes a problem into classes Four stages to the decomposition process Brainstorming Filtering Scenarios Responsibility algorithms
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
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 don t belong in the problem solution
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
Object-Oriented Design Methodology Role playing
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 object s variables Action responsibilities are a little more complicated, often involving calculations Responsibilities algorithms are often decomposed using top-down design
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
Computer Example Let s 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
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
CRC Cards Can you think of any other useful responsibilities?
CRC Cards Can you think of any other useful responsibilities?
CRC Cards How is this class different from Name and Person?
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 Tells name to initialize itself Tells name to print itself name.print() Write "Telephone number: " + telephonenumber Write "Email address: " + emailaddress
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