使用指针,按照优先数的大小,把五个进程连成队列,用指针指向下一个进程的首地址,最后一个进程中的指针为0
时间: 2024-01-30 07:02:41 浏览: 16
好的,我理解了你的问题。下面是一个示例代码,用于将五个进程按照优先数大小连接成队列:
```c++
#include <iostream>
using namespace std;
struct process {
int priority;
process* next;
};
int main() {
process* head = NULL;
process* tail = NULL;
// 创建五个进程
process* p1 = new process;
p1->priority = 3;
p1->next = NULL;
process* p2 = new process;
p2->priority = 1;
p2->next = NULL;
process* p3 = new process;
p3->priority = 5;
p3->next = NULL;
process* p4 = new process;
p4->priority = 2;
p4->next = NULL;
process* p5 = new process;
p5->priority = 4;
p5->next = NULL;
// 将进程按照优先数连接成队列
if (head == NULL) {
head = p1;
tail = p1;
}
else {
tail->next = p1;
tail = p1;
}
if (p2->priority > head->priority) {
p2->next = head;
head = p2;
}
else {
process* current = head;
while (current->next != NULL && p2->priority <= current->next->priority) {
current = current->next;
}
p2->next = current->next;
current->next = p2;
}
process* current = head;
while (current->next != NULL) {
current = current->next;
}
if (p3->priority > current->priority) {
current->next = p3;
}
else {
process* current = head;
while (current->next != NULL && p3->priority <= current->next->priority) {
current = current->next;
}
p3->next = current->next;
current->next = p3;
}
current = head;
while (current->next != NULL) {
current = current->next;
}
if (p4->priority > current->priority) {
current->next = p4;
}
else {
process* current = head;
while (current->next != NULL && p4->priority <= current->next->priority) {
current = current->next;
}
p4->next = current->next;
current->next = p4;
}
current = head;
while (current->next != NULL) {
current = current->next;
}
if (p5->priority > current->priority) {
current->next = p5;
}
else {
process* current = head;
while (current->next != NULL && p5->priority <= current->next->priority) {
current = current->next;
}
p5->next = current->next;
current->next = p5;
}
// 输出队列
current = head;
while (current != NULL) {
cout << current->priority << " ";
current = current->next;
}
cout << endl;
// 释放内存
current = head;
while (current != NULL) {
process* tmp = current;
current = current->next;
delete tmp;
}
return 0;
}
```
这段代码创建了五个进程,并将它们按照优先数大小连接成了一个队列,最后输出了队列中每个进程的优先数。注意,在连接进程时,需要考虑队列为空或者插入的进程应该成为队列头部的情况。同时,在最后一个进程中,需要将 `next` 指针设置为 `NULL`,表示队列的结尾。
希望这个示例代码可以解答你的问题!
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)