# FZU 1893 (模拟内存分配算法 涉及链表等 13.12.03)

847人阅读 评论(2)

Problem 1893 内存管理

## Input

Create：两个整数i和size，i表示新进程的进程号，i>0，size表示新进程需占用的内存大小，size>0，输入保证新进程的进程号与已存在的进程号不同。
Delete：一个整数i，i>0，表示要结束的进程号。
Print：无参数。
End：无参数，不做任何处理，表示系统终止命令。

## Sample Input

2Create 1 30Create 2 20Create 3 30PrintCreate 4 100Delete 4Delete 2PrintDelete 3PrintEndCreate 1 100PrintEnd

## Sample Output

Create process 1 of size 30 successfully!Create process 2 of size 20 successfully!Create process 3 of size 30 successfully!P 1 30P 2 20P 3 30H 20No enough memory!No such process!Delete process 2 of size 20 successfully!P 1 30H 20P 3 30H 20Delete process 3 of size 30 successfully!P 1 30H 70Create process 1 of size 100 successfully!P 1 100

## Source

AC代码：

#include<stdio.h>
#include<string.h>

struct memory{
int mark;
int num;
int space;
memory *next;
};

int main() {
int T;
scanf("%d", &T);
while(T--) {
memory *m = new memory;
m->mark = 0;
m->num = -1;
m->space = 100;
m->next = NULL;
char str[25];
while(scanf("%s", str) != EOF) {
if(!strcmp(str, "Create")) {
int n1, big;
int flag1 = 0;
scanf("%d %d", &n1, &big);
memory *p1 = m;
memory *Min = new memory;

Min->space = 0;

while(p1 != NULL) {
if(p1->space > Min->space)
Min = p1;
p1 = p1->next;
}

p1 = m;
while(p1 != NULL) {
if(p1->mark == 0 && p1->space >= big && p1->num != n1) {
if(p1->space < Min->space) {
Min = p1;
flag1 = 1;
}
else if(p1->space <= Min->space && p1->next == NULL) {
Min = p1;
flag1 = 1;
}
}
p1 = p1->next;
}
if(flag1) {
Min->mark = 1;
Min->num = n1;
if((Min->space - big) > 0) {
memory *t = new memory;
t->mark = 0;
t->num = -1;
t->space = Min->space - big;
t->next = Min->next;
Min->next = t;
}
Min->space = big;
printf("Create process %d of size %d successfully!\n", n1, big);
}
else
printf("No enough memory!\n");
}
else if(!strcmp(str, "Print")) {
memory *p2 = m;
while(p2 != NULL) {
if(p2->mark == 1)
printf("P %d %d\n", p2->num, p2->space);
else
printf("H %d\n", p2->space);
p2 = p2->next;
}
}
else if(!strcmp(str, "Delete")) {
int n2;
int flag2 = 0;
scanf("%d", &n2);
memory *p3 = m;
while(p3 != NULL) {
if(p3->num == n2) {
printf("Delete process %d of size %d successfully!\n", p3->num, p3->space);
flag2 = 1;
p3->mark = 0;
p3->num = -1;
memory *p4 = m;
while(p4 != NULL) {
while(p4->mark == 0 && p4->next != NULL && p4->next->mark == 0) {
p4->space = p4->space + p4->next->space;
p4->next = p4->next->next;
}
p4 = p4->next;
}
}
p3 = p3->next;
}
if(!flag2)
printf("No such process!\n");
}
else if(!strcmp(str, "End")) {
delete m;
break;
}
}
}
return 0;
}

0
1

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：90792次
• 积分：2372
• 等级：
• 排名：第15732名
• 原创：152篇
• 转载：0篇
• 译文：0篇
• 评论：11条
博客专栏
 ACM专栏 文章：1篇 阅读：704
阅读排行
评论排行
最新评论