C++中堆/优先队列的使用

堆的特性:完全二叉树
如果是调用priority_queue实现的堆,同时还满足有序(默认从大到小排序,即大根堆)和只能访问堆头。

#include <queue>//头文件
//1.1 定义一个堆
priority_queue<int> heap;//默认大根堆,堆头最大
//1.2 定义一个大根堆
priority_queue<int, vector<int>, less<int>> heap;//大根堆,堆头最大
//1.3 定义一个小根堆
priority_queue<int, vector<int>, greater<int>> heap;//小根堆,堆头最小
//2 插入元素x
heap.push(x);
//3 删除堆头
heap.pop();
//4 取堆头
heap.top();
//5 堆中元素个数
heap.size();

示例程序,

#include <iostream>
#include <queue>

using namespace std;

void my_show(priority_queue<int, vector<int>, greater<int>> h)
{
    while(h.size())
    {
        cout << h.top() << ' ';
        h.pop();
    }
    cout << endl;
}


int main()
{

    priority_queue<int, vector<int>, greater<int>> heap;//小根堆,greater表示越来越大

    heap.push(23);
    heap.push(9);
    heap.push(15);
    heap.push(10);
    heap.push(50);

    my_show(heap);

    //删除堆头后显示堆
    heap.pop();
    my_show(heap);

    //显示堆头
    cout << heap.top() << endl;

    //显示堆中的元素个数
    cout << heap.size() << endl;

    return 0;
}

输出为,

9 10 15 23 50
10 15 23 50
10
4
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 C 语言中,可以使用优先队列来实现基于优先级的元素插入和删除操作。C 语言并没有内置的优先队列数据结构,但我们可以使用来实现优先队列。 首先,需要定义一个结构体来表示队列中的元素,该结构体包含优先级和数据两个成员变量。例如: ```c typedef struct { int priority; // 优先级 int data; // 数据 } Element; ``` 然后,创建一个数组来保存队列中的元素。在插入和删除操作时,我们需要保持队列中元素的有序性,通常使用最小或最大来实现。在 C 语言中,可以使用数组来表示,并实现一些操作来维护的性质。 下面是一个简单的优先队列的实现示例: ```c #include <stdio.h> #define MAX_SIZE 100 typedef struct { int priority; int data; } Element; typedef struct { Element heap[MAX_SIZE]; int size; } PriorityQueue; void enqueue(PriorityQueue* queue, Element element) { if (queue->size >= MAX_SIZE) { printf("Queue is full!\n"); return; } int index = queue->size; while (index > 0 && element.priority < queue->heap[(index - 1) / 2].priority) { queue->heap[index] = queue->heap[(index - 1) / 2]; index = (index - 1) / 2; } queue->heap[index] = element; queue->size++; } Element dequeue(PriorityQueue* queue) { if (queue->size <= 0) { printf("Queue is empty!\n"); return (Element) {0, 0}; } Element result = queue->heap[0]; Element lastElement = queue->heap[queue->size - 1]; int index = 0; int childIndex = 1; while (childIndex < queue->size) { if (childIndex + 1 < queue->size && queue->heap[childIndex + 1].priority < queue->heap[childIndex].priority) { childIndex++; } if (lastElement.priority < queue->heap[childIndex].priority) { break; } queue->heap[index] = queue->heap[childIndex]; index = childIndex; childIndex = childIndex * 2 + 1; } queue->heap[index] = lastElement; queue->size--; return result; } int main() { PriorityQueue queue = {{0}, 0}; enqueue(&queue, (Element) {2, 20}); enqueue(&queue, (Element) {4, 40}); enqueue(&queue, (Element) {1, 10}); Element e1 = dequeue(&queue); printf("Dequeued element: priority=%d data=%d\n", e1.priority, e1.data); Element e2 = dequeue(&queue); printf("Dequeued element: priority=%d data=%d\n", e2.priority, e2.data); Element e3 = dequeue(&queue); printf("Dequeued element: priority=%d data=%d\n", e3.priority, e3.data); return 0; } ``` 在示例中,我们使用 `enqueue` 函数向优先队列中插入元素,使用 `dequeue` 函数从优先队列中删除并返回优先级最小的元素。在这个示例中,我们使用最小来实现优先队列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值