-2

我有一个有序的arrayList。元素将在 1,2,3,4,5,6 中排序。目前推送功能不起作用。它将插入一个元素,但有一个我无法弄清楚的问题。一旦你插入一个递增的数字,推送就会起作用......所以就像 1,2,3,4,5 但一旦我插入这样的 5,2,4,3,2,1 就不起作用了。谁能帮我这个?

这是我的代码:

初始化

template <class Datatype>
//--------------------------------------------------------------------------------------------
//  Class: OrderedArray.
//--------------------------------------------------------------------------------------------
class OrderedArray
{
    //--------------------------------------------------------------------------------------------
    //  Member Variables.
    //--------------------------------------------------------------------------------------------
private:
    Datatype* m_array;
    int size;
    int g_size;
    int num_elements;
    //---------------------------------------------------------------------------------------
    //  Name:            Print Function:
    //  Description:     To print out all elemenst in the Array.
    //---------------------------------------------------------------------------------------
    void print()
    {
        for(int i=0;i< size;i++)
        {
            cout << "Position: " <<m_array[i]<<endl;
        }
    }

    //---------------------------------------------------------------------------------------
    //  Name:           Resize Function:
    //  Description:    To resize the Array.
    //---------------------------------------------------------------------------------------
    void Resize(int p_size)//resizes the array to the size of p_size
    {
        if(p_size < 0)//checks if new size is less than 0
        {
            cout << "ERROR! Size of an array can not be less than 0!" << endl;
        }
        else//else its ok to continue
        {
            Datatype* newArray = new Datatype[p_size];//creates a pointer newArray that points at a new array
            if(newArray == 0)
                return;

            int min;

            if(p_size < m_size)//checks the if the new array is smaller than the old one
                min = p_size;
            else//else its going to be bigger
                min = m_size;

            int index;
            int temp = num_elements;//puts num_elements into a temporary variable called temp
            num_elements = 0;//num_elements is set to 0
            for(index = 0; index < min; index++)
            {
                newArray[index] = m_array[index];//places everything from the old array into the new array that will fit.
                if(num_elements < temp)//if the num_elements is less than temp(the original num_elements)
                {
                    num_elements++;//increment num_elements. This will keep incrementing to create the new num_elements based the number of elements cut off in the resize
                }
            }
            m_size = p_size;//sets the old size to be equal to the new size

            if(m_array != 0)
                delete[] m_array;//deletes the old array
            m_array = newArray;//makes m_array point at the new array
            newArray = 0;//makes newArray a null pointer
        }
    }


    //---------------------------------------------------------------------------------------
// Name:             Push
// Description:      
//---------------------------------------------------------------------------------------
void push(Datatype p_item)
{
    if(num_elements == size)//checks if the array is full and needs to be resized
    {
        Resize(size + g_size);//calls the resize function
    }

    int pos = num_elements;
    for(int x=0;x<num_elements;x++)
    {
        if(p_item < m_array[x])
        pos=x;
        break;
    }

    //loops through the array from high to low moving all values to the right
    //to make space for the passed in value until it gets to the right place
    for(int index = num_elements; index >= pos; index--)
    {
        m_array[index] = m_array[index-1];//moves the values to the right
    }
        m_array[pos] = p_item;//the passed in value is positioned into its ordered position
        num_elements++;

    cout<< "Num Elements " << num_elements;
    cout<< "Size " <<size;
}
4

1 回答 1

0

我想我找到了问题的开始:

for(int x=0;x<num_elements;x++)
{
    if(p_item < m_array[x])
        pos=x;
}

pos将始终以这种方式位于数组的末尾(假设数组已正确排序到该点)。添加一条break语句,让您的循环知道何时应该停止分配越来越大的值pos

for(int x=0;x<num_elements;x++)
{
    if(p_item < m_array[x])
    {
        pos=x;
        break;
    }
}

您的代码也有一些其他问题,例如

for(int index = num_elements; index > pos; index--)
{
    m_array[index] = m_array[index+1];//moves the values to the right
}

实际上将值向左移动。将分配更改为

for(int index = num_elements; index > pos; index--)
{
    m_array[index] = m_array[index-1];//moves the values to the right
}
于 2013-03-11T16:17:16.530 回答