一小链表 帮忙看看哪里有问题
程序代码:
#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; printf("please input the information of process\n"); printf("name:"); scanf("%s",head->name); printf("run time:"); scanf("%d",&head->rtime); printf("priority:"); scanf("%d",&head->priority); head->state='R'; printf("\n"); return head; } /* *找到优先级最高的进程 */ void MaxPriorityProcess(struct PCB * head,struct PCB* pCurrent) { struct PCB* temp; struct PCB* p; temp=head; if(NULL==temp)//队列为空 { temp=pCurrent; } if(pCurrent->priority>temp->priority)//如果优先级最大 { pCurrent->next=temp; temp=pCurrent; } if(NULL==temp->next)//队列里只有一个元素 { if(temp->priority>pCurrent->priority) { temp->next=pCurrent; } } /*队列元素排列*/ p=head; while(p!=NULL) { if(temp->priority>pCurrent->priority) { p=temp;//记录当前节点 temp=temp->next;//后移 } else { break; } } pCurrent->next=p->next; p->next=pCurrent; } /* *打印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; struct PCB* temp; head=new PCB; //PCB AA; //head = &AA; for(int i=0;i<3;i++) { temp=initPCB(); temp->next = NULL; MaxPriorityProcess(head,temp); } printPCB(head); return 0; }