Академический Документы
Профессиональный Документы
Культура Документы
1
What is a list?
• A list is a homogeneous collection of
elements.
• Linear relationship between elements:
(1) Each element except the first one has
a unique predecessor.
(2) Each element except the last one has
a unique successor.
• Length: the number of items in the list.
2
What is an unsorted list?
• A list in which data items are placed in
no particular order.
5
RetrieveItem (ItemType& item,
Boolean& found)
• Function: Retrieves list element whose key
matches item's key (if present).
• Preconditions:
(1) List has been initialized,
(2) List is not full,
(3) item is not in list.
9
void GetNextItem (ItemType&
item)
• Function: Gets the next element in list.
template<class ItemType>
bool UnsortedType<ItemType>::IsFull() const
{
return (length == MAX_ITEMS);
}
template<class ItemType>
int UnsortedType<ItemType>::LengthIs() const
{
return length;
} 13
14
Unsorted List Implementation
(cont.)
template<class ItemType>
void UnsortedType<ItemType>::RetrieveItem (ItemType& item,
bool& found)
{
int location = 0;
found = false;
while( (location < length) && !found)
if (item == info[location]) {
found = true;
item = info[location];
}
else
location++;
}
15
Unsorted List Implementation
(cont.)
template<class ItemType>
void UnsortedType<ItemType>::InsertItem (ItemType
item)
{
info[length] = item;
length++;
}
16
Unsorted List Implementation
(cont.)
template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item)
{
int location = 0;
while(item != info[location])
location++;
17
18
Unsorted List Implementation
template<class ItemType>
(cont.)
void UnsortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template<class ItemType>
bool UnsortedType<ItemType>::IsLastItem()
{
return(currentPos == length - 1);
}
template<class ItemType>
void UnsortedType<ItemType>::GetNextItem (ItemType& item)
{
currentPos++;
item = info[currentPos];
} 19
20
Write a client function that splits an
unsAorted list into two unsorted lists
using the following specification.
list.ResetList();
while ( !list.IsLastItem()) {
list.GetNextItem(listItem);
if(listItem > item) {
if (!list2.IsFull())
list2.InsertItem(listItem);
}
else {
if ( !list1.IsFull())
list1.InsertItem(listItem);
}
} 22