一个关于打印单链表的错误

来源:互联网 发布:中英文分词 python 编辑:程序博客网 时间:2024/05/07 01:48
#include<stdlib.h>
#include<stdio.h>






struct process
{
int name;//进程名称
int priority;//进程优先级
int time;//进程运行时间
char state;//进程状态,用R表示就绪,用E表示结束
struct process *next;
};


//进程初始化
void initialize(struct process *head);


//运行进程
int run(struct process *head);




int main()
{
int a=1;//a表示程序的运行次数
    struct process *head;
head=(struct process *)malloc(sizeof(struct process ));
head->next=NULL;
initialize(head);
while(head->next)
{
printf("第%d次运行",a);
   run(head);
a++;


}


system("PAUSE");
return 0;
}
//进程初始化
void initialize(struct process *head)
{
int i;
struct process *s,*p;  //s为插入的新节点,p为标记节点   


for(i=0;i<5;i++)
{
   s=(struct process *)malloc(sizeof(struct process));
p=head;
printf("请输入第%d个进程的优先级,运行时间,用空格隔开\n",i+1);
scanf("%d %d",&s->priority,&s->time);
fflush(stdin);
s->state='R';
s->name=i+1;
while(p->next)
{
if(s->priority>p->next->priority)
  break;
p=p->next;


}
s->next=p->next;
p->next=s;


}
}


//运行进程
int run(struct process *head)
{
struct process *current,*p;//current为正在运行的进程的节点,p为标记节点

p=head;
current=p->next;
p->next=current->next;
printf("\n开始运行进程%d\n",current->name);
current->time=current->time-1;
current->priority=current->priority-1;
if(current->time>0)
{
printf("进程%d运行完之后为就绪状态\n",current->name);


  while(p->next)
{
if(current->priority>=p->next->priority)
  break;
p=p->next;


}
current->next=p->next;
p->next=current;


}
else
{
       printf("进程%d运行完之后为完成状态\n",current->name);
delete current;
   
}

printf("运行完毕之后的就绪进程队列为:\n");
    while(head->next)
{
printf("%d",head->next->name);
head=head->next;
}
/*WRONG:
while(p->next)
{
printf("%d",p->next->name);
p=p->next;
}
*/

printf("\n\n\n");
return 0;
}
0 0
原创粉丝点击