Files
Data-Structure/LinearList/priorityQueue/maxPriorityQueueRealise.h
2025-08-09 00:52:57 +08:00

61 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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