如何使用给定的类函数对以下列表进行排序?

时间:2014-02-07 21:56:41

标签: c++ sorting

这是CSortedList.cpp文件,我在做我想做的事情时遇到了麻烦。 例如,在InsertItem函数下,我无法对列表进行排序。

MoveListItem函数用于帮助对列表进行排序,方法是根据需要添加/删除项目 - 这是由教授为此作业提供的,我认为不需要更改,但我可能错了。

RemoveItem函数应该完全按照它所说的去做 - 如果我要从列表中删除一个项目,它应该在MoveListItem函数的帮助下将数组内的项目移动到前面。

最后,根据指示,我们的教授给了我们这个函数 - GetTargetIndex,但我不知道我应该用它做什么,也不知道我应该如何使用它来帮助编码。

#include    <iostream>
using namespace std;
#include    "CSortedList.h"


void    CSortedList::DispRetVal(const char*  szMessage
                                                    , ListRetVal  value) const
{
    #ifdef  DEBUG_LIST
    cerr << szMessage << ": ";
    switch (value)
        {
        case   LIST_FULL:
            cerr << "LIST_FULL\n";
            break;

        case   LIST_EMPTY:
            cerr << "LIST_EMPTY\n";
            break;

        case   LIST_ERROR:
            cerr << "LIST_ERROR\n";
            break;

        case   LIST_SUCCESS:
            cerr << "LIST_SUCCESS\n";
            break;

        case   LIST_INVALID_INDEX:
            cerr << "LIST_INVALID_INDEX\n";
            break;

        default:
            cerr << "Unrecognized error code\n";
            break;
        }
    #endif  // DEBUG_LIST

} 

ListRetVal  CSortedList::GetListItem(int  index, ListItemType  &item) const
{
    // if the list is empty, return an error cocde
    if (true == IsListEmpty())
        {
        DispRetVal("GetListItem", LIST_EMPTY);
        return LIST_EMPTY;
        }

    // if the target location is not within appropriate list boundaries (i.e.,
    // 0 to m_numItems-1), return an error code
    if ((index < 0)  ||  (index > (m_numItems - 1)))
        {
        DispRetVal("GetListItem", LIST_INVALID_INDEX);
        return LIST_INVALID_INDEX;
        }

    // initialize the reference parameter and return
    item = m_items[index];
    return LIST_SUCCESS;

}

ListRetVal  CSortedList::InsertItem(const ListItemType  &newItem)
{
    // if the list is already full, return an error code
    if (true == IsListFull())
        {
        DispRetVal("InsertItem", LIST_FULL);
        return LIST_FULL;
        }

    // Supposed to sort the array
    for (int i = 0; i < MAX_ITEMS; i++)
    {
        if (newItem < m_items[i])
        {
            MoveListItems(i, MOVE_TO_BACK);
            m_items[0] = newItem;
            break;
        }
        else
        {
            break;
        }
    }

    m_items[0 + m_numItems] = newItem;
    ++m_numItems;

    return LIST_SUCCESS;

} 

int     CSortedList::MoveListItems(int  targetIndex, int  direction)
{
    int         destIndex;
    int         increment;
    int         sourceIndex;
    int         totalItemsMoved = 0;

    // initialize the source and destination index values
    if (MOVE_TO_BACK == direction)
        {
        sourceIndex = m_numItems - 1;
        destIndex = m_numItems;
        increment = -1;     // move from higher to lower addresses
        }
    else    // MOVE_TO_FRONT
        {
        sourceIndex = targetIndex + 1;
        destIndex = targetIndex;
        increment = 1;      // move from lower to higher addresses
        }

    // loop and "move" elements
    bool        bContinueLoop = true;
    do  {
        m_items[destIndex] = m_items[sourceIndex];
        destIndex += increment;
        sourceIndex += increment;
        ++totalItemsMoved;

        if (MOVE_TO_BACK == direction)
            {
            if ((sourceIndex < targetIndex)  ||  (destIndex < 1))
                {
                bContinueLoop = false;
                }
            }
        else    // MOVE_TO_FRONT
            {
            if (sourceIndex >= m_numItems)
                {
                bContinueLoop = false;
                }
            }

        }  while (true == bContinueLoop);

    return totalItemsMoved;

} 

ListRetVal  CSortedList::RemoveItem(const ListItemType  &targetItem)
{
    // if the list is empty, return an error cocde
    if (true == IsListEmpty())
        {
        DispRetVal("RemoveItem", LIST_EMPTY);
        return LIST_EMPTY;
        }

   // Search for the given value inside the array, and if found
   // delete it.
    for (int i = 0; i < MAX_ITEMS; i++)
    {
        if (targetItem == m_items[i])
        {
            MoveListItems(i, MOVE_TO_FRONT);
        }
    }

    return LIST_SUCCESS;

}  

// This function is going to be helpful in determining if an item is already in the list,
// or if it isn't, where it belongs.
bool CSortedList::GetTargetIndex(const ListItemType  &newItem, int  &targetIndex) const
{

}

总之,我的问题是这些:我不明白'GetListItem'和'GetTargetIndex'应该做什么 - 它们看似多余而且完全没必要,但我不能不使用它们,因为显然它们'那是有原因的。

目前,当我尝试使用给定代码(在'InsertItem'中)对列表进行排序时,最后一个值是'lost',例如,如果我输入以下内容: 12 15 3 将显示以下内容: 3 12 3 而不是应该显示的内容: 3 12 15

从列表中删除一个值('RemoveItem')根本就不起作用。如果我进入 12 15 3 我选择删除'3',显示: 12 3 4217156(基本上是随机数)

现在,显然我不想让代码勺给我 - 我要求的是我的逻辑错误,并解释一些我可能会模糊不清的概念?

谢谢。

0 个答案:

没有答案