大家帮忙看下 输出有问题
程序代码:/*
*单链表插入排序
*/
#include <stdio.h>
#include <stdlib.h>
struct PCB
{
char name[10];
int rtime;
int priority;
char state;
struct PCB *next;
};
/*
*初始化PCB
*/
struct PCB *initPCB()
{
struct PCB *head;
head=(struct PCB *)malloc(sizeof(struct PCB));
if(NULL==head)
{
exit(-1);
}
head->next=NULL;
return head;
}
struct PCB *inputValue(struct PCB *head)
{
struct PCB *temp;
temp=head;
//for(int i=0;i<5;i++)
//{
printf("please input the information of process\n");
printf("name:");
scanf("%s",temp->name);
printf("run time:");
scanf("%d",&temp->rtime);
printf("priority:");
scanf("%d",&temp->priority);
temp->state='R';
printf("\n");
// }
return temp;
}
/*
*找到优先级最高的进程
*/
void MaxPriorityProcess(struct PCB * head,struct PCB * pCurrent)
{
struct PCB *temp;
struct PCB *p;//p为新节点分配内存,temp用来遍历链表
temp=head->next;
p=(struct PCB*)malloc(sizeof(struct PCB));
p->priority=pCurrent->priority;
while(temp)
{
if(pCurrent->priority>temp->priority) //如果大于某个已经存在的节点
{ //则把新节点插入到该节点前面
p->next=temp->next;
temp->next=p;
}
temp=temp->next;
}
p->next=NULL;//如果小于所有已经存在的节点,则执行下面的代码
head->next=p;//把新节点插入到链表的最后
}
/*
*打印PCB
*/
void printPCB(struct PCB * head)
{
struct PCB *temp;
temp=head->next;
while(temp!=NULL)
{
printf("\n process name: %s\n run time: %d\n priority num: %d\n process state:%c\n",
temp->name,temp->rtime,temp->priority,temp->state);
temp=temp->next;
}
printf("\n");
}
int main()
{
struct PCB *head=NULL;
struct PCB *temp=NULL;
//head=initPCB();
/*head=(struct PCB *)malloc(sizeof(struct PCB));
if(NULL==head)
{
exit(-1);
}
head->next=NULL;
for(int i=0;i<5;i++)
{
temp=initPCB();
//temp->next = NULL;
MaxPriorityProcess(head,temp);
}*/
head=initPCB();
for(int i=0;i<5;i++)
{
temp=inputValue(head);
MaxPriorityProcess(head,temp);
}
printPCB(head);
return 0;
}

