简体   繁体   English

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

[英]How to search array from last index?

My program is suppose to replicate the memory management function Next-fit. 我的程序假设要复制内存管理功能Next-fit。 Essentially my program is suppose to have a job and a partition. 基本上我的程序假设有一个作业和一个分区。 It will check to see if the job is less than or equal to the partition size. 它将检查作业是否小于或等于分区大小。 If it is less than or equal to then the job is stored. 如果小于或等于,则存储作业。 A value (index) should hold in which partition the job was stored in last and starts searching from that location. 值(索引)应该保存作业最后存储在哪个分区中,并从该位置开始搜索。

Problem: I do not know how to continuously call the search from the where the last job was stored. 问题:我不知道如何从最后一个作业的存储位置继续调用搜索。

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
}
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;
        }
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 扩展二进制搜索算法以查找要在数组中搜索的键值的第一个和最后一个索引 - Extension of Binary search algo to find the first and last index of the key value to be searched in an array 如何在C ++中获取字符串数组的最后一个索引 - How to get last index of a string array in C++ 当第二个下一个值不存在时如何从数组的开头开始? 并在没有第二个 prev 时从数组的最后一个索引中获取值 - how to start from the beginning of the array when second next value doesn’t exist? and take the value from the last index of array when no second prev 在数组搜索中返回迭代器索引 - Returning iterator index on an array search 如何从multi_index_container获取倒数第二个元素 - How to get the second to last element from a multi_index_container 我正在尝试将我的 c++ 数组从第二个索引切到最后一个索引 - I'm trying to slice my c++ array from the second to the last index 如何获取for循环中的最后一个索引 - How to get the last index in a for loop 使用二进制搜索的多个键的最后索引? - Last index of multiple keys using binary-search? 如何从数组中的给定索引附加非const wchar数组 - How to append non const wchar array from given index in the array 如何在代码中显示模拟,为每个搜索 session 打印数组并最后打印总比较? - How do I show the simulations inside the code printing the array for each search session and printing the total comparison at last?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM