FZU 1893 (13.12.03)(二)

2014-11-24 02:56:58 · 作者: · 浏览: 5
t 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; }