Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
3 ADTs Unsorted List and Sorted List PowerPoint Presentation
Download Presentation
3 ADTs Unsorted List and Sorted List

3 ADTs Unsorted List and Sorted List

328 Views Download Presentation
Download Presentation

3 ADTs Unsorted List and Sorted List

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 3 ADTs Unsorted List and Sorted List Chapter 3 ADT Unsorted List

  2. Goals • Describe the Abstract Data Type Unsorted List from three perspectives • Use the Unsorted List operations to implement utility routines to do the following application-level tasks: • Print the list of elements • Create a list of elements from a file • Implement the following Unsorted List operations using an array-based implementation • Create and destroy a list • Determine whether the list is full • Insert an element • Retrieve an element • Delete an element

  3. Goals • Write and execute a test plan for an abstract data type • Declare variables of pointer types • Access the variables to which pointers point • Implement the list operations outlined above using a linked implementation • Compare the two implementations of the ADT Unsorted List in terms of Big-O approximations

  4. Lists • 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 • Unsorted list • A list in which data items are placed in no particular order; the only relationships between data elements are the list predecessor and successor relationships

  5. Lists • 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 Name some possible keys

  6. ADT Unsorted List • Abstract Data Type (ADT) • A data type whose properties (domain and operations) are specified independently of any particular implementation Can you think of what operations we should provide for our ADT Unsorted List?

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

  8. ADT Unsorted List Common vocabulary location accesses a particular element Node(location) is all data of element Info(location) is the user's data at location Info(last) user's data at the last location Next(location) is the node following Node(location) Two implementations

  9. // SPECIFICATION FILE ( unsortedType.h ) • #include “ItemType.h” • class UnsortedType // declares a class data type • { • public : // 8 public member functions • UnsortedType(); • void MakeEmpty(); • bool IsFull( ) const; • int GetLength( ) 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 ); Public declarations are the same for either implementation; only private data changes What is ItemType? 9

  10. ADT Unsorted List • Generic data type • A type for which the operations are defined but the types of the items being manipulated are not defined • How can we make the items on the list generic? • List items are of class ItemType, which hasa ComparedTo function that returns (LESS, GREATER, EQUAL)

  11. ADT Unsorted List • Constructor • A special member function of a class • that is implicitly invoked when a class • object is defined • UnsortedType(); Why do you need a constructor if you have an operation MakeEmpty?

  12. Array-Based Implementation Private data members for array-based implementation private int length; ItemType info[MAX_ITEMS]; int currentPos; }; Where does MAX_ITEMS come from?

  13. Array-Based Implementation Array-based implementation Notice the difference between the array and the list stored in the array

  14. Array-Based Implementation • What should the constructor do? • UnsortedType::UnsortedType() • { • length = 0; • } 14

  15. Array-Based Implementation • What is a full list? An empty list? • bool UnsortedType::IsFull() • { • return (length == MAX_ITMES); • } • bool UnsortedType::IsEmpty() • { • return (length == 0); • } 15

  16. Array-Based Implementation length 3 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] . . . [MAX_ITEMS-1] insert("Hsing"); If the list is unsorted, where should the next element go ? 16

  17. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] That was easy! Can you code it ? 17

  18. Array-Based Implementation • void UnsortedType::InsertItem(ItemType item) • // Post: item is in the list. • { • info[length] = item; • length++; • } • How would you go about finding an item in the list? • Cycle through the list looking for the item

  19. Array-Based Implementation What are the two ending cases? The item is found The item is not in the list How do we compare items? We use function ComparedTo in class ItemType

  20. Array-Based Implementation • Initialize location to position of first item • Set found to false • Set moreToSearch to (have not examined Info(last)) • while moreToSearch AND NOT found • switch (item.ComparedTo(Info(location))) • case LESS : • case GREATER : • Set location to Next(location) • Set moreToSearch to (have not examined Info(last)) • case EQUAL : • Set found to true • Set item to Info(location) Replace bold general statements with array-based code

  21. Array-Based Implementation • void UnsortedType::RetrieveItem(ItemType& item, bool& found) • // Pre: Key member(s) 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); • break; • case EQUAL : found = true; • item = info[location]; • break; • } • } • }

  22. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . [MAX_ITEMS-1] moreToSearch: true found: false location: 0 Search for Anderson 22

  23. Array-Based Implementation moreToSearch: true found: false location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . [MAX_ITEMS-1] 23

  24. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] moreToSearch: true found: false location: 2 24

  25. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . [MAX_ITEMS-1] moreToSearch: true found: false location: 3 25

  26. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] moreToSearch: false found: false location: 4 26

  27. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] What are moreToSearch, location, and found at the end of a search for Bradley ? 27

  28. Array-Based Implementation How do you delete an item? First you find the item Yes, but how do you delete it? Move those below it up on slot Replace it with another item What other item? How about the item at info[length-1]?

  29. Array-Based Implementation 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-- ; } Why don't we have to check for end of list?

  30. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] location: 0 Delete Bradley 30

  31. Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] location: 1 Key Bradley has been matched 31

  32. Array-Based Implementation length 3 info [ 0 ] Maxwell [ 1 ] Hsing [ 2 ] Asad [ 3 ] Hsing . . . [MAX_ITEMS-1] location: 1 Copy of last list element is in the position where the key Bradley was before; length has been decremented 32

  33. Array-Based Implementation • void PrintList(ofstream& dataFile, UnsortedType list) • // Pre: list has been initialized. • // dataFile is open for writing. • // Post: Each component in list has been written. • // dataFile is still open. • { • int length; • ItemType item; • list.ResetList(); • length = list.GetLength(); • for (int counter = 1; counter <= length; counter++) • { • list.GetNextItem(item); • item.Print(dataFile); • } • } How do ResetList and GetNextItem work?

  34. Array-Based Implementation • void UnsortedType::ResetList ( ) • // Pre: List has been inititalized. • // Post: Current position is prior to first element. • { • 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] ; • } 34

  35. 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 type • } ;

  36. Class ItemType • // 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; • } How would this class change if the items on the list were strings rather than integers ?

  37. UML Diagram

  38. Pointer Types • Pointer variable • A variable whose value is the address of a location in memory int* intPointer

  39. Pointer Types int alpha; int* intPointer; intPointer = &alpha; If alpha is at address 33, memory looks like this Say again??

  40. Pointer Types • 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

  41. Pointer Types • Dereference operator (*) • An operator that, when applied to a pointer variable, denotes the variable to which the pointer points • Dynamic allocation (new operator) • Allocation of memory space for a variable at runt time (as opposed to static allocation at compile time)

  42. Pointer Types • int x; • x = 12; • int* ptr; • ptr = &x; • std::cout << *ptr; • *ptr is the value in the place to which ptr points 2000 12 x 3000 2000 ptr

  43. Pointer Types • 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

  44. Pointer Types 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

  45. Pointer Types intPointer = new int;

  46. Pointer Types • NULL Pointer • A pointer that points to nothing; available in cstddef • Memory Leak • The loss of available memory space that occurs when memory is allocated dynamically but never deallocated • Garbage • Memory locations that can no longer be accessed

  47. Linked Implementation

  48. Linked Implementation Be sure you understand the differences among location, *location, and location->info

  49. Linked Implementation Remember the design notation? Node(location) *location Info(location) location->info Next(location) location->next How do youset location to Next (location)? How do you setInfo(location) to value?

  50. Linked Implementation • Private data for the Unsorted List ADT, linked-list implementation • private: • NodeType* listData; • int length; • NodeType* currentPos; • }; List with two items