#include <malloc.h>
#include <stdio.h>
#include <string.h>
#define NULL 0
typedef struct table
{
int key;/*进程ID号*/
int priority;/*优先数值*/
char message[10];/*进程说明信息*/
struct table *next;
}node;
node *creat(void)/*定义函数,输入ID号和优先级,按照输入次序建立进程*/
{
node *head,*p1,*p2;
int n=0;
printf("新建的进程控制表为:\n");
p1=p2=(node *)malloc(sizeof(node));
printf("key priority message:\n");
scanf("%d %d",&p1->key,&p1->priority);
gets(p1->message);
head=NULL;
while (p1->key!=0)/*输入0表示结束*/
{
n=n+1;
if (n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(node *) malloc (sizeof(node));
scanf("%d %d",&p1->key,&p1->priority);
gets(p1->message);
}
p2->next=NULL;
return head;
}
void print(node *head)/*输出链表*/
{
node *p;
printf("\nThe Table is:\n");
p=head;
if(!p) printf("链表为空!\n");
else
{
while(p)
{
printf("%d,%d,%s\n",p->key,p->priority,p->message);
p=p->next;
}
}
}
node *processdo(node *head)/*模拟当前最大优先数进程出队的过程*/
{
int max;
static int count=0;
node *p,*pre;
p=pre=head;
max=p->priority;
count++;
while(p)/*找出优先数最大进程*/
{
if(p->priority>max)
{
max=p->priority;
}
p=p->next;
}
p = head;
while(p->priority!=max)
{
pre=p;
p=p->next;
}
printf("\n\n第%d次出队的进程为:\n",count);
printf("key=%d,priority=%d,message=%s\n",p->key,p->priority,p->message);
if(p==head)
{
head=head->next;
}
else
{
pre->next=p->next;
}
free(p);
return head;
}
int main()
{/*模拟创建进程控制块队列,并按照优先级逐个出队过程*/
node *p,*q;
p=q=creat();
print(p);
while(p)
{
q=processdo(p);
p=q;
}
return 0;
}
模拟进程队列管理——按照优先级出列
最新推荐文章于 2022-04-01 19:04:17 发布