📄 seqpqueue.h
字号:
struct Process
{
int processnum; //进程名称
int priority; //进程优先级
int arrivetime; //进程到达时间
int needtime; //进程所需时间
int possessivetime; //占有CPU时间
char state; //进程状态
int processwaittime; //进程等待时间
void display()
{
cout<<"这是进程:"<<processnum <<endl;
cout<<"优先权:"<<priority<<endl;
cout<<"到达时间:"<<arrivetime<<endl;
cout<<"运行时间:"<<needtime<<endl;
cout<<"占用CPU时间:"<<possessivetime<<endl;
cout<<"目前状态:"<<state<<endl;
}
};
class SeqPQueue
{
private:
Process data[MaxQueueSize]; //顺序队列数组
int front; //队头指示器
int rear; //队尾指示器
int count; //元素个数计数器
public:
SeqPQueue(void) //构造函数
{front = rear = 0; count = 0;};
~SeqPQueue(void){}; //析构函数
void Append(const Process& item); //入队列
Process Delete(void); //出队列
Process GetFront(void)const; //取优先级最高元素
int NotEmpty(void)const //非空否
{return count != 0;};
};
void SeqPQueue::Append(const Process& item) //入队列
//把数据元素item插入队列作为当前的新队尾
{
if(count >= MaxQueueSize)
{
cout << "队列已满!" << endl;
exit(0);
}
data[rear] = item; //把元素item加在队尾
rear = rear + 1; ///队尾指示器加1
count++; //计数器加1
}
Process SeqPQueue::Delete(void) //出队列
//把优先级最高的元素出队列并由函数返回
//优先级相同时按先进先出的原则出队列
{
if(count == 0)
{
cout << "队列已空!" << endl;
exit(0);
}
Process max = data[0]; //初始选data[0]为优先级最高的元素
int maxIndex = 0; //maxIndex为优先级最高元素的下标
for(int i = 1 ; i < count; i++) //寻找优先级最高的元素及对应下标
if(data[i].priority > max.priority)
{
max = data[i];
maxIndex = i;
}
for(i = maxIndex+1; i < count; i++) //数据元素依次前移
data[i-1] = data[i];
count--; //计数器减1
rear--;
return max; //返回
}
Process SeqPQueue::GetFront(void)const //取优先级最高元素
//取优先级最高的元素并由函数返回
//优先级相同时按先进先出的原则取元素
{
if(count == 0)
{
cout << "队列空!" << endl;
exit(0);
}
Process max = data[0]; //初始选data[0]为优先级最高的元素
int maxIndex = 0; //maxIndex为优先级最高元素的下标
for(int i = 1 ; i < count ; i++) //寻找优先级最高的元素即对应下标
if(data[i].priority > max.priority)
{
max = data[i];
maxIndex = i;
}
return max; //返回
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -