강의자료7

404 views
313 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
404
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

강의자료7

  1. 1. What is a List? <ul><li>A list is a homogeneous collection of elements, with a linear relationship between elements. </li></ul><ul><li>That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor. </li></ul>
  2. 2. ADT Unsorted List Operations <ul><li>Transformers </li></ul><ul><ul><li>MakeEmpty </li></ul></ul><ul><ul><li>InsertItem </li></ul></ul><ul><ul><li>DeleteItem </li></ul></ul><ul><li>Observers </li></ul><ul><ul><li>IsFull </li></ul></ul><ul><ul><li>LengthIs </li></ul></ul><ul><ul><li>RetrieveItem </li></ul></ul><ul><li>Iterators </li></ul><ul><ul><li>ResetList </li></ul></ul><ul><ul><li>GetNextItem </li></ul></ul>change state observe state process all
  3. 3. <ul><li>// SPECIFICATION FILE ( unsorted.h ) </li></ul><ul><li>class UnsortedType // declares a class data type </li></ul><ul><li>{ </li></ul><ul><li>public : // 8 public member functions </li></ul><ul><li>…… </li></ul><ul><li> </li></ul><ul><li>private : // 3 private data members </li></ul><ul><li>// 리스트를 배열로 표현 </li></ul><ul><li>int length ; </li></ul><ul><li>ItemType info[MAX_ITEMS] ; // MAX_ITEMS 은 상수임 </li></ul><ul><li>int currentPos ; </li></ul><ul><li>// Linked List 구현 </li></ul><ul><li>private : </li></ul><ul><li>NodeType* listData; </li></ul><ul><li>int length; </li></ul><ul><li>NodeType* currentPos; </li></ul><ul><li>} ; </li></ul>ADT Unsorted List 의 Linked 리스트 구현 struct ItemType { … . KeyType key; … };
  4. 4. class UnsortedType MakeEmpty ~UnsortedType DeleteItem . . . InsertItem UnsortedType RetrieveItem GetNextItem ‘ X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ?
  5. 5. Linked Implementation 다음 세 가지의 차이점 ? location , *location , 과 location->info
  6. 6. <ul><li>s truct NodeType </li></ul><ul><li>{ </li></ul><ul><li>ItemType info; </li></ul><ul><li>NodeType* next; </li></ul><ul><li>}; </li></ul><ul><li>class UnsortedType </li></ul><ul><li>{ </li></ul><ul><li>public : // LINKED LIST IMPLEMENTATION </li></ul><ul><li>UnsortedType ( ) ; </li></ul><ul><li>~UnsortedType ( ) ; </li></ul><ul><li>void MakeEmpty ( ) ; </li></ul><ul><li>bool IsFull ( ) const ; </li></ul><ul><li>int LengthIs ( ) const ; </li></ul><ul><li>void RetrieveItem ( ItemType& item, bool& found ) ; </li></ul><ul><li>void InsertItem ( ItemType item ) ; </li></ul><ul><li>void DeleteItem ( ItemType item ) ; </li></ul><ul><li>void ResetList ( ); </li></ul><ul><li>void GetNextItem ( ItemType& item ) ; </li></ul><ul><li>private : </li></ul><ul><li>NodeType* listData; </li></ul><ul><li>int length; </li></ul><ul><li>NodeType* currentPos; </li></ul><ul><li>} ; </li></ul>
  7. 7. <ul><li>// LINKED LIST IMPLEMENTATION ( unsorted.cpp ) </li></ul><ul><li>#include “itemtype.h” </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>UnsortedType<ItemType>::UnsortedType ( ) // constructor </li></ul><ul><li>// Pre: None. </li></ul><ul><li>// Post: List is empty. </li></ul><ul><li>{ </li></ul><ul><li>length = 0 ; </li></ul><ul><li>listData = NULL; </li></ul><ul><li>} </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>int UnsortedType<ItemType>::LengthIs ( ) const </li></ul><ul><li>// Post: Function value = number of items in the list. </li></ul><ul><li>{ </li></ul><ul><li>return length; </li></ul><ul><li>} </li></ul>
  8. 8. Linked Implementation: RetrieveItem
  9. 9. <ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>::RetrieveItem( ItemType& item, bool& found ) </li></ul><ul><li>// Pre: Key member of item is initialized. </li></ul><ul><li>// Post: If found, item’s key matches an element’s key in the list </li></ul><ul><li>// and a copy of that element has been stored in item; otherwise, </li></ul><ul><li>// item is unchanged. </li></ul><ul><li>{ bool moreToSearch ; </li></ul><ul><li>NodeType<ItemType>* location ; </li></ul><ul><li>location = listData ; </li></ul><ul><li>found = false ; </li></ul><ul><li>moreToSearch = ( location != NULL ) ; </li></ul><ul><li>while ( moreToSearch && !found ) </li></ul><ul><li>{ if ( item .key == location->info .key ) // match here </li></ul><ul><li>{ found = true ; </li></ul><ul><li> item = location->info ; </li></ul><ul><li> } </li></ul><ul><li> else // advance pointer </li></ul><ul><li> { location = location->next ; </li></ul><ul><li> moreToSearch = ( location != NULL ) ; </li></ul><ul><li>} } </li></ul><ul><li>} </li></ul>
  10. 10. <ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>::InsertItem ( ItemType item ) </li></ul><ul><li>// Pre: list is not full and item is not in list. </li></ul><ul><li>// Post: item is in the list; length has been incremented. </li></ul><ul><li>{ </li></ul><ul><li>NodeType<ItemType>* location ; </li></ul><ul><li>// obtain and fill a node </li></ul><ul><li>location = new NodeType<ItemType> ; </li></ul><ul><li>location->info = item ; </li></ul><ul><li>location->next = listData ; </li></ul><ul><li>listData = location ; </li></ul><ul><li>length++ ; </li></ul><ul><li>} </li></ul>
  11. 11. Inserting ‘B’ into an Unsorted List Private data: length 3 listData currentPos ? ‘ X’ ‘C’ ‘L’
  12. 12. location = new NodeType<ItemType>; Private data: length 3 listData currentPos ? item location ‘ B’ ‘ X’ ‘C’ ‘L’
  13. 13. location->info = item ; Private data: length 3 listData currentPos ? <ul><li>item </li></ul><ul><li>location </li></ul>‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  14. 14. location->next = listData ; Private data: length 3 listData currentPos ? <ul><li>item </li></ul><ul><li>location </li></ul>‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  15. 15. listData = location ; Private data: length 3 listData currentPos ? <ul><li>item </li></ul><ul><li>location </li></ul>‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  16. 16. length++ ; Private data: length 4 listData currentPos ? <ul><li>item </li></ul><ul><li>location </li></ul>‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  17. 17. Linked Implementation: DeleteItem <ul><li>How do you delete an item from the list? </li></ul><ul><ul><li>Find the item </li></ul></ul><ul><ul><li>Remove the item </li></ul></ul>
  18. 18. <ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>:: Delete Item ( ItemType item ) </li></ul><ul><li>// Pre: An element in the list has a key that matches item’s. </li></ul><ul><li>// Post: No element in the list has a key that matches item’s. </li></ul><ul><li>{ NodeType<ItemType>* location = listData ; </li></ul><ul><li>NodeType<ItemType>* tempLocation; </li></ul><ul><li>// Locate node to be deleted. </li></ul><ul><li>if (item.key == listData->info.key) </li></ul><ul><li>{ tempLocation = location; </li></ul><ul><li>listData = listData->next; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ while(!(item.key == (location->next)->info.key)) </li></ul><ul><li>location = location->next; </li></ul><ul><li>// Delete node at location->next. </li></ul><ul><li>tempLocation = location->next; </li></ul><ul><li>location->next = location->next->next; </li></ul><ul><li>} </li></ul><ul><li>delete tempLocation; </li></ul><ul><li>length--; </li></ul><ul><li>} </li></ul>
  19. 19. <ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>::ResetList() </li></ul><ul><li>{ </li></ul><ul><li>currentPos = NULL; </li></ul><ul><li>} </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>::GetNextItem(ItemType& item) </li></ul><ul><li>{ </li></ul><ul><li>if (currentPos == NULL) </li></ul><ul><li>currentPos = listData; </li></ul><ul><li>else </li></ul><ul><li>currentPos = currentPos->next; </li></ul><ul><li>item = currentPos->info; </li></ul><ul><li>} </li></ul>
  20. 20. <ul><li>template <class ItemType> </li></ul><ul><li>UnsortedType<ItemType>::~UnsortedType() </li></ul><ul><li>{ </li></ul><ul><li>NodeType<ItemType>* tempPtr; </li></ul><ul><li>while(listData != NULL) </li></ul><ul><li>{ </li></ul><ul><li>tempPtr = listData; </li></ul><ul><li>listData = listData->next; </li></ul><ul><li>delete tempPtr; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×