头文件中定义的一个pbc类,后续作为链队列的节点
class PBC
{
public:
PBC(void);
~PBC(void);
PBC(string name,int time,int Runtime,int priority,char state,PBC* next);//构造函数
public:
string name;//进程名
int time;
int Runtime;//运行时间
int priority;//优先级
char state;//状态
PBC* next;//指向下一个进程
};
实现pbc类
#include "StdAfx.h"
#include "PBC.h"
PBC::PBC(void)
{
name="",time=0,Runtime=0,priority=0,state=NULL,next=NULL;
}
PBC::~PBC(void)
{
}
PBC::PBC(string name,int time,int Runtime,int priority,char state,PBC* next)
{
this->name=name;
this->time=time;
this->Runtime=Runtime;
this->priority=priority;
this->state=state;
this->next=next;
}
头文件中定义的链式队列类
#pragma once
#include "PBC.h"
class CQueue
{
public:
CQueue(void);
~CQueue(void);
void InitQueue();//队列初始化
bool IsEmpty();//判断是否为空
void EnQueue(PBC* p);//入队
void DeQueue();//出队
void showqueue();//显示队列信息
void start();
public:
PBC* front;//头指针
PBC* rear;//尾指针
PBC* head;//头结点
};
然后是实现
#include "StdAfx.h"
#include "Queue.h"
CQueue::CQueue(void)
{
head->name="",head->time=0,head->Runtime=0,head->priority=0,head->state=0,head->next=NULL;
}
CQueue::~CQueue(void)
{
}
void CQueue::InitQueue()
{
front=head;
rear=head;
}
bool CQueue::IsEmpty()
{
if (front==rear)
{
return true;
}
else
{
return false;
}
}
void CQueue::EnQueue(PBC* p)
{
if (IsEmpty())
{
head->next=p;
rear=p;
return;
}
PBC* phead=head;
while(phead->next!=NULL)
{
if (phead->next->priority > p->priority)
{
phead=phead->next;
}
}
if (phead->next==NULL)
{
phead->next=p;
}
else
{
p->next=phead->next;
phead->next=p;
}
}
void CQueue::DeQueue()
{
if (head->next->next==NULL)
{
head->next=NULL;
}
else
{
head->next=head->next->next;
}
}
void CQueue::showqueue()
{
cout<<"进程名:"<<head->next->name<<"\t"<<"到达时间:"<<head->next->time<<"\t"
<<"运行所需时间:"<<head->next->Runtime<<"\t"<<"优先级:"<<head->next->priority<<endl;
}
void CQueue::start()
{
PBC* p=new PBC;
while(head->next!=NULL)
{
showqueue();
head->next->Runtime--;
head->next->priority--;
p=head->next;
if(p->Runtime==0)
{
head->next->state='E';
DeQueue();
}
else
{
DeQueue();
EnQueue(p);
}
}
}
主程序中调用
// CPU.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include "Queue.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
PBC* p1=new PBC("p1",0,5,2,'R',NULL);
PBC* p2=new PBC("p2",0,4,4,'R',NULL);
PBC* p3=new PBC("p3",0,4,5,'R',NULL);
PBC* p4=new PBC("p4",0,3,3,'R',NULL);
PBC* p5=new PBC("p5",0,2,1,'R',NULL);
CQueue queue;
queue.InitQueue();
queue.EnQueue(p1);
queue.EnQueue(p2);
queue.EnQueue(p3);
queue.EnQueue(p4);
queue.EnQueue(p5);
queue.start();
system("pause");
return 0;
}
错误提示是:
应该是有空指针,加断点调试下
看下队栈调用关系,定位是哪一句报的异常