数据结构C++ 大根堆——大根堆的应用

时间:2021-06-23 10:33:57

两个应用实例,函数声明 application.h :

 1 #pragma once
2 #include "jobNode.h"
3
4 //堆排序
5 template<typename T>
6 void heapSort(T *dataArray, int arraySize)
7 {
8 maxHeap<T> Heap;
9 Heap.initialize(dataArray, arraySize);
10 for (int i = 0; i < arraySize; i++)
11 {
12 dataArray[i] = Heap.top();
13 cout << int(dataArray[i]) << " ";
14 Heap.pop();
15 }
16 cout << endl;
17 }
18
19
20 //LPT,最长处理时间机器调度
21 void LPTschedule(jobNode *Jobs, int jobSize, int machineSize);

机器调度算法所需要的结构定义在 jobNode.h 中:

 1 #pragma once
2
3 struct jobNode
4 {
5 int jobNo;
6 int Time;
7 jobNode() = default;
8 jobNode(int cNo, int cTime)
9 { jobNo = cNo; Time = cTime; }
10
11 operator int() const
12 { return Time; }
13 };
14
15
16 struct machineNode
17 {
18 int machineNo;
19 int Time;
20 machineNode() = default;
21 machineNode(int cNo, int cTime)
22 { machineNo = cNo; Time = cTime; }
23
24 operator int() const
25 { return Time; }
26 };

应用实例的实现 application.cpp :

 1 #include <iostream>
2 #include "jobNode.h"
3 #include "maxHeap.h"
4 #include "application.h"
5
6
7 using namespace std;
8
9
10 void LPTschedule(jobNode *Jobs, int jobSize, int machineSize)
11 {
12 if (jobSize <= machineSize)
13 {
14 cout << "jobSize <= machineSize" << endl;
15 return ;
16 }
17 int allTime = 0;
18 for (int k = 0; k < jobSize; k++)
19 allTime += Jobs[k].Time;
20
21 heapSort(Jobs, jobSize);
22
23 maxHeap<machineNode> Heap;
24 for (int i = 0; i < machineSize; i++)
25 {
26 machineNode jjj(i + 1, allTime);
27 Heap.push(jjj);
28 }
29 for (int j = 0; j < jobSize; j++)
30 {
31 machineNode machine = Heap.top();
32 Heap.pop();
33 cout << machine.machineNo << "号机器从 " << allTime - machine.Time
34 << "" << (allTime - machine.Time + Jobs[j].Time) << " 期间完成"
35 << j << "号任务" << endl;
36 machine.Time -= Jobs[j].Time;
37 Heap.push(machine);
38 }
39 }