MaxPriorityQueue
This commit is contained in:
8
LinearList/priorityQueue/CMakeLists.txt
Normal file
8
LinearList/priorityQueue/CMakeLists.txt
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.31)
|
||||||
|
project(priorityQueue)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
|
||||||
|
add_executable(priorityQueue main.cpp
|
||||||
|
maxPriorityQueue.h
|
||||||
|
maxPriorityQueueRealise.h)
|
57
LinearList/priorityQueue/main.cpp
Normal file
57
LinearList/priorityQueue/main.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "maxPriorityQueueRealise.h"
|
||||||
|
using namespace std;
|
||||||
|
int main() {
|
||||||
|
maxPriorityQueueRealise<string> q;
|
||||||
|
vector<pair<string, int>> v;
|
||||||
|
v.push_back(make_pair("first", 9));
|
||||||
|
v.push_back(make_pair("second", 8));
|
||||||
|
v.push_back(make_pair("third", 7));
|
||||||
|
v.push_back(make_pair("fourth", 6));
|
||||||
|
v.push_back(make_pair("fifth", 5));
|
||||||
|
v.push_back(make_pair("sixth", 4));
|
||||||
|
v.push_back(make_pair("seventh", 1));
|
||||||
|
v.push_back(make_pair("eighth", 2));
|
||||||
|
v.push_back(make_pair("ninth", 3));
|
||||||
|
q.push(v.at(4));
|
||||||
|
q.push(v.at(3));
|
||||||
|
q.push(v.at(2));
|
||||||
|
q.push(v.at(1));
|
||||||
|
q.push(v.at(0));
|
||||||
|
q.push(v.at(5));
|
||||||
|
q.push(v.at(6));
|
||||||
|
q.push(v.at(7));
|
||||||
|
q.push(v.at(8));
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
q.pop();
|
||||||
|
cout << q.top() << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
*发现789的三个顺序倒过来了,类的设计正确
|
||||||
|
*本类设计的是maxpriority,如果有重复的priority,优先处理先进入的,也就是FIFO
|
||||||
|
*如果有需求修改可以把比较的时候加入等号,变成FILO
|
||||||
|
first
|
||||||
|
second
|
||||||
|
third
|
||||||
|
fourth
|
||||||
|
fifth
|
||||||
|
sixth
|
||||||
|
ninth
|
||||||
|
eighth
|
||||||
|
seventh
|
||||||
|
*/
|
19
LinearList/priorityQueue/maxPriorityQueue.h
Normal file
19
LinearList/priorityQueue/maxPriorityQueue.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Created by PC on 25-8-9.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MAXPRIORITYQUEUE_H
|
||||||
|
#define MAXPRIORITYQUEUE_H
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class maxPriorityQueue {
|
||||||
|
public:
|
||||||
|
virtual ~maxPriorityQueue() = default;
|
||||||
|
virtual bool empty() const = 0;
|
||||||
|
virtual int size() const = 0;
|
||||||
|
virtual T& top() = 0;
|
||||||
|
virtual void pop() = 0;
|
||||||
|
virtual void push(const std::pair<T, int>& x) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //MAXPRIORITYQUEUE_H
|
60
LinearList/priorityQueue/maxPriorityQueueRealise.h
Normal file
60
LinearList/priorityQueue/maxPriorityQueueRealise.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
//
|
||||||
|
// Created by PC on 25-8-9.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MAXPRIORITYQUEUEREALISE_H
|
||||||
|
#define MAXPRIORITYQUEUEREALISE_H
|
||||||
|
#include "maxPriorityQueue.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class maxPriorityQueueRealise : public maxPriorityQueue<T> {
|
||||||
|
private:
|
||||||
|
std::vector<std::pair<T, int>> queue; //T是元素,int是优先等级
|
||||||
|
int qsize;
|
||||||
|
public:
|
||||||
|
maxPriorityQueueRealise() : qsize(0) {}
|
||||||
|
|
||||||
|
bool empty() const {return qsize == 0;}
|
||||||
|
int size() const {return qsize;}
|
||||||
|
T& top();
|
||||||
|
void pop();
|
||||||
|
void push(const std::pair<T, int>& x);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T& maxPriorityQueueRealise<T>::top() {
|
||||||
|
int max = -1;
|
||||||
|
int pos = 0;
|
||||||
|
int index = 0;
|
||||||
|
for ( std::pair<T, int> x : queue ) {
|
||||||
|
if (x.second > max) {
|
||||||
|
max = x.second;
|
||||||
|
pos = index;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
return queue[pos].first;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void maxPriorityQueueRealise<T>::pop() {
|
||||||
|
int max = -1;
|
||||||
|
int pos = 0;
|
||||||
|
int index = 0;
|
||||||
|
for ( std::pair<T, int> x : queue ) {
|
||||||
|
if (x.second > max) {
|
||||||
|
max = x.second;
|
||||||
|
pos = index;
|
||||||
|
}
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
queue.erase(queue.begin() + pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void maxPriorityQueueRealise<T>::push(const std::pair<T, int>& x) {
|
||||||
|
queue.push_back(x);
|
||||||
|
}
|
||||||
|
#endif //MAXPRIORITYQUEUEREALISE_H
|
Reference in New Issue
Block a user