?? mbtlist.cpp
字號:
#include "stdafx.h"
#include "MBTList.h"
#include <iostream.h>
MBTList::MBTList()
{
headMBT = 0;
tailMBT = 0;
len = 0;
count = 0;
}
MBTList::~MBTList()
{
if(IsEmpty())
{
return;
}
MBT *tempMBT = tailMBT;
while(tempMBT != 0 && tailMBT->preMBT != 0)
{
tempMBT = tailMBT->preMBT;
delete tailMBT;
tailMBT = tempMBT;
}
if(tailMBT->preMBT == 0)
delete tailMBT;
}
bool MBTList::InsertHeadMBT(MBT *mbt)//插入第一個存儲塊
{
bool result = false;
if(IsEmpty() == true)
{
result = true;
headMBT = mbt;
tailMBT = mbt;
headMBT->nextMBT = 0;
tailMBT->preMBT = 0;
}
return result;
}
bool MBTList::InsertAfterHead(MBT *mbt)//在第一個存儲塊的后面添加一個存儲塊
{
bool result = false;
if(IsOnlyOne() == true)
{
result = true;
headMBT->preMBT = 0;
headMBT->nextMBT = mbt;
mbt->nextMBT = 0;
mbt->preMBT = headMBT;
tailMBT = mbt;
}
else if(IsEmpty() == false)
{
result = true;
headMBT->nextMBT->preMBT = mbt;
mbt->preMBT = headMBT;
mbt->nextMBT = headMBT->nextMBT;
headMBT->nextMBT = mbt;
}
return result;
}
bool MBTList::InsertAfterTail(MBT *mbt)//在最后一個存儲塊的后面添加一個存儲塊
{
bool result = false;
if(IsEmpty() == false)
{
result = true;
tailMBT->nextMBT = mbt;
mbt->preMBT = tailMBT;
tailMBT = mbt;
tailMBT->nextMBT = 0;
headMBT->preMBT = 0;
}
return result;
}
bool MBTList::InsertBeforeHead(MBT *mbt)//在第一個存儲塊前插入一個存儲塊
{
bool result = false;
if(IsEmpty() == false)
{
result = true;
headMBT->preMBT = mbt;
mbt->nextMBT = headMBT;
headMBT = mbt;
headMBT->preMBT = 0;
tailMBT->nextMBT = 0;
}
return result;
}
bool MBTList::InsertBeforeTail(MBT *mbt)//在最后一個存儲塊的前面插入一個存儲塊
{
bool result = false;
if(IsOnlyOne() == true)
{
result = true;
tailMBT->preMBT = mbt;
tailMBT->nextMBT = 0;
mbt->nextMBT = tailMBT;
mbt->preMBT = 0;
headMBT = mbt;
}
else if(IsEmpty() == false)
{
result = true;
tailMBT->preMBT->nextMBT = mbt;
mbt->nextMBT = tailMBT;
mbt->preMBT = tailMBT->preMBT;
tailMBT->preMBT = mbt;
}
return result;
}
bool MBTList::IsEmpty()//判斷是否為空
{
bool result = false;
if(count == 0)
result = true;
return result;
}
bool MBTList::IsOnlyOne()//判斷是否只有一個存儲塊
{
bool result = false;
if(count == 1)
result = true;
return result;
}
bool MBTList::InsertMBT(MBT *mbt, int sort)
{
mbt->nextMBT = 0;
mbt->preMBT = 0;
bool result = InsertHeadMBT(mbt);
if(result == false)
{
MBT *tempMBT = headMBT;
while(tempMBT != 0 && result == false)
{
int cur = 0, temp = 0;
switch(sort)
{
case 1://按照地址從低到高的順序
cur = mbt->startAdd;
temp = tempMBT->startAdd;
break;
case 2://按照存儲塊長度由小到大
cur = mbt->len;
temp = tempMBT->len;
break;
case 3://按照存儲塊長度由大到小
cur = tempMBT->len;
temp = mbt->len;
break;
default:
cout << "數據有誤!" << endl;
return false;
}
if(cur < temp)
{
if(tempMBT == headMBT)
{
result = InsertBeforeHead(mbt);
}
else if(tempMBT == tailMBT)
{
result = InsertBeforeTail(mbt);
}
else
{
result = true;
tempMBT->preMBT->nextMBT = mbt;
mbt->preMBT = tempMBT->preMBT;
tempMBT->preMBT = mbt;
mbt->nextMBT = tempMBT;
}
}
else if(cur == temp)
{
result = InsertMBT(tempMBT, mbt);
}
tempMBT = tempMBT->nextMBT;
}
}
if(result == false)
{
result = InsertAfterTail(mbt);
}
count++;
len = len + mbt->len;
return result;
}
bool MBTList::InsertMBT(MBT *tempMBT, MBT *mbt)
{
bool result = false;
MBT *curMBT = tempMBT;
while(tempMBT != 0 && tempMBT->len == mbt->len)
{
if(tempMBT->startAdd > mbt->startAdd)
{
if(tempMBT == headMBT)
{
result = InsertBeforeHead(mbt);
}
else if(tempMBT == tailMBT)
{
result = InsertBeforeTail(mbt);
}
else
{
result = true;
tempMBT->preMBT->nextMBT = mbt;
mbt->preMBT = tempMBT->preMBT;
mbt->nextMBT = tempMBT;
tempMBT->preMBT = mbt;
}
break;
}
curMBT = tempMBT;
tempMBT = tempMBT->nextMBT;
}
if(result == false && curMBT != 0)
{
if(curMBT == tailMBT)
{
result = InsertAfterTail(mbt);
}
else
{
result = true;
curMBT->nextMBT->preMBT = mbt;
mbt->nextMBT = curMBT->nextMBT;
mbt->preMBT = curMBT;
curMBT->nextMBT = mbt;
}
}
return result;
}
void MBTList::Print()
{
cout << "起始地址" << " " << "長度" << " " << "進程名稱" << endl;
MBT *tempMBT = headMBT;
while(tempMBT != 0)
{
cout << tempMBT->startAdd << " " << tempMBT->len << " ";
if(tempMBT->curPCB != 0)
cout << tempMBT->curPCB->name;
cout << endl;
tempMBT = tempMBT->nextMBT;
}
}
MBT* MBTList::Destribute(PCB *pcb)
{
MBT *get = 0;
MBT *tempMBT = headMBT;
while(tempMBT != 0)
{
if(tempMBT->len >= pcb->len)
{
int pL = tempMBT->len - pcb->len;
tempMBT->len = pcb->len;
int stAdd = tempMBT->startAdd + tempMBT->len;
get = new MBT(1, pcb);
get->startAdd = tempMBT->startAdd;
get->len = pcb->len;
tempMBT->startAdd = stAdd;
tempMBT->len = pL;
break;
}
tempMBT = tempMBT->nextMBT;
}
return get;
}
void MBTList::Delete(MBT *mbt)
{
MBT *tempMBT = headMBT;
while(tempMBT != 0)
{
if(tempMBT == mbt)
{
tempMBT->preMBT->nextMBT = tempMBT->nextMBT;
tempMBT->nextMBT->preMBT = tempMBT->preMBT;
break;
}
tempMBT = tempMBT->nextMBT;
}
}
MBT* MBTList::Recyle(char *n)
{
MBT *get = 0;
MBT *tempMBT = headMBT;
while(tempMBT != 0)
{
if(tempMBT->curPCB->name == n)
{
get = tempMBT;
Delete(get);
break;
}
tempMBT = tempMBT->nextMBT;
}
return get;
}
void MBTList::UnionMBT(MBT *mbt, int sort)
{
InsertMBT(mbt, sort);
MBT *tempMBT = headMBT;
bool result = false;
while(tempMBT != 0)
{
if(tempMBT->nextMBT != 0)
{
if(tempMBT->startAdd + tempMBT->len == tempMBT->nextMBT->startAdd)
{
result = true;
MBT *delMBT = 0;
tempMBT->flag = 0;
tempMBT->curPCB = 0;
tempMBT->len = tempMBT->len + tempMBT->nextMBT->len;
if(tempMBT->nextMBT->nextMBT != 0)
{
tempMBT->nextMBT->nextMBT->preMBT = tempMBT;
delMBT = tempMBT->nextMBT;
tempMBT->nextMBT = tempMBT->nextMBT->nextMBT;
}
delete delMBT;
}
else
{
if(result == true)return;
tempMBT = tempMBT->nextMBT;
}
}
else
{
tempMBT = tempMBT->nextMBT;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -