201406114331-进程调度

#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}queuenode;
typedef struct node1
{
queuenode *front;
queuenode *rear;
}queue;
queue initqueue()//初始化队列
{
queue q;
q.front=(queuenode *)malloc(sizeof(queuenode));
q.front->next=NULL;
q.rear=q.front;
return q;
}
queuenode *applynode(int x)//申请新的结点
{
queuenode *p;
p=(queuenode *)malloc(sizeof(queuenode));
p->data=x; //将x的值存入新的结点
p->next=NULL;
return p;

}
queue insreq(queue q,int x)//进队
{
queuenode *p;
p=applynode(x);//申请新的结点
q.rear->next=p;//将队尾指针指向p结点,p结点成为队尾
q.rear=p;//再将队尾指针指向p
return q;
}
queue delete(queue q)//出队列
{
queuenode *p;
if(q.front==q.rear)//判断队列是否为空
{
printf("队空,无法出队!");
return q;
}
p=q.front->next;//p指向队头结点,待删除
q.front->next=p->next;//front结点指针域跳过p,指向p的后继
if(p==q.rear)//如果被删除的结点是队尾结点
{
q.rear=q.front;//rear指向头结点
}
free(p); //释放p结点的空间
return q;
}
void display(queue q)
{
queuenode *p;
printf("\n进程目录:");
p=q.front->next;//printf("%d ",q.front->next->data);
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}

}
void displ(queue b)
{
queuenode *p;
printf("\n运行时间:");
p=b.front->next;//printf("%d ",q.front->next->data);
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}

}
void way1(int *a,int n)
{
int i,j,m,temp;
for(i=0;i<n-1;i++)
{
m=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[m])
m=j;
}
if(m!=i)
{
temp=a[i];
a[i]=a[m];
a[m]=temp;
}
}
}
void way2(int *a,int n)
{
int i,j,m,temp,l,k=0;
for(i=0;i<n-1;i++,k++)
{
m=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[m])
m=j;
}
if(m!=i)
{
temp=a[i];
a[i]=a[m];
a[m]=temp;
}
printf("\ni=%d:[ ",i);
for(l=0;l<n;l++)
{
printf(" %d ",a[l]);
if(l==k)
printf("]");
}
printf("\n");
}
}
main()
{
int k,h=1,a[5],n,c[5];
char i;
queue q,b;
q=initqueue();//初始化队列
b=initqueue();
for(k=0;k<5;k++)
{
printf("输入进程号:");
scanf("%d",&c[k]);
q=insreq(q,c[k]);
printf("输入运行时间:");
scanf("%d",&a[k]);
b=insreq(b,a[k]);

}
display(q);
displ(b);
printf("\n");
printf("\n运行时间的排列:");
way1(a,5);
for(k=0;k<5;k++)
printf("%4d",a[k]);
printf("\n");
printf("\n运行时间的具体排列方法:");
way2(a,5);
do
{
printf("是否要出队列?y/n ");
scanf("%c",&i);
if(i=='y'||i=='Y')
{
q=delete(q);
display(q);
}

if(i=='n'||i=='N')
h=-1;

}while(h==1);
}

posted on 2016-04-13 14:59  31黄智涛  阅读(104)  评论(0编辑  收藏  举报