Your SlideShare is downloading. ×
강의자료10
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

강의자료10

309
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
309
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ADT Sorted List ( 정렬 리스트 ) linked 구조에 의한 구현
  • 2. What is a List?
    • A list is a homogeneous collection of elements, with a linear relationship between elements.
    • That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.
  • 3. ADT Unsorted List Operations
    • Transformers
      • MakeEmpty
      • InsertItem
      • DeleteItem
    • Observers
      • IsFull
      • LengthIs
      • RetrieveItem
    • Iterators
      • ResetList
      • GetNextItem
    change state observe state process all
  • 4. class SortedType<char> MakeEmpty ~SortedType DeleteItem . . . InsertItem SortedType RetrieveItem GetNextItem Private data: length 3 listData currentPos ? ‘ C’ ‘L’ ‘X’
  • 5.
    • template<class ItemType>
    • s truct NodeType
    • {
    • ItemType info;
    • NodeType* next;
    • };
    • template <class ItemType>
    • class SortedType
    • {
    • public : // LINKED LIST IMPLEMENTATION
    • SortedType ( ) ;
    • ~SortedType ( ) ;
    • void MakeEmpty ( ) ;
    • bool IsFull ( ) const ;
    • int LengthIs ( ) const ;
    • void RetrieveItem ( ItemType& item, bool& found ) ;
    • void InsertItem ( ItemType item ) ;
    • void DeleteItem ( ItemType item ) ;
    • void ResetList ( );
    • void GetNextItem ( ItemType& item ) ;
    • private :
    • NodeType <ItemType> * listData;
    • int length;
    • NodeType <ItemType> * currentPos;
    • } ;
  • 6.
    • // LINKED LIST IMPLEMENTATION ( Sorted.cpp )
    • #include “itemtype.h”
    • template <class ItemType>
    • SortedType<ItemType>::SortedType ( ) // constructor
    • // Pre: None.
    • // Post: List is empty.
    • {
    • length = 0 ;
    • listData = NULL;
    • }
    • template <class ItemType>
    • int SortedType<ItemType>::LengthIs ( ) const
    • // Post: Function value = number of items in the list.
    • {
    • return length;
    • }
  • 7.
    • // LINKED LIST IMPLEMENTATION ( Sorted.cpp )
    • #include “itemtype.h”
    • template <class ItemType>
    • void SortedType<ItemType>::MakeEmpty ( )
    • {
    • NodeType<ItemType>* tempPtr;
    • while(listData != NULL)
    • {
    • tempPtr = listData;
    • listData = listData->next;
    • delete tempPtr;
    • }
    • length = 0;
    • }
  • 8.
    • template <class ItemType>
    • void UnsortedType<ItemType>::RetrieveItem( ItemType& item, bool& found )
    • {
    • bool moreToSearch ;
    • NodeType<ItemType>* location ;
    • location = listData ;
    • found = false ;
    • moreToSearch = ( location != NULL ) ;
    • while ( moreToSearch && !found )
    • {
    • if (location->info .key < item .key ) // advance pointer
    • { location = location->next;
    • moreToSearch = ( location != NULL ) ;
    • }
    • else if (item.key == location->info .key) // match here
    • { found = true;
    • item = location->info;
    • }
    • else
    • moreToSearch = false; }
    • }
  • 9.
    • // LINKED LIST IMPLEMENTATION ( Sorted.cpp )
    • #include “itemtype.h”
    • template <class ItemType>
    • SortedType<ItemType>::~SortedType ( )
    • {
    • NodeType<ItemType>* tempPtr;
    • while(listData != NULL)
    • {
    • tempPtr = listData;
    • listData = listData->next;
    • delete tempPtr;
    • }
    • }
  • 10. InsertItem algorithm for Sorted Linked List
    • Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location.
    • Obtain a node for insertion and place item in it.
    • Insert the node by adjusting pointers.
    • Increment length.
  • 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. The Inchworm Effect
  • 13. Inserting ‘S’ into a Sorted List Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch
  • 14. Finding proper position for ‘S’ Private data: length 3 listData currentPos ? predLoc location NULL ‘ C’ ‘L’ ‘X’ moreToSearch true
  • 15. Finding proper position for ‘S’ Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch true
  • 16. Finding Proper Position for ‘S’ Private data: length 3 listData currentPos ? predLoc location ‘ C’ ‘L’ ‘X’ moreToSearch false
  • 17. Inserting ‘S’ into Proper Position ‘ C’ ‘L’ ‘X’ Private data: length 4 listData currentPos predLoc location moreToSearch false ‘ S’
  • 18.
    • // LINKED LIST IMPLEMENTATION ( Sorted.cpp )
    • template <class ItemType>
    • Void SortedType<ItemType>::InsertItem (ItemType item)
    • { NodeType<ItemType>* newNode;
    • NodeType<ItemType>* predLoc;
    • NodeType<ItemType>* location;
    • location = listData; predLoc = NULL;
    • while(location != NULL)
    • { if (item.key > location->info.key)
    • { predLoc = location;
    • location = location->next;
    • }
    • else
    • break;
    • }
    • newNode = new NodeType<ItemType>;
    • newNode->info = item;
    • if (predLoc == NULL)
    • { newNode->next = listData;
    • listData = newNode;
    • }
    • else
    • { newNode->next = location;
    • predLoc->next = newNode;
    • }
    • length++;
    • }
  • 19.
    • // LINKED LIST IMPLEMENTATION ( Sorted.cpp )
    • template <class ItemType>
    • void SortedType<ItemType>:: Delete Item ( ItemType item )
    • // Pre: 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.
    • { NodeType<ItemType>* location = listData ;
    • NodeType<ItemType>* tempLocation;
    • // Locate node to be deleted.
    • if (item.key == listData->info.key)
    • { tempLocation = location;
    • listData = listData->next;
    • }
    • else
    • {
    • while(!(item.key == (location->next)->info.key))
    • location = location->next;
    • // Delete node at location->next.
    • tempLocation = location->next;
    • location->next = location->next->next;
    • }
    • delete tempLocation;
    • length--;
    • }