# 数据结构题目009

设计一个程序模仿操作系统的进程管理问题，进

1 30
2 20
3 40
4 20
5 0


#include <stdio.h>
#include <stdlib.h>

#define MAX 100

typedef struct ElemType
{
int lever;
int num;
}ElemType;

typedef struct Heap
{
ElemType data[MAX];
int len;
}Heap,*LHeap;

void Init(LHeap H)
{
H->len = 0;
}

bool IsEmpty(LHeap H)
{
return (H->len == 0);
}

bool IsFull(LHeap H)
{
return (H->len == MAX - 1);
}

void Insert(LHeap H,ElemType x)
{
int i;
if (!IsFull(H))
{
i = H->len + 1;
while ((i!=1)&&(x.lever < H->data[i/2].lever))
{
H->data[i] = H->data[i/2];
i/=2;
}
H->data[i].lever = x.lever;
H->data[i].num = x.num;
H->len++;
}
}

void DeleteMax(LHeap H,ElemType* x)
{
int parent = 1,child = 2;
ElemType tmp;
if (!IsEmpty(H))
{
x->lever = H->data[1].lever;
x->num = H->data[1].num;
tmp = H->data[H->len--];
while (child <= H->len)
{
if ((child < H->len) && (H->data[child].lever > H->data[child+1].lever))
{
child++;
}
if (tmp.lever <= H->data[child].lever)
{
break;
}
H->data[parent] = H->data[child];
parent = child;
child*=2;
}
H->data[parent] = tmp;
}
}

int main()
{
Heap H;
FILE *fp;
int i = 1;

if ((fp = fopen("task.dat","r")) == NULL)
{
exit(0);
}

Init(&H);

while (!feof(fp))
{
}

printf("序号\t任务号\t优先级\t\n");

while (!IsEmpty(&H))
{
printf("%d\t",i);
i++;
}
system("pause");
return 0;
}

12-30 591

09-13 7366
08-10 4337
07-22 1万+
08-29
05-22 608
02-02 4036