1 / 54

3 ADT Unsorted List

3 ADT Unsorted List . List Definitions. Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time. List Definitions.

jean
Download Presentation

3 ADT Unsorted List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3 ADT Unsorted List

  2. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor. Length The number of items in a list; the length can vary over time.

  3. List Definitions Unsorted list A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationships. Sorted list A list that is sorted by the value in the key; there is a semantic relationship among the keys of the items in the list. Key The attributes that are used to determine the logical order of the list.

  4. Abstract Data Type (ADT) • A data type whose properties (domain and operations) are specified independently of any particular implementation.

  5. Data from 3 different levels • Application (or user) level: modeling real-life data in a specific context. • Logical (or ADT) level: abstract view of the domain and operations. WHAT • Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW

  6. 4 Basic Kinds of ADT Operations • Constructor -- creates a new instance (object) of an ADT. • Transformer -- changes the state of one or more of the data values of an instance. • Observer -- allows us to observe the state of one or more of the data values of an instance without changing them. • Iterator -- allows us to process all the components in a data structure sequentially. 6

  7. Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) .

  8. change state observe state process all ADT Unsorted List Operations Transformers • MakeEmpty • InsertItem • DeleteItem Observers • IsFull • LengthIs • RetrieveItem Iterators • ResetList • GetNextItem 8

  9. What is a Generic Data Type? A generic data type is a type for which the operations are defined but the types of the items being manipulated are not defined. One way to simulate such a type for our UnsortedList ADT is via a user-defined class ItemType with member function ComparedTo returning an enumerated type value LESS, GREATER, or EQUAL.

  10. // 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 LengthIs ( ) const ; // returns length of list void RetrieveItem ( ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( ); void GetNextItem ( ItemType& item ) ; private : // 3 private data members int length ; ItemType info[MAX_ITEMS] ; int currentPos ; } ; 10

  11. Class Constructor A special member function of a class that is implicitly invoked when a class object is defined.

  12. Class Constructor Rules • A constructor cannot return a function value, and has no return value type. • A class may have several constructors. The compiler chooses the appropriate constructor by the number and types of parameters used. • Constructor parameters are placed in a parameter list in the declaration of the class object. • The parameterless constructor is the default constructor. • 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.

  13. Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos Class Interface Diagram UnsortedType class UnsortedType IsFull LengthIs RetrieveItem InsertItem DeleteItem ResetList GetNextItem

  14. // 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

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

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

  17. int UnsortedType::LengthIs ( ) 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

  18. void UnsortedType::RetrieveItem ( 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 has been stored in item; // otherwise, item is unchanged. { 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 ; } } } 18

  19. Retrieving Ivan from anUnsorted List length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . [MAX_ITEMS-1] moreToSearch: true found: false location: 0 19

  20. Retrieving Ivan from anUnsorted List moreToSearch: true found: false location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . [MAX_ITEMS-1] 20

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

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

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

  24. 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-- ; } 24

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

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

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

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

  29. void UnsortedType::ResetList ( ) // Pre: List has been inititalized. // Post: Current position is prior to first element in list. { currentPos = -1 ; } void UnsortedType::GetNextItem ( ItemType& item ) // Pre: List has been initialized. Current position is defined. // Element at current position is not last in list. // Post: Current position is updated to next position. // item is a copy of element at current position. { currentPos++ ; item = info [currentPos] ; } 29

  30. Specifying class ItemType // SPECIFICATION FILE ( itemtype.h ) const int MAX_ITEM = 5 ; enum RelationType { LESS, EQUAL, GREATER } ; class ItemType// declares class data type { public : // 3 public member functions RelationType ComparedTo ( ItemType ) const ; void Print ( ) const; void Initialize ( int number ) ; private : // 1 private data member int value ; // could be any different type } ;

  31. // IMPLEMENTATION FILE ( itemtype.cpp ) // Implementation depends on the data type of value. #include “itemtype.h” #include <iostream> RelationType ComparedTo ( ItemType otherItem ) const { if ( value < otherItem.value ) return LESS ; else if ( value > otherItem.value ) return GREATER ; else return EQUAL ; } void Print ( ) const { using namespace std; cout << value << endl ; } void Initialize ( int number ) { value = number ; }

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

  33. What is a pointer variable? • A pointer variable is a variable whose value is the address of a location in memory. • To declare a pointer variable, you must specify the type of value that the pointer will point to. For example, int* ptr; // ptr will hold the address of an int char* q;// q will hold the address of a char

  34. Using a pointer variable int x; x = 12; int* ptr; ptr = &x; NOTE: Because ptr holds the address of x, we say that ptr “points to” x 2000 12 x 3000 2000 ptr

  35. Unary operator * is the deference (indirection) operator int x; x = 12; int* ptr; ptr = &x; std::cout << *ptr; NOTE: The value pointed to by ptr is denoted by *ptr 2000 12 x 3000 2000 ptr

  36. Using the dereference operator int x; x = 12; int* ptr; ptr = &x; *ptr = 5; // changes the value // at adddress ptr to 5 2000 12 5 x 3000 2000 ptr

  37. Another Example 4000 A Z ch 5000 6000 4000 4000 q p char ch; ch = ‘A’; char* q; q = &ch; *q = ‘Z’; char* p; p = q; // the right side has value 4000 // now p and q both point to ch

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

  39. The NULL Pointer There is a pointer constant 0 called the “null pointer” denoted by NULL in cstddef. But NULL is not memory address 0. NOTE: It is an error to dereference a pointer whose value is NULL. Such an error may cause your program to crash, or behave erratically. It is the programmer’s job to check for this. while (ptr != NULL) { . . .// ok to use *ptr here }

  40. What is a List? • A list is a homogeneous collection of elements, with a linear relationship between elements. • That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

  41. change state observe state process all ADT Unsorted List Operations Transformers • MakeEmpty • InsertItem • DeleteItem Observers • IsFull • LengthIs • RetrieveItem Iterators • ResetList • GetNextItem 41

  42. #include “ItemType.h” // unsorted.h . . . template <class ItemType> class UnsortedType { public : // LINKED LIST IMPLEMENTATION UnsortedType ( ) ; ~UnsortedType ( ) ; void MakeEmpty () ; bool IsFull ( ) const ; int LengthIs ( ) const ; void RetrieveItem ( ItemType& item, bool& found ) ; void InsertItem ( ItemType item ) ; void DeleteItem ( ItemType item ) ; void ResetList ( ); void GetNextItem ( ItemType& item ) ; private : NodeType<ItemType>* listData; int length; NodeType<ItemType>* currentPos; } ; 42

  43. ‘X’ ‘C’ ‘L’ class UnsortedType<char> UnsortedType Private data: length 3 listData currentPos ? MakeEmpty ~UnsortedType RetrieveItem InsertItem DeleteItem . . . GetNextItem

  44. // LINKED LIST IMPLEMENTATION ( unsorted.cpp ) #include “itemtype.h” template <class ItemType> UnsortedType<ItemType>::UnsortedType ( ) // constructor // Pre: None. // Post: List is empty. { length = 0 ; listData = NULL; } template <class ItemType> int UnsortedType<ItemType>::LengthIs ( ) const // Post: Function value = number of items in the list. { return length; } 44

  45. template <class ItemType> void UnsortedType<ItemType>::RetrieveItem( 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 has been stored in item; otherwise, // item is unchanged. { bool moreToSearch ; NodeType<ItemType>* location ; location = listData ; found = false ; moreToSearch = ( location != NULL ) ; while ( moreToSearch && !found ) { if ( item == location->info ) // match here { found = true ; item = location->info ; } else // advance pointer { location = location->next ; moreToSearch = ( location != NULL ) ; } } } 45

  46. template <class ItemType> void UnsortedType<ItemType>::InsertItem ( ItemType item ) // Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. { NodeType<ItemType>* location ; // obtain and fill a node location = new NodeType<ItemType> ; location->info = item ; location->next = listData ; listData = location ; length++ ; } 46

  47. ‘X’ ‘C’ ‘L’ Inserting ‘B’ into an Unsorted List Private data: length 3 listData currentPos ?

  48. ‘X’ ‘C’ ‘L’ location = new NodeType<ItemType>; item location ‘B’ Private data: length 3 listData currentPos ?

  49. ‘X’ ‘C’ ‘L’ location->info = item ; • item • location ‘B’ ‘B’ Private data: length 3 listData currentPos ?

  50. ‘X’ ‘C’ ‘L’ location->next = listData ; ‘B’ • item • location ‘B’ Private data: length 3 listData currentPos ?

More Related