| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 689 人关注过本帖
标题:一小链表 帮忙看看哪里有问题
只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:2 
一小链表 帮忙看看哪里有问题
程序代码:
#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;
}



搜索更多相关主题的帖子: next 
2011-11-17 22:28
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
木有人
2011-11-18 11:07
qq605412254
Rank: 2
等 级:论坛游民
帖 子:15
专家分:63
注 册:2011-11-18
收藏
得分:20 
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;
}
head里面的数据是什么?
struct PCB *initPCB()
里面输入没循环?
2011-11-19 09:36
快速回复:一小链表 帮忙看看哪里有问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018510 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved