Question: I need help with modifying my best,worst,delete, and compact function #include #include #include #include #include #include #include memory _ interface.cpp using namespace std; const

I need help with modifying my best,worst,delete, and compact function
#include
#include
#include
#include
#include
#include
#include "memory_interface.cpp"
using namespace std;
const char FIRST_FIT ='F';
const char WORST_FIT ='W';
const char BEST_FIT ='B';
struct alloc_entry_t{
int start;
int length;
char contents;
};
// GLOBALS
char nAllocatorType;
list free_list;
list used_list;
/**********
TEMPLATES
**********/
MemoryInterface mem;
string promptUser();
void deleteEntry(char ch);
void showhelp();
void compactMemory();
bool store(int, char);
/****
END TEMPLATES
****/
/**
** Finds the first free section of memory to fit 'length' many characters.
**--this function should return the location of the beginning of
** a sufficiently large hole. Specifically, the first on big enough.
Returns -1 if there isn't enough free memory.
** TODO: Finish this function!
**/
int firstfit(int requestedLength)
{
// NOTE: this file does NOT need to add the record to the used_list,
// because store() takes care of that for you!
for(list::iterator it = free_list.begin(); it != free_list.end(); it++)
{
if(it->length >= requestedLength){
// can fit here
int loc = it->start;
if(it->length > requestedLength){
it->length -= requestedLength;
it->start += requestedLength;
}
else{
free_list.erase(it);
}
return loc;
}
}
// if you get here then I guess we couldn't find a big enough hole
return -1;
}
/**
** TODO: IMPLEMENT THIS!!
** DESCRIPTION: find the `worst' hole for a length of
** size length (as defined by the worst-fit algorithm)
** and return that location. Return -1 if no location
** exists.
**
*/
int worstfit(int length)
{
// find the largest block/hole
int largestSize =-1;
int worstIndex =-1;
for(list::iterator it = free_list.begin(); it != free_list.end(); it++)
{
// find largest hole
if(it->length >= length && it->length > largestSize){
largestSize = it->length;
worstIndex = it->start;
}
}
if(worstIndex !=-1){
// if we found a hole that can fit the requested size
// and it is the largest hole, return the start of that hole
for(list::iterator it = free_list.begin(); it != free_list.end(); it++)
{
if(it->start == worstIndex){
if(it->length > length){
it->length -= length;
it->start += length;
}
else{
free_list.erase(it);
}
}
}
return worstIndex;
}
return -1;
}
/**
** TODO: IMPLEMENT THIS!!
** DESCRIPTION: find the `best' hole for a length of
** size length (as defined by the worst-fit algorithm)
** and return that location. Return -1 if no location
** exists.
**
*/
// This function should find the smallest hole that can fit the
//requested block of memory, but it must still be larger than or equal to the requested size.
int bestfit(int requestedLength){
int bestLoc =-1; // This will store the location of the best fitting hole
int bestSize =0; // This will store the size of the best fitting hole, initially set to zero for comparison
// Loop through the free list to find the smallest hole that fits the requested length
for (list::iterator it = free_list.begin(); it != free_list.end(); ++it){
if (it->length >= requestedLength){
// If it's the first fitting hole found or a smaller fitting hole than previously found
if (!bestSize || it->length < bestSize){
bestSize = it->length;
bestLoc = it->start;
}
}
}
// If a best fit was found, adjust the hole or remove it
if (bestLoc !=-1){
for (list::iterator it = free_list.begin(); it != free_list.end(); ++it){
if (it->start == bestLoc){
if (it->length > requestedLength){
it->length -= requestedLength;
it->start += requestedLength;
} else {
free_list.erase(it);
}
break; // Exit the loop after modifying the list to avoid invalid iterator usage
}
}
return bestLoc;
}
return -1; // Return -1 if no suitable hole is found
}
/**
** TODO: IMPLEMENT THIS!!
** DESCRIPTION: should compact the holes in the memory (i.e., defrag)
**
*/
void compactMemory()
{
}
void deleteEntry(char item){
// find it in used_list
for(list::iterator it = used_list.begin(); it != used_list.end(); it++)
{
if(it->contents == item){
// create a new hole
alloc_entry_t

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!