优先级队列实现
#include <iostream>
#include <Windows.h>
#include <iomanip>
#define MAXSIZE 11
using namespace std;
typedef int DataType;
typedef struct _QNode {
DataType data;
int priority;
struct _QNode* next;
}QNode;
typedef QNode* QueuePtr;
typedef struct Queue {
int length;
QueuePtr front;
QueuePtr rear;
}LinkQueue;
void InitQueue(LinkQueue*& LQ) {
if (!LQ) { return; }
LQ->length = NULL;
LQ->front = LQ->rear = NULL;
}
bool IsEmpty(LinkQueue*& LQ) {
if (!LQ) { return false; }
if (LQ->front == NULL) { return true; }
return false;
}
bool IsFull(LinkQueue*& LQ) {
if (!LQ) { return false; }
if (LQ->length > MAXSIZE) { return true; }
return false;
}
bool EnterQueue(LinkQueue* LQ, DataType data, int priority) {
if (!LQ) { return false; }
if (IsFull(LQ)) { cout << "无法插入:" << data << "队列已满!" << endl; return false; }
QNode* qNode = new QNode;
qNode->data = data;
qNode->priority = priority;
qNode->next = NULL;
if (IsEmpty(LQ)) {
LQ->front = LQ->rear = qNode;
}
else {
LQ->rear->next = qNode;
LQ->rear = qNode;
}
LQ->length++;
return true;
}
bool DeleteQueue(LinkQueue* LQ, DataType& data) {
QNode** prev = NULL, * prev_node = NULL;
QNode* tmp = NULL, * last = NULL;
if (IsEmpty(LQ)) { cout << "队列为空!!" << endl; return false; }
prev = &(LQ->front);
printf("第一个节点的优先级:[%d]\n", (*prev)->priority);
last = LQ->front;
tmp = last->next;
while (tmp) {
if (tmp->priority > (*prev)->priority) {
printf("抓到一个更大的节点:[%d]\n", tmp->priority);
prev = &(last->next);
prev_node = last;
}
last = tmp;
tmp = tmp->next;
}
if (!&data) { return false; }
data = (*prev)->data;
tmp = *prev;
*prev = (*prev)->next;
delete tmp;
LQ->length--;
if (LQ->length == 0) {
LQ->rear = NULL;
}
if (prev_node && prev_node->next == NULL) {
LQ->rear = prev_node;
}
return true;
}
void PrintQueue(LinkQueue* LQ) {
QueuePtr tmp = NULL;
if (!LQ) { return; }
if (IsFull(LQ)) { cout << "队列为空!" << endl; return; }
tmp = LQ->front;
while (tmp) {
cout << setw(4) << tmp->data;
tmp = tmp->next;
}
cout << endl;
}
bool GetHead(LinkQueue* LQ, DataType& data) {
if (!LQ || IsEmpty(LQ)) { cout << "队列为空!!" << endl; return false; }
if (!data) { return false; }
data = LQ->front->data;
return true;
}
int getLength(LinkQueue* LQ) {
if (!LQ || IsEmpty(LQ)) { cout << "队列为空!!" << endl; return 0; }
return LQ->length;
}
void ClearQueue(LinkQueue* LQ) {
if (!LQ) return;
while (LQ->front) {
QueuePtr tmp = LQ->front;
LQ->front = tmp->next;
cout << "删除:" << tmp->data << "元素成功!" << endl;
delete tmp;
}
LQ->front = LQ->rear = NULL;
LQ->length = NULL;
}
void ClearQueue1(LinkQueue* LQ) {
if (!LQ) return;
while (LQ->front) {
QueuePtr tmp = LQ->front->next;
delete LQ->front;
LQ->front = tmp;
}
LQ->front = LQ->rear = NULL;
LQ->length = 0;
}
int main(void) {
LinkQueue* LQ = new LinkQueue;
DataType data = -1;
InitQueue(LQ);
for (int i = 0; i < 5; i++) {
EnterQueue(LQ, i + 10, i);
}
printf("队列中的元素(总共%d 个):", getLength(LQ)); PrintQueue(LQ);
cout<<endl;
for(int i=0; i<5; i++){
if(DeleteQueue(LQ, data)){
cout<<"出队的元素是:"<<data<<endl;
}else {
cout<<"出队失败!"<<endl;
}
}
printf("出队五个元素后,队列中剩下的元素[%d]:\n", getLength(LQ));
PrintQueue(LQ);
cout<<endl;
ClearQueue(LQ);
cout<<"清空队列!\n";
PrintQueue(LQ);
delete LQ;
system("pause");
return NULL;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/468cade6ec98011f83343ad60fa6cfa6.png)