This commit is contained in:
2025-09-15 21:12:04 +08:00
commit 3f58f483ff
144 changed files with 5298 additions and 0 deletions

View File

@@ -0,0 +1,128 @@
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef enum { INT_TYPE, FLOAT_TYPE, DOUBLE_TYPE } ElementType;
typedef struct {
ElementType type;
union {
int intValue;
float floatValue;
double doubleValue;
} data;
} Item;
typedef struct {
Item items[MAX_SIZE];
int size;
} PriorityQueue;
PriorityQueue createPriorityQueue() {
PriorityQueue pq;
pq.size = 0;
return pq;
}
bool isLess(Item item1, Item item2) {
if (item1.type != item2.type) {
fprintf(stderr, "错误:无法比较不同类型的元素。\n");
return false;
}
switch (item1.type) {
case INT_TYPE:
return item1.data.intValue < item2.data.intValue;
case FLOAT_TYPE:
return item1.data.floatValue < item2.data.floatValue;
case DOUBLE_TYPE:
return item1.data.doubleValue < item2.data.doubleValue;
default:
fprintf(stderr, "错误:未知的元素类型。\n");
return false;
}
}
void swap(Item* item1, Item* item2) {
Item temp = *item1;
*item1 = *item2;
*item2 = temp;
}
void push(PriorityQueue* pq, Item newItem) {
if (pq->size >= MAX_SIZE) {
fprintf(stderr, "错误:优先队列已满,无法插入元素。\n");
return;
}
int childIndex = pq->size;
pq->items[childIndex] = newItem;
pq->size++;
int parentIndex = (childIndex - 1) / 2;
while (childIndex > 0 && isLess(pq->items[childIndex], pq->items[parentIndex])) {
swap(&pq->items[childIndex], &pq->items[parentIndex]);
childIndex = parentIndex;
parentIndex = (childIndex - 1) / 2;
}
}
Item pop(PriorityQueue* pq) {
if (pq->size <= 0) {
fprintf(stderr, "错误:优先队列为空,无法弹出元素。\n");
Item emptyItem;
emptyItem.type = INT_TYPE; // 返回一个空元素
emptyItem.data.intValue = 0;
return emptyItem;
}
Item minItem = pq->items[0];
pq->size--;
pq->items[0] = pq->items[pq->size];
int parentIndex = 0;
while (true) {
int leftChildIndex = parentIndex * 2 + 1;
int rightChildIndex = parentIndex * 2 + 2;
int smallestIndex = parentIndex;
if (leftChildIndex < pq->size && isLess(pq->items[leftChildIndex], pq->items[smallestIndex])) {
smallestIndex = leftChildIndex;
}
if (rightChildIndex < pq->size && isLess(pq->items[rightChildIndex], pq->items[smallestIndex])) {
smallestIndex = rightChildIndex;
}
if (smallestIndex == parentIndex) {
break; // 已经是最小堆,无需继续下沉
}
swap(&pq->items[parentIndex], &pq->items[smallestIndex]);
parentIndex = smallestIndex;
}
return minItem;
}
int main() {
PriorityQueue pq = createPriorityQueue();
// 向优先队列中插入一些元素
Item item1, item2, item3;
item1.type = INT_TYPE;
item1.data.intValue = 5;
item2.type = INT_TYPE;
item2.data.intValue = 2;
item3.type = INT_TYPE;
item3.data.intValue = 10;
push(&pq, item1);
push(&pq, item2);
push(&pq, item3);
// 从优先队列中弹出元素
Item minItem = pop(&pq);
printf("弹出的最小元素为:%d\n", minItem.data.intValue);
return 0;
}

View File

@@ -0,0 +1,15 @@
# QUEUE
## PriorityQueue - 优先队列
- 优先队列也是一种队列只不过不同的是优先队列的出队顺序是按照优先级来的可能需要找到元素集合中的最小或者最大元素可以利用优先队列ADT来完成操作优先队列ADT是一种数据结构它支持插入和删除最小值操作返回并删除最小元素或删除最大值操作返回并删除最大元素
- 这些操作等价于队列的`enQueue``deQueue`操作,区别在于,对于优先队列,元素进入队列的顺序可能与其被操作的顺序不同,作业调度是优先队列的一个应用实例,它根据优先级的高低而不是先到先服务的方式来进行调度;
- 如果最小键值元素拥有最高的优先级,那么这种优先队列叫作**升序优先队列**(即总是先删除最小的元素),类似的,如果最大键值元素拥有最高的优先级,那么这种优先队列叫作**降序优先队列**(即总是先删除最大的元素);由于这两种类型时对称的,所以只需要关注其中一种,如升序优先队列;
- 优先队列的应用
- 数据压缩:赫夫曼编码算法;
- 最短路径算法Dijkstra算法
- 最小生成树算法Prim算法
- 事件驱动仿真:顾客排队算法;
- 选择问题查找第k个最小元素
- 等等等等....