Upcoming SlideShare
×

# 강의자료10

513 views
450 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
513
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 강의자료10

1. 1. ADT Sorted List ( 정렬 리스트 ) linked 구조에 의한 구현
2. 2. 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>
3. 3. 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
4. 4. class SortedType<char> MakeEmpty ~SortedType DeleteItem . . . InsertItem SortedType RetrieveItem GetNextItem Private data: length 3 listData currentPos ? ‘ C’ ‘L’ ‘X’
5. 5. <ul><li>template<class ItemType> </li></ul><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>template <class ItemType> </li></ul><ul><li>class SortedType </li></ul><ul><li>{ </li></ul><ul><li>public : // LINKED LIST IMPLEMENTATION </li></ul><ul><li>SortedType ( ) ; </li></ul><ul><li>~SortedType ( ) ; </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 <ItemType> * listData; </li></ul><ul><li>int length; </li></ul><ul><li>NodeType <ItemType> * currentPos; </li></ul><ul><li>} ; </li></ul>
6. 6. <ul><li>// LINKED LIST IMPLEMENTATION ( Sorted.cpp ) </li></ul><ul><li>#include “itemtype.h” </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>SortedType<ItemType>::SortedType ( ) // 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 SortedType<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>
7. 7. <ul><li>// LINKED LIST IMPLEMENTATION ( Sorted.cpp ) </li></ul><ul><li>#include “itemtype.h” </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>void SortedType<ItemType>::MakeEmpty ( ) </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>length = 0; </li></ul><ul><li>} </li></ul>
8. 8. <ul><li>template <class ItemType> </li></ul><ul><li>void UnsortedType<ItemType>::RetrieveItem( ItemType& item, bool& found ) </li></ul><ul><li>{ </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>{ </li></ul><ul><li>if (location->info .key < item .key ) // advance pointer </li></ul><ul><li>{ location = location->next; </li></ul><ul><li> moreToSearch = ( location != NULL ) ; </li></ul><ul><li> } </li></ul><ul><li> else 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 </li></ul><ul><li>moreToSearch = false; } </li></ul><ul><li>} </li></ul>
9. 9. <ul><li>// LINKED LIST IMPLEMENTATION ( Sorted.cpp ) </li></ul><ul><li>#include “itemtype.h” </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>SortedType<ItemType>::~SortedType ( ) </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>
10. 10. InsertItem algorithm for Sorted Linked List <ul><li>Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. </li></ul><ul><li>Obtain a node for insertion and place item in it. </li></ul><ul><li>Insert the node by adjusting pointers. </li></ul><ul><li>Increment length. </li></ul>
11. 11. Implementing SortedType member function InsertItem // LINKED LIST IMPLEMENTATION (sorted.cpp) #include “ItemType.h” template <class ItemType> void SortedType<ItemType> :: InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member. // Post: item is in the list. List is still sorted. { . . . }
12. 12. The Inchworm Effect
13. 13. Inserting ‘S’ into a Sorted List Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch
14. 14. Finding proper position for ‘S’ Private data: length 3 listData currentPos ? predLoc location NULL ‘ C’ ‘L’ ‘X’ moreToSearch true
15. 15. Finding proper position for ‘S’ Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch true
16. 16. Finding Proper Position for ‘S’ Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch false
17. 17. Inserting ‘S’ into Proper Position ‘ C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location moreToSearch false ‘ S’
18. 18. <ul><li>// LINKED LIST IMPLEMENTATION ( Sorted.cpp ) </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>Void SortedType<ItemType>::InsertItem (ItemType item) </li></ul><ul><li>{ NodeType<ItemType>* newNode; </li></ul><ul><li>NodeType<ItemType>* predLoc; </li></ul><ul><li>NodeType<ItemType>* location; </li></ul><ul><li>location = listData; predLoc = NULL; </li></ul><ul><li>while(location != NULL) </li></ul><ul><li>{ if (item.key > location->info.key) </li></ul><ul><li>{ predLoc = location; </li></ul><ul><li>location = location->next; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul><ul><li>newNode = new NodeType<ItemType>; </li></ul><ul><li>newNode->info = item; </li></ul><ul><li>if (predLoc == NULL) </li></ul><ul><li>{ newNode->next = listData; </li></ul><ul><li>listData = newNode; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ newNode->next = location; </li></ul><ul><li>predLoc->next = newNode; </li></ul><ul><li>} </li></ul><ul><li>length++; </li></ul><ul><li>} </li></ul>
19. 19. <ul><li>// LINKED LIST IMPLEMENTATION ( Sorted.cpp ) </li></ul><ul><li>template <class ItemType> </li></ul><ul><li>void SortedType<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>{ </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>