C++实现分配算法

#include 
#include

using namespace std;
const int LEN = 640;

typedef struct FreeStack
{
    int len,address;
    struct FreeStack *next;
}*FreeLink;

typedef struct BusyStack
{
    int len,address;
    char name;
    struct BusyStack *next;
}*BusyLink;

FreeLink free_head=NULL;
BusyLink busy_head=NULL;
BusyLink busy_tail=NULL;

void Init()
{
    FreeLink p;

    free_head = (FreeLink)malloc(sizeof(FreeStack));
    free_head->address = -1;
    free_head->len = -1;
    free_head->next = NULL;

    busy_head = (BusyLink)malloc(sizeof(BusyStack));
    busy_head->address = 0;
    busy_head->len = 64;
    busy_head->name = 'O';
    busy_head->next = NULL;

    p = (FreeLink)malloc(sizeof(FreeStack));
    p->address = busy_head->len;
    p->len = LEN - busy_head->len;
    free_head->next = p;
    p->next = NULL;

    busy_tail = busy_head;
}

void Allocate(char name, int require)
{
    FreeLink p = free_head->next;
    BusyLink q = NULL;
    while (p != NULL)
    {
        if (p->len >= require)
        {
            q = (BusyLink)malloc(sizeof(BusyStack));
            q->address = p->address;
            q->len = require;
            q->name = name;
            q->next = NULL;

            p->len -= require;
            p->address += require;
            break;
        }
        p = p->next;
    }
    if (p == NULL)
    {
        cout << "\nWe Can't Allocate This Stuff Called: " << name << endl;
    }
    else
    {
        cout << "\nAllocate This Stuff Success: " << name << endl;
        busy_tail->next = q;
        busy_tail = busy_tail->next;
    }

}

void Reclaim(char name)
{
    FreeLink q = NULL;

    FreeLink cur = free_head->next;
    FreeLink pref = free_head;
    BusyLink preb = busy_head;
    BusyLink curr = busy_head->next;

    while (curr != NULL && curr->name != name)
    {
        preb = preb->next;
        curr = curr->next;
    }

    if (curr == NULL)
    {
        cout << name << "\nis Not Found" << endl;
        return;
    }

    cout << "\nYou Are About To Reclaim The: " << name << endl;

    preb->next = curr->next;

    curr->next = NULL;
    int address = curr->address;
    int len = curr->len;

    q = (FreeLink)malloc(sizeof(FreeStack));
    q->address = address;
    q->len = len;
    q->next = NULL;

    free(curr);

    while (cur->address < q->address)
    {
        cur = cur->next;
        pref = pref->next;
    }

    if (pref->address + pref->len == q->address && q->address + q->len == cur->address)
    {
        pref->len = pref->len + q->len + cur->len;
        pref->next = cur->next;
        free(cur);
        free(q);
    }
    else if (pref->address + pref->len == q->address)
    {
        pref->len += q->len;
        free(q);
    }
    else if (q->address + q->len == cur->address)
    {
        cur->address = q->address;
        cur->len += q->len;
        free(q);
    }
    else
    {
        q->next = cur;
        pref->next = q;
    }
}

void PrintLink()
{
    FreeLink p = free_head->next;
    free_head->next->next;
    BusyLink q = busy_head;

    cout << "--------------------------Start--------------------------------" << endl;
    cout << "The Free Stack is :" << endl;
    while (p != NULL)
    {
        cout << "Address : " << p->address << "  " << "The Len : " << p->len << endl;
        p = p->next;
    }
    cout << endl;
    cout << "The Busy Stack is :" << endl;
    while (q != NULL)
    {
        cout << "Address : " << q->address << "  " << "The Len : " << q->len << "  " << "JOb_Name : " << q->name << endl;
        q = q->next;
    }
    cout << "--------------------------End--------------------------------" << endl;
}

int main()
{
    Init();
    Allocate('A',8);
    Allocate('B',16);
    Allocate('C',64);
    Allocate('D',128);
    PrintLink();
    Reclaim('A');
    Reclaim('C');
    PrintLink();
    Reclaim('B');
    PrintLink();
    Allocate('E',128);
    PrintLink();
}

你可能感兴趣的:(算法,C语言)