如何从上一个索引搜索数组?

时间:2015-03-04 16:21:52

标签: c++ arrays search

我的程序假设要复制内存管理功能Next-fit。基本上我的程序假设有一个作业和一个分区。它将检查作业是否小于或等于分区大小。如果小于或等于,则存储作业。值(索引)应该保存作业最后存储在哪个分区中,并从该位置开始搜索。

问题:我不知道如何从最后一个作业的存储位置继续调用搜索。

void  Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
    Partition temp;
    int Next;
    int index;
    //loops the amount of jobs
    for (int i = 0; i < numberOfJobs; i++)
    {
        //job less than or = partition
        if (job[i].jobSize <= partitionArray[i].size)
        {
            //partition =  partition - job
            partitionArray[i].size = partitionArray[i].size -job[i].jobSize;
            cout << "Unused space (Hole): " << partitionArray[i].size << "Kb" << endl;
            cout << endl;

        }
        //index becomes the location of where the last job was stored
        index = i;

        //relaunch loop to search from point index
}

1 个答案:

答案 0 :(得分:0)

void  Partition::nextFit(Job *job, Partition *partitionArray, int partitionArraySize, int numberOfJobs)
{
    int j = 0;
    int count=0;
    int locationOfMemory;

//finds the largest partition
    int maxsize = partitionArray[0].size;

    for (int l = 0; l < partitionArraySize; l++)
    {
        if (maxsize <= partitionArray[l].size)
        {
            maxsize = partitionArray[l].size;
        }
    }


//loops the amount of jobs

    for (int i = 0; i < numberOfJobs; i++)
    {
        if (job[i].jobSize < maxsize)
        {
            //job less than or = partition
            if (job[i].jobSize <= partitionArray[j].size)
            {
                locationOfMemory = j+1;
                //partition =  partition - job
                cout << "Job: " << i + 1 << " was allocated in Partition: " << locationOfMemory<< endl;
                partitionArray[j].size = partitionArray[j].size - job[i].jobSize;
                cout << "Unused space (Hole): " << partitionArray[j].size << "Kb" << endl;
                cout << endl;
                if (j != partitionArraySize - 1)
                {
                    j++;
                }
                else
                {
                    j = 0;
                }

            }
            else
            {    //if the job is larger than the partition stay at the current job and increment the partition index to the next one
                i = i - 1;
                j = j + 1;
                }
        }
         else
        {
            cout << "Job" << "is too big and cannot be allocated" << endl;
        }
    }
//partition busy or not
    for (int m = 0; m < partitionArraySize; m++)
    {

         if (partitionArray[m].size == 0)
        {
            count = m + 1;
            cout << "Partition # " << count << " Status: Busy" << endl;
        }
        else
        {
            count = m + 1;
            cout << "Partition # " << count << " Status: Free" << endl;
        }
    }


}