コード例 #1
0
void SortedType::RetreiveItem(ItemType& item, bool& found)
{
    int midPoint, first = 0, last = length - 1;
    bool moreToSearch = (first <= last);
    found = false;
    
    while(moreToSearch && !found)
    {
        midPoint = (first + last) / 2;
        switch(item.ComparedTo(info[midPoint]))
        {
            case LESS:
                last = midPoint - 1;
                moreToSearch = (first <= last);
                break;
            case GREATER:
                first = midPoint + 1;
                moreToSearch = (first <= last);
                break;
            case EQUAL:
                found = true;
                item = info[midPoint];
                break;
        }
    }
}
コード例 #2
0
void SortedType::InsertItem(ItemType item)
{
    int location = 0;
    bool moreToSearch = (location < length);
    
    while(moreToSearch)
    {
        switch(item.ComparedTo(info[location]))
        {
            case LESS:
                moreToSearch = false;
                break;
                
            case GREATER:
                location++;
                moreToSearch = (location < length);
                break;
        }
    }
    
    for(int index = length; index > location; index--)
    {
        info[index] = info[index - 1];
    }
    info[location] = item;
    length++;
}
コード例 #3
0
ファイル: sorted.cpp プロジェクト: Just-Sieb/School
ItemType SortedType::GetItem(ItemType item, bool& found)
{
    bool moreToSearch;
    NodeType* location;

    location = listData;
    found = false;
    moreToSearch = (location != NULL);

    while (moreToSearch && !found)
    {
        switch (item.ComparedTo(location->info))
        {
            case LESS    : moreToSearch = false;
                           break;

            case GREATER : location = location->next;
                           moreToSearch = (location != NULL);
                           break;
                           
            case EQUAL   : found = true;
                           item = location->info;
                           break;
        }
    }
    return item;
}
コード例 #4
0
ファイル: sorted.cpp プロジェクト: Just-Sieb/School
void SortedType::DeleteItem(ItemType item)
{
    NodeType* location = listData;
    NodeType* tempLocation;
    if(item.ComparedTo(listData->info) == EQUAL)
    {
        tempLocation = location;
        listData = listData->next;
    }
    else
    {
        while(item.ComparedTo((location->next)->info) != EQUAL)
        {
            location = location->next;
        }
        tempLocation = location->next;
        location->next = (location->next)->next;
    }
    delete tempLocation;
    length--;
}
コード例 #5
0
void SortedType::DeleteItem(ItemType item)
{
    int location = 0;
    
    while(item.ComparedTo(info[location]) != EQUAL)
    {
        location++;
    }
    
    for(int index = location + 1; index< length; index++)
    {
        info[index - 1] = info[index];
    }
    length--;
}
コード例 #6
0
ファイル: sorted.cpp プロジェクト: Just-Sieb/School
void SortedType::PutItem(ItemType item)
{
    NodeType* newNode;
    NodeType* predLoc;
    NodeType* location;
    bool moreToSearch;

    location = listData;
    predLoc = NULL;
    moreToSearch = (location != NULL);

    while(moreToSearch)
    {
        switch(item.ComparedTo(location->info))
        {
            case GREATER : predLoc = location;
                           location = location->next;
                           moreToSearch = (location != NULL);
                           //std::cout << "We are in greater\n";
                           break;

            case EQUAL   : predLoc = location;
                           location = location->next;
                           moreToSearch = false;
                           break;

            case LESS    : moreToSearch = false;
                           break;

        }
    }

    newNode = new NodeType;
    newNode->info = item;

    if(predLoc == NULL)
    {
        newNode->next = listData;
        listData = newNode;
    }
    else
    {
        newNode->next = location;
        predLoc->next = newNode;
    }
    length++;
}