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.
ADT Unsorted List Operations Transformers   MakeEmpty  InsertItem  DeleteItem Observers  IsFull LengthIs RetrieveItem   Iterators  ResetList  GetNextItem change state observe state process all
//  SPECIFICATION FILE ( unsorted.h ) class  UnsortedType // declares a class data type { public :  //  8 public member functions ……   private :  //  3 private data members //  리스트를 배열로 표현 int  length ;  ItemType info[MAX_ITEMS] ;  //  MAX_ITEMS 은 상수임   int currentPos ; // Linked List  구현 private : NodeType*   listData; int  length; NodeType*  currentPos; } ; ADT Unsorted List 의  Linked  리스트 구현 struct ItemType { … . KeyType key; … };
class UnsortedType MakeEmpty ~UnsortedType  DeleteItem . . . InsertItem UnsortedType RetrieveItem GetNextItem ‘ X’  ‘C’  ‘L’ Private data: length  3 listData currentPos  ?
Linked Implementation 다음 세 가지의 차이점 ? location , *location ,   과 location->info
s truct NodeType  { ItemType  info; NodeType* next; };   class  UnsortedType { public :    //  LINKED LIST IMPLEMENTATION UnsortedType ( ) ; ~UnsortedType ( ) ; 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*   listData; int  length; NodeType*  currentPos; } ;
// LINKED LIST IMPLEMENTATION  ( unsorted.cpp ) #include “itemtype.h” template <class ItemType>   UnsortedType<ItemType>::UnsortedType ( )  // constructor //  Pre: None. // Post: List is empty. {  length  =  0 ; listData = NULL; } template <class ItemType> int  UnsortedType<ItemType>::LengthIs (  )  const // Post:  Function value = number of items in the list. { return  length; }
Linked Implementation: RetrieveItem
template <class ItemType>   void  UnsortedType<ItemType>::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<ItemType>*  location ; location = listData ; found = false ; moreToSearch = ( location  !=  NULL ) ; while ( moreToSearch  &&  !found )  {  if ( item .key   == location->info .key  )  // match here {  found = true ;   item  = location->info ;   }   else   // advance pointer    {  location = location->next ;   moreToSearch = ( location  !=  NULL ) ; }  }  }
template <class ItemType>   void UnsortedType<ItemType>::InsertItem ( ItemType  item )  //  Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. {  NodeType<ItemType>*  location ; // obtain and fill a node location = new  NodeType<ItemType> ; location->info = item ; location->next = listData ; listData = location ; length++ ; }
Inserting ‘B’ into an Unsorted List Private data: length  3 listData currentPos  ? ‘ X’  ‘C’  ‘L’
location = new  NodeType<ItemType>; Private data: length  3 listData currentPos  ? item location ‘ B’ ‘ X’  ‘C’  ‘L’
location->info  =  item ; Private data: length  3 listData currentPos  ? item location ‘ B’ ‘ B’ ‘ X’  ‘C’  ‘L’
location->next  =  listData ; Private data: length  3 listData currentPos  ? item location ‘ B’ ‘ B’ ‘ X’  ‘C’  ‘L’
listData  =  location ; Private data: length  3 listData currentPos  ? item location ‘ B’ ‘ B’ ‘ X’  ‘C’  ‘L’
length++ ; Private data: length  4 listData currentPos  ? item location ‘ B’ ‘ B’ ‘ X’  ‘C’  ‘L’
Linked Implementation: DeleteItem How do you delete an item from the list? Find the item Remove the item
template <class ItemType>   void UnsortedType<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--; }
template <class ItemType>   void UnsortedType<ItemType>::ResetList() { currentPos = NULL; } template <class ItemType>   void UnsortedType<ItemType>::GetNextItem(ItemType& item) { if (currentPos == NULL) currentPos = listData; else currentPos = currentPos->next; item = currentPos->info; }
template <class ItemType>   UnsortedType<ItemType>::~UnsortedType() { NodeType<ItemType>* tempPtr; while(listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } }

강의자료7

  • 1.
    What is aList? 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.
  • 2.
    ADT Unsorted ListOperations Transformers MakeEmpty InsertItem DeleteItem Observers IsFull LengthIs RetrieveItem Iterators ResetList GetNextItem change state observe state process all
  • 3.
    // SPECIFICATIONFILE ( unsorted.h ) class UnsortedType // declares a class data type { public : // 8 public member functions …… private : // 3 private data members // 리스트를 배열로 표현 int length ; ItemType info[MAX_ITEMS] ; // MAX_ITEMS 은 상수임 int currentPos ; // Linked List 구현 private : NodeType* listData; int length; NodeType* currentPos; } ; ADT Unsorted List 의 Linked 리스트 구현 struct ItemType { … . KeyType key; … };
  • 4.
    class UnsortedType MakeEmpty~UnsortedType DeleteItem . . . InsertItem UnsortedType RetrieveItem GetNextItem ‘ X’ ‘C’ ‘L’ Private data: length 3 listData currentPos ?
  • 5.
    Linked Implementation 다음세 가지의 차이점 ? location , *location , 과 location->info
  • 6.
    s truct NodeType { ItemType info; NodeType* next; }; class UnsortedType { public : // LINKED LIST IMPLEMENTATION UnsortedType ( ) ; ~UnsortedType ( ) ; 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* listData; int length; NodeType* currentPos; } ;
  • 7.
    // LINKED LISTIMPLEMENTATION ( unsorted.cpp ) #include “itemtype.h” template <class ItemType> UnsortedType<ItemType>::UnsortedType ( ) // constructor // Pre: None. // Post: List is empty. { length = 0 ; listData = NULL; } template <class ItemType> int UnsortedType<ItemType>::LengthIs ( ) const // Post: Function value = number of items in the list. { return length; }
  • 8.
  • 9.
    template <class ItemType> void UnsortedType<ItemType>::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<ItemType>* location ; location = listData ; found = false ; moreToSearch = ( location != NULL ) ; while ( moreToSearch && !found ) { if ( item .key == location->info .key ) // match here { found = true ; item = location->info ; } else // advance pointer { location = location->next ; moreToSearch = ( location != NULL ) ; } } }
  • 10.
    template <class ItemType> void UnsortedType<ItemType>::InsertItem ( ItemType item ) // Pre: list is not full and item is not in list. // Post: item is in the list; length has been incremented. { NodeType<ItemType>* location ; // obtain and fill a node location = new NodeType<ItemType> ; location->info = item ; location->next = listData ; listData = location ; length++ ; }
  • 11.
    Inserting ‘B’ intoan Unsorted List Private data: length 3 listData currentPos ? ‘ X’ ‘C’ ‘L’
  • 12.
    location = new NodeType<ItemType>; Private data: length 3 listData currentPos ? item location ‘ B’ ‘ X’ ‘C’ ‘L’
  • 13.
    location->info = item ; Private data: length 3 listData currentPos ? item location ‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  • 14.
    location->next = listData ; Private data: length 3 listData currentPos ? item location ‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  • 15.
    listData = location ; Private data: length 3 listData currentPos ? item location ‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  • 16.
    length++ ; Privatedata: length 4 listData currentPos ? item location ‘ B’ ‘ B’ ‘ X’ ‘C’ ‘L’
  • 17.
    Linked Implementation: DeleteItemHow do you delete an item from the list? Find the item Remove the item
  • 18.
    template <class ItemType> void UnsortedType<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--; }
  • 19.
    template <class ItemType> void UnsortedType<ItemType>::ResetList() { currentPos = NULL; } template <class ItemType> void UnsortedType<ItemType>::GetNextItem(ItemType& item) { if (currentPos == NULL) currentPos = listData; else currentPos = currentPos->next; item = currentPos->info; }
  • 20.
    template <class ItemType> UnsortedType<ItemType>::~UnsortedType() { NodeType<ItemType>* tempPtr; while(listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; } }