C语言链表 为什么出现死循环 求教各位大侠

来源:百度知道 编辑:UC知道 时间:2024/06/08 13:58:48
#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include "conio.h"
#define READY 1
#define RUN 2
#define BLOCK 3
typedef struct pcb
{
int num;
struct pcb *next;
int watetime; /* */
int cputime;
int state;
}pcb;
int x;
struct pcb *head;
struct pcb *run;
pcb *creatpc(int n) /* ying gai shi yige xun huan biao*/
{
int i=1;
pcb *head,*p,*q;
randomize();
head=(pcb *)malloc(sizeof(pcb));
head->num=i;
head->next=NULL;
head->watetime=random(10);
head->cputime=random(10)+1;
head->state=READY;
p=head;
for(i=2;i<=n;i++)
{
q=(pcb *)malloc(sizeof(pcb));
p->next=q;
q->num=i;
q->next=NULL;
/* q->priority=random(10); */
q->watetime=random(10); /* */
q->cputime=random(10)+1; /* to day*/

把creatpc函数倒数第二行 p->next=head;去掉就好了。
p->next=head;就是把链表的结尾又指向了头节点,就形成了一个循环链表。所以永远也没有一个节点的next域为NULL。也就导致了主函数中while(run)永远是true。

在我机器编不过去,没时间全看,要是有能编过去的发出来给你看看.