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

#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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值