原理
相比于普通队列,优先队列的节点多了一个优先级的变量,使在优先队列中的数据出队时,优先级高的先出队。
结构体定义
使用的是链表形式的队列,Queue中的length记录队列的长度,节点Qnode包含数据,优先级和下一个节点的指针。
typedef struct _Qnode
{
int priority;
int data;
struct _Qnode* next;
}Qnode;
typedef struct _Queue
{
int length;
Qnode* front;
Qnode* rear;
}Queue;
代码实现
初始化
bool initQueue(Queue* q)
{
if (!q) return false;
q->length = 0;
q->rear = q->front = NULL;
return true;
}
销毁
void clearQueue(Queue* q)
{
if (!q) return;
Qnode* p = q->front;
while (p)
{
q->front = p->next;
delete p;
p = q->front;
}
q->length = 0;
q->front = q->rear = NULL;
}
遍历
这里又括号打印了每个数据的优先级
void printQueue(Queue* q)
{
if (!q) return;
Qnode* p = q->front;
while (p)
{
cout << p->data << "(" << p->priority << ")" << " ";
p = p->next;
}
cout << endl;
}
判断空队列
bool isEmpty(Queue* q)
{
if (!q) return false;
if (q->length == 0)
{
return true;
}
return false;
}
入队
没什么区别,加了一个优先级而已
bool enterQueue(Queue* q, int data, int priority)
{
if (!q) return false;
Qnode* node = new Qnode;
node->priority = priority;
node->next = NULL;
node->data = data;
if (q->length == 0)
{
q->front = q->rear = node;
}
else
{
q->rear->next = node;
q->rear = node;
}
q->length++;
return true;
}
出队
这里的出队,我们寻找整个队列中优先级最大的数据节点的地址,保存在二级指针prev里面,最大优先级节点前一个节点保存在prev_node中
bool deleteQueue(Queue* q)
{
if (!q) return false;
if (isEmpty(q)) return false;
Qnode** prev = NULL, *prev_node = NULL;
Qnode* last = NULL, *tmp = NULL;
prev = &(q->front);
last = q->front;
tmp = last->next;
while (tmp)
{
if (tmp->priority > (*prev)->priority)
{
prev = &(last->next); //保存最大优先级的节点
prev_node = last; //保存最大优先级前一个节点
}
last = tmp;
tmp = tmp->next;
}
tmp = *prev;
*prev = (*prev)->next;
delete tmp;
q->length--;
// 删除完只有头结点了
if (q->length == 0)
{
q->rear = NULL;
}
// 删除的是尾节点
if (prev_node && prev_node == NULL)
{
q->rear = prev_node;
}
return true;
}
完整代码
#include <iostream>
using namespace std;
typedef struct _Qnode
{
int priority;
int data;
struct _Qnode* next;
}Qnode;
typedef struct _Queue
{
int length;
Qnode* front;
Qnode* rear;
}Queue;
// 初始化
bool initQueue(Queue* q)
{
if (!q) return false;
q->length = 0;
q->rear = q->front = NULL;
return true;
}
// 销毁
void clearQueue(Queue* q)
{
if (!q) return;
Qnode* p = q->front;
while (p)
{
q->front = p->next;
delete p;
p = q->front;
}
q->length = 0;
q->front = q->rear = NULL;
}
// 遍历
void printQueue(Queue* q)
{
if (!q) return;
Qnode* p = q->front;
while (p)
{
cout << p->data << "(" << p->priority << ")" << " ";
p = p->next;
}
cout << endl;
}
// 判断队列为空
bool isEmpty(Queue* q)
{
if (!q) return false;
if (q->length == 0)
{
return true;
}
return false;
}
// 入队
bool enterQueue(Queue* q, int data, int priority)
{
if (!q) return false;
Qnode* node = new Qnode;
node->priority = priority;
node->next = NULL;
node->data = data;
if (q->length == 0)
{
q->front = q->rear = node;
}
else
{
q->rear->next = node;
q->rear = node;
}
q->length++;
return true;
}
// 出队
bool deleteQueue(Queue* q)
{
if (!q) return false;
if (isEmpty(q)) return false;
Qnode** prev = NULL, *prev_node = NULL;
Qnode* last = NULL, *tmp = NULL;
prev = &(q->front);
last = q->front;
tmp = last->next;
while (tmp)
{
if (tmp->priority > (*prev)->priority)
{
prev = &(last->next); //保存最大优先级的节点
prev_node = last; //保存最大优先级前一个节点
}
last = tmp;
tmp = tmp->next;
}
tmp = *prev;
*prev = (*prev)->next;
delete tmp;
q->length--;
// 删除完只有头结点了
if (q->length == 0)
{
q->rear = NULL;
}
// 删除的是尾节点
if (prev_node && prev_node == NULL)
{
q->rear = prev_node;
}
return true;
}
int main(void)
{
Queue* q = new Queue;
initQueue(q);
for (int i = 0; i < 5; i++)
{
enterQueue(q, i * 5, i);
}
printQueue(q);
deleteQueue(q);
printQueue(q);
clearQueue(q);
delete q;
return 0;
}