#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 *insert(node *head,node *news)/*将新进程插入到已经排序的队列中,对进程表按优先数从大到小排序*/
{
node *p,*q;
p=head;
while(p&&p->priority>=news->priority)
{
q=p;
p=p->next;
}
if(p==head)
{
news->next=head;
head=news;
}
else if(p==NULL)
{
q->next=news;
q=news;
}
else
{
news->next = p;
q->next = news;
}
return head;
}
node *creat(void)/*定义函数,调用insert函数建立排好序的进程链表*/
{
node *head,*p1;
int n=0;
printf("新建的进程控制表为:\n");
printf("key priority message:\n");
p1=(node *)malloc(sizeof(node));
scanf("%d%d",&p1->key,&p1->priority);
gets(p1->message);
p1->next=NULL;
head=NULL;
while (p1->key>0)/*输入0表示结束*/
{
n=n+1;
if (n==1) head=p1;
else
head=insert(head,p1);
p1=(node *) malloc (sizeof(node));
scanf("%d%d",&p1->key,&p1->priority);
gets(p1->message);
p1->next=NULL;
}
return head;
}
void print(node *head)/*输出链表*/
{
node *p;
printf("\nThe Table is:\n");
p=head;
if(!p) printf("链表为空!");
else
{
while(p)
{
printf("%d,%d,%s\n",p->key,p->priority,p->message);
p=p->next;
}
}
}
void rank_out(node *head)/*模拟按优先数大小进程分级出队的过程*/
{
int count=1;
node *p;
p=head;
while(p)
{
printf("\n第%d个出队进程为:%d\n\n",count,p->priority);
printf("key=%d,priority=%d,message=%s\n",p->key,p->priority,p->message);
p=p->next;
count++;
}
}
int main()
{
node *p;
p=creat();
print(p);
rank_out(p);
return 0;
}
模拟进程队列管理——按照优先级入列
最新推荐文章于 2022-01-30 23:00:50 发布