adt unsorted list outline
play

ADT Unsorted List Outline What is a list? C++ Data Types Simple - PowerPoint PPT Presentation

ADT Unsorted List Outline What is a list? C++ Data Types Simple Composite Integral Floating array struct union class char short int long enum Address float double long double pointer reference Concept: ADT List


  1. ADT Unsorted List

  2. Outline • What is a list?

  3. C++ Data Types Simple Composite Integral Floating array struct union class char short int long enum Address float double long double pointer reference

  4. Concept: ADT List • Abstract Data Type: A data type whose properties (domain and operations) are specified independently of any particular implementation. • ADT list is a homogeneous collection of elements, with linear relationship among elements • each element except the first has a unique predecessor, and each element except the last has a unique successor.) • Each element in list can be of any type: simple, composite, address, or list, … • Often times, element is a struct or a class type • one field/attribute of the struct/class is used as key field (used to search for element, sort elements)

  5. Examples You anticipate a busy day ahead, and decide to write down tasks that you need to finish: Typically you write down things as they occur to you… Check emails and answer urgent emails Make doctor’s appointment Attend a meeting Start on math homework Revise history essay …. Elements (tasks) are arranged in no particular order. This is an Unsorted List.

  6. Examples: cont’d You anticipate a busy day ahead, and decide to write down tasks that you need to finish: Check emails and answer urgent emails Make doctor’s appointment Attend a meeting Start on math homework Revise history essay …. If you assign a priority to each task, and rearrange todo-list in the order of priority, then it’s a sorted list 1 Start on math homework 2 Revise history essay 3 Attend a meeting 5 Make doctor’s appointment 8 Check emails…

  7. Sorted and Unsorted Lists SORTED LIST UNSORTED LIST elements are stored in Elements are placed into sorted order -- the list in no particular numerically or order. alphabetically by e.g., a to-do list elements themselves, or a shopping list by a component/field of element (called a KEY you write them down member) . when something occurs • class list sorted by to you… students last name • to-do list sorted by • priority • shopping list sorted alphabetically

  8. ADT Unsorted List Operations Transformers • MakeEmpty change state • PutItem • DeleteItem Observers • IsFull • GetLength observe state • GetItem Iterators • ResetList process all • GetNextItem

  9. What is a Generic Data Type? • Build ADT Unsorted List as a generic data type , means that items being stored in the list is not specified, i.e., it can be of any type • Advantage: we implement it once, and use it to store different lists: • list of to-dos, list of numbers, list of student records, … • We have used generic data type before: • vector<int>, vector<double>, vector<DateType> … • vector is a generic data type implemented in C++ Standard Template Library

  10. Recall from CS2… • function template • class template template<class T> template<class T> class Pair { void swap (T & x, T & y) { { public: T tmp; Pair (T f, T s); tmp = x; // … x = y; private: y = tmp; T first; } T second; double a[2]={3,4}; } swap (a[1], a[2]); Pair<int> pair1 (3,4);

  11. generic programming without template? For now, we simulate a generic data type without using template: • implement ADT list to store elements of a user- defined class ItemType • User (of ADT list) can define ItemType to encapsulate any type of items as needed • as long as ItemType provides member function to compare two items’ keys • In sample code, member function is named ComparedTo, and returns an enumerated type value LESS , GREATER , or EQUAL . )

  12. ItemType Class Interface Diagram class ItemType ComparedTo Private data Print value Initialize

  13. // SPECIFICATION FILE ( unsorted.h ) #include “ItemType.h” class UnsortedType // declares a class data type { public : // 8 public member functions void UnsortedType ( ); bool IsFull ( ) const; int GetLength ( ) const ; // returns length of list ItemType GetItem ( ItemType item, bool& found); void PutItem ( ItemType item ); void DeleteItem ( ItemType item ); void ResetList ( ); ItemType GetNextItem (); private : // 3 private data members int length; ItemType info[MAX_ITEMS]; int currentPos; };

  14. Class Constructor Rules 1 A constructor cannot return a function value, and has no return value type. 2 A class may have several constructors. The compiler chooses the appropriate constructor by the number and types of parameters used. 3 Constructor parameters are placed in a parameter list in the declaration of the class object. 4 The parameterless constructor is the default constructor. 5 If a class has at least one constructor, and an array of class objects is declared, then one of the constructors must be the default constructor, which is invoked for each element in the array.

  15. Class Interface Diagram UnsortedType class UnsortedType Private data: IsFull length GetLength info [ 0 ] GetItem [ 1 ] [ 2 ] PutItem [MAX_ITEMS-1] DeleteItem this (using static currentPos array) is just one ResetList way to GetNextItem implement it…

  16. // IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp ) #include “itemtype.h” void UnsortedType::UnsortedType ( ) // Pre: None. // Post: List is empty. { length = 0; } void UnsortedType::InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // Post: item is in the list. { info[length] = item; length++; } 14

  17. Before Inserting Hsing into an 
 Unsorted List The item will length 3 be placed into the length location, info [ 0 ] Maxwell and length will be incremented. [ 1 ] Bradley Asad [ 2 ] [ 3 ] . . . [MAX_ITEMS-1]

  18. After Inserting Hsing into an 
 Unsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . . [MAX_ITEMS-1]

  19. int UnsortedType::GetLength( ) const // Pre: List has been inititalized. // Post: Function value == ( number of elements in // list ). { return length; } bool UnsortedType::IsFull ( ) const // Pre: List has been initialized. // Post: Function value == ( list is full ). { return ( length == MAX_ITEMS ); } 17

  20. ItemType UnsortedType::GetItem ( ItemType item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list // and a copy of that element is returned; // otherwise, input item is returned. { bool moreToSearch; int location = 0; found = false; moreToSearch = ( location < length ); while ( moreToSearch && !found ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++; moreToSearch = ( location < length ); case EQUAL : found = true; item = info[ location ]; break; } } return item } 18

  21. Getting Ivan from an Unsorted List length 4 moreToSearch: true found: false info [ 0 ] Maxwell location: 0 [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . [MAX_ITEMS-1]

  22. Getting Ivan from an Unsorted List moreToSearch: true length 4 found: false info [ 0 ] Maxwell location: 1 [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . [MAX_ITEMS-1]

  23. Getting Ivan from an Unsorted List length 4 moreToSearch: true found: false info [ 0 ] Maxwell location: 2 [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . . [MAX_ITEMS-1]

  24. Getting Ivan from an Unsorted List length 4 moreToSearch: true found: false info [ 0 ] Maxwell location: 3 [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . . [MAX_ITEMS-1]

  25. Getting Ivan from an Unsorted List length 4 moreToSearch: false found: false info [ 0 ] Maxwell location: 4 [ 1 ] Bradley Asad [ 2 ] Hsing [ 3 ] . . . [MAX_ITEMS-1]

  26. void UnsortedType::DeleteItem ( ItemType item ) // Pre: item’s key has been inititalized. // An element in the list has a key that matches item’s. // Post: No element in the list has a key that matches item’s. { int location = 0 ; while (item.ComparedTo (info [location] ) != EQUAL ) location++; // move last element into position where item was located info [location] = info [length - 1 ] ; length-- ; }

  27. Deleting Bradley from an Unsorted List length 4 location: 0 info [ 0 ] Maxwell [ 1 ] Bradley Key Bradley has Asad [ 2 ] not been matched. Hsing [ 3 ] . . . [MAX_ITEMS-1]

  28. Deleting Bradley from an Unsorted List length 4 info [ 0 ] Maxwell location: 1 [ 1 ] Bradley Asad [ 2 ] Key Bradley has Hsing [ 3 ] been matched. . . . [MAX_ITEMS-1]

  29. Deleting Bradley from an Unsorted List length 4 location: 1 info [ 0 ] Maxwell [ 1 ] Hsing Asad [ 2 ] Placed copy of last list element Hsing [ 3 ] into the position . where the key Bradley . was before. . [MAX_ITEMS-1]

  30. Deleting Bradley from an 
 Unsorted List length 3 location: 1 info [ 0 ] Maxwell [ 1 ] Hsing Asad [ 2 ] Decremented length. Hsing [ 3 ] . . . [MAX_ITEMS-1]

Recommend


More recommend