1 / 40

ADT Unsorted List

ADT Unsorted List. Chapter 3. Outline. Abstract Data Type Unsorted List Array-based Implementation Linked Implementation Comparison. struct NodeType. #include “ItemType.h” struct NodeType{ ItemType info; NodeType * next; };. Linked Implementation. NodeType * location;

fayl
Télécharger la présentation

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. ADT Unsorted List Chapter 3 Yanjun Li CS2200

  2. Outline • Abstract Data Type • Unsorted List • Array-based Implementation • Linked Implementation • Comparison Yanjun Li CS2200

  3. struct NodeType #include “ItemType.h” struct NodeType{ ItemType info; NodeType * next; }; Yanjun Li CS2200

  4. Linked Implementation NodeType * location; Be sure you Understand the Differences among location, *location, and location->info Yanjun Li CS2200

  5. Linked Implementation Yanjun Li CS2200

  6. // SPECIFICATION FILE ( UnsortedType.h ) #include “ItemType.h” struct NodeType { ItemType info; NodeType *next; }; class UnsortedType// declares a class data type { public : UnsortedType(); ~UnsortedType(); void MakeEmpty(); // 8 public member functions bool IsFull( ) const; bool IsEmpty( ) 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 ); Yanjun Li CS2200

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

  8. Linked Implementation • What should the constructor do? • Set length to 0 • Set listData to NULL • How do you know that a linked list is empty? • listData is NULL • What aboutcurrentPos? • We let ResetList() take care of initializing currentPos You write the constructor Yanjun Li CS2200

  9. Linked Implementation What about the observers IsFull(), IsEmpty() 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 Yanjun Li CS2200

  10. Linked Implementation bool Unsortedtype::IsFull() const { NodeType* location; try { location = new NodeType; delete location; return false; } catch (std::bad_alloc e) { return true; } } Yanjun Li CS2200

  11. 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 if item.ComparedTo(Info(location)) is EQUAL : Set found to true Set item to Info(location) else : Set location to Next(location) Set moreToSearch to (have not examined Info(last)) RetrieveItem( ) Yanjun Li CS2200

  12. Linked Implmentation Yanjun Li CS2200

  13. Linked Implementation Remember the design notation? Node(location) *location Info(location) location->info Next(location) location->next How do yousetlocationtoNext(location)? How do you setInfo(location)tovalue? Yanjun Li CS2200

  14. Linked Implementation // 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. void UnsortedType::RetrieveItem( ItemType& item, bool& found ) { bool moreToSearch; NodeType* location = listData; found = false ; moreToSearch = ( location != NULL ) ; while ( moreToSearch && !found ) { if ( item.ComparedTo(location->info ) == EQUAL) // match here { found = true; item = location->info; } else // advance pointer { location = location->next; moreToSearch = ( location != NULL ); } } } Yanjun Li CS2200

  15. 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; • Put pointer to node in external pointer • listData = location; Yanjun Li CS2200 15

  16. Linked Implementation Location->next=NULL; We forgot something: We must put NULL in the Next position of the first node Yanjun Li CS2200 16

  17. Linked Implementation • How do we add a node to our list? • Get a node using new • location = new NodeType; • Put value into info portion • location->info = Item; • Put node into list … Where? Where should the node go? Does it matter? Yanjun Li CS2200

  18. Linked Implementation Now we must put the two parts together--carefully! Where is the item inserted? Head or End? Why? Yanjun Li CS2200

  19. Liked Implementation These steps must be done in this order! Why? Yanjun Li CS2200

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

  21. Linked Implementation • How do you delete an item from the list? • Find the item • Remove the item Yanjun Li CS2200

  22. tempLocation location tempLocation Yanjun Li CS2200

  23. Linked Implementation NodeType* location = listData; NodeType* tempLocation; // Find the item if (item.ComparedTo(listData->info) == EQUAL) {// item in first location tempLocation = location; listData = listData->next; //move the head of the list } else{ while (item.ComparedTo((location->next)->info) != EQUAL) location = location->next; tempLocation = location->next; location->next = (location ->next)->next; //remove the middle one, connect the two ends } delete tempLocation; length--; Pre: the item is in the list!! Yanjun Li CS2200

  24. tempPtr ‘D’ ‘E’ ‘H’ ‘H’ Linked Implementation void UnsortedType::MakeEmpty() { NodeType *tempPtr; while (listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } length = 0; } listData Why can we just set listData to NULL? Length=0; Yanjun Li CS2200

  25. Linked Implementation ResetList() and GetNextItem() What was currentPos in the array- based implementation? What would be the equivalent in a linked implementation? Yanjun Li CS2200

  26. Linked Implementation void UnsortedType::ResetList() { currentPos = NULL; } void UnsortedType::GetNextItem(ItemType& item) { if (currentPos == NULL) currentPos = listData; else currentPos = currentPos->next; item = currentPos->info; } What is the Precondition & Postcondition on GetNextItem ? Yanjun Li CS2200

  27. Class Destructors • Recall: listData is deallocated when it goes out of scope but what listData points to is not deallocated • Class Destructor • A function that is implicitly called when class object goes out of scope ~UnsortedType(); // Destructor What must this function do? Have we done a similar job? Yanjun Li CS2200

  28. Which One Is Better ? • Array-based Or Linked List? • How to Compare them? how much our implementation costs us in terms of time, space (memory) ! Yanjun Li CS2200

  29. Algorithm Growth Rates • An algorithm’s time requirements can be measured as a function of the problem size N • Number of nodes in a linked list • Size of an array • Algorithm’s growth rate: enables comparison of one algorithm with another • Algorithm efficiency is typically a concern for large problems only Yanjun Li CS2200

  30. The Story of Big O Big O (order of magnitude) is a measure of the “worst-case” scenario for a given algorithm taking in input of size N. Mathematically: Given an input of size N (where N is really really really big), what is the asymptotic upper bounds of the algorithm in terms of time/space (how long will it take to execute/how much memory will it require)? Yanjun Li CS2200

  31. Algorithm Growth Rates Figure 9-1 Time requirements as a function of the problem size n • Algorithm A requires time proportional to n2 • Algorithm B requires time proportional to n Yanjun Li CS2200

  32. O(?) There are a few common notations for Big-O. These are categories into which almost all algorithms fit into (once we throw away all the garbage and reduce the algorithm to its purest form). These are denoted as follows: • O(N M) • O(M N) • Or some combination of these. • O(1) • O(log2 N) • O(N) Yanjun Li CS2200

  33. O(1): What A Dream! • O(1) doesn’t mean • we finish everything in one second or one CPU cycle • use one-element array or linked list • We just wish that it did Yanjun Li CS2200

  34. O(1): What It Is About • O(1) means constant time or space. No matter what we throw at a particular algorithm, we know EXACTLY how long/how much it will take to execute the program: • Given an array of size N (where N is unknown), access a particular value. • Given a list of size P (where P is unknown), retrieve the first element of the list. Yanjun Li CS2200

  35. O(N): Linear time/space • This one’s pretty easy to see. • If we have a list containing N elements, and we are searching for a particular value that isn’t even contained in the list, how many elements do we have to search through? N elements! Yanjun Li CS2200

  36. Space • how much memory space is needed: • Array-based: max. size of the array. O(NMax.) • Linked-List: number of elements actually in the list at run time . O(n) • However, each node element of linked list is larger than array element because the pointer to next member is stored as well. Yanjun Li CS2200

  37. Time Which array-based operations are O(1)? Which linked operations are O(1)? Which array-based operations are O(N)? Which linked operations are O(N)? Can you say which implementation is better? Yanjun Li CS2200

  38. Array-based IsFull() GetLength() IsEmpty() InsertItem() MakeEmpty() ResetList() GetNextItem() Linked IsFull() GetLength() IsEmpty() InsertItem() ResetList() GetNextItem() Time - O(1) Yanjun Li CS2200

  39. Array-based RetrieveItem() DeleteItem() Linked RetrieveItem() DeleteItem() MakeEmpty() Time – O(N) Yanjun Li CS2200

  40. Reference • Reproduced from C++ Plus Data Structures, 4th edition by Nell Dale. • Reproduced by permission of Jones and Bartlett Publishers International. Yanjun Li CS2200

More Related