数据结构c/c++--------------优先队列

原理

相比于普通队列,优先队列的节点多了一个优先级的变量,使在优先队列中的数据出队时,优先级高的先出队。
结构体定义
使用的是链表形式的队列,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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值