강의자료7
TRANSCRIPT
1
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
2
// 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;
} ;
struct ItemType{ …. KeyType key; …};
ADT Unsorted List 의 Linked 리스트 구현
4
class UnsortedType
MakeEmpty
~UnsortedType
DeleteItem . . .
InsertItem
UnsortedType
RetrieveItem
GetNextItem
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
Linked Implementation
5
다음 세 가지의 차이점 ?
location,*location, 과location->info
struct 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;}
7
Linked Implementation: RetrieveItem
8
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 ) ; }
} } 9
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++ ;
}
10
11
Inserting ‘B’ into an Unsorted List
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
12
location = new NodeType<ItemType>;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
13
location->info = item ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
14
location->next = listData ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
15
listData = location ;
‘X’ ‘C’ ‘L’
Private data:
length 3
listData
currentPos ?
item
location
‘B’
‘B’
16
length++ ;
‘X’ ‘C’ ‘L’
Private data:
length 4
listData
currentPos ?
item
location
‘B’
‘B’
Linked Implementation: DeleteItem
17
How do you delete an item from the list?
1. Find the item
2. Remove the item
template <class ItemType>
void UnsortedType<ItemType>::DeleteItem ( 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--;
}
18
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;
}
19
template <class ItemType>
UnsortedType<ItemType>::~UnsortedType()
{
NodeType<ItemType>* tempPtr;
while(listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
20