如何解决使用循环链表的循环算法
使用循环单链表来实现Round Robin流程调度算法,其中 为每个进程提供固定的执行时间(量子),并在该时间段后抢占 允许其他进程执行。假设已经准备好执行一组“ n”个流程。 读取时间量,并为每个进程读取总执行时间。 依次将进程命名为“ A”,“ B”,依此类推。每个节点应包含名称 过程的总执行时间和剩余执行时间。如果一个过程 完成其执行,在显示其名称和名称后将其从列表中删除 完成时间。
输入格式:
第一行包含“ n”的值,即进程数
第二行包含时间量
其余各行按顺序包含进程的总执行时间。
5
2
6
3
7
5
1
输出:
E 9
B 12
A 18
D 21
C 22
解决方法
#include <iostream>
using namespace std;
class node
{
public:
char name;
int tm;
int rt;
node *next;
};
class rr
{
public:
node * Head = NULL;
int j = 65;
void insert (int n)
{
node *nn = new node;
nn->name = j++;
nn->tm = n;
nn->rt = nn->tm;
if (Head == NULL)
{
Head = nn;
Head->next = Head;
}
else
{
node *temp = Head;
while (temp->next != Head)
temp = temp->next;
nn->next = temp->next;
temp->next = nn;
}
}
void quantum (int t)
{
node *temp = Head;
int c = 0,i = 0;
while (Head != NULL)
{
{
temp->rt = temp->rt - t;
c = c + t;
if (temp->rt <= 0)
{
c = c + temp->rt;
cout << temp->name;
cout << c << endl;
del (temp->name);
if (temp->next == temp)
{
break;
}
}
temp = temp->next;
}
}
}
void del (char x)
{
node *p = NULL;
node *temp = Head;
if (Head->name == x)
{
while (temp->next != Head)
temp = temp->next;
p = Head;
temp->next = Head->next;
Head = Head->next;
delete p;
}
else
{
while (temp->name != x)
{
p = temp;
temp = temp->next;
}
p->next = temp->next;
delete temp;
}
}
};
int
main ()
{
rr robin;
int i,n,x,y,t;
cin >> y;
cin >> t;
for (i = 0; i < y; i++)
{
cin >> n;
robin.insert (n);
}
robin.quantum (t);
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。