640 likes | 661 Views
Explore the Abstract Data Type Unsorted List and learn to implement utility routines for common tasks. Compare array-based and linked implementations in terms of Big-O approximations. Understand the concepts of Transformers, Observers, Iterators, and common vocabulary in ADT Unsorted List. Utilize ItemType and constructors to manipulate generic list items efficiently. Dive into array-based implementation details and list manipulation techniques.
E N D
Fall 2010 Chapter 3 ADT Unsorted List 1
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
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
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
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
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?
ADT Unsorted List • Transformers • MakeEmpty • InsertItem • DeleteItem • Observers • IsFull • GetLength • RetrieveItem • Iterators • ResetList • GetNextItem change state observe state process all
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
// 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
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)
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?
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?
Array-Based Implementation Array-based implementation Notice the difference between the array and the list stored in the array
Array-Based Implementation • What should the constructor do? • UnsortedType::UnsortedType() • { • length = 0; • } 14
Array-Based Implementation • What is a full list? An empty list? • bool UnsortedType::IsFull() • { • return (length == MAX_ITEMS); • } • bool UnsortedType::IsEmpty() • { • return (length == 0); • } 15
Array-Based Implementation length 3 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] . . . [MAX_ITEMS-1] insert(“John"); If the list is unsorted, where should the next element go ? 16
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] How is this implemented in code ?
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
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
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
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; • } • } • }
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . [MAX_ITEMS-1] moreToSearch: true found: false location: 0 Search for Anderson 22
Array-Based Implementation moreToSearch: true found: false location: 1 length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . [MAX_ITEMS-1] 23
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] moreToSearch: true found: false location: 2
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . [MAX_ITEMS-1] moreToSearch: true found: false location: 3
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] moreToSearch: false found: false location: 4
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] What are moreToSearch, location, and found at the end of a search for Bradley ?
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 one slot Replace it with another item What other item? How about the item at info[length-1]?
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?
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] location: 0 Delete Bradley
Array-Based Implementation length 4 info [ 0 ] Maxwell [ 1 ] Bradley [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] location: 1 Key Bradley has been matched 31
Array-Based Implementation length 3 info [ 0 ] Maxwell [ 1 ] John [ 2 ] Ashley [ 3 ] John . . . [MAX_ITEMS-1] location: 1 Copy of last list element is in the position where the key Bradley was before; length has been decremented
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?
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
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 • } ;
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 ?
Linked Implementation Be sure you understand the differences among location, *location, and location->info
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?
Linked Implementation • Private data for the Unsorted List ADT, linked-list implementation • private: • NodeType* listData; • int length; • NodeType* currentPos; • }; List with two items
Linked Implementation • How do you know that a linked list is empty? • listData is NULL • What should the constructor do? • Set length to 0 • Set listData to NULL • What about currentPos? • We let ResetList take care of initializing • currentPos What should the constructor do?
Linked Implementation • What about the observers IsFull and GetLength? • GetLength just returns length • Can a linked list ever be full? • Yes, if you run out of memory • Ask for a new node within a try/catch block
Linked Implementation • bool Unsortedtype::IsFull() const • { • NodeType* location; • try • { • location = new NodeType; • delete location; • return false; • } • catch (std::bad_alloc exception) • { • return true; • } • } What about MakeEmpty?
Linked Implementation • void Unsortedtype::MakeEmpty() • { • NodeType* tempPtr; • while (listData != NULL) • { • tempPtr = listData; • listData = listData->next; • delete tempPtr; • } • length = 0; • } Why can’t we just set listData to NULL?
Linked 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 linked code
Linked Implementation • 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; • NodeType* 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 ); • } • } • } 47
Linked Implementation How do we go about building a linked list? Create a list of one item Get a node using new location = new NodeType Put value into info portion of node location->info = item Save pointer to node in external pointer listData = location 49
Linked Implementation Remember: We must put NULL in the Next position of the first node 50