mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
128
dataStruct/Queue/PriorityQueue.c
Normal file
128
dataStruct/Queue/PriorityQueue.c
Normal 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;
|
||||
}
|
||||
15
dataStruct/Queue/README.md
Normal file
15
dataStruct/Queue/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# QUEUE
|
||||
|
||||
## PriorityQueue - 优先队列
|
||||
|
||||
- 优先队列也是一种队列,只不过不同的是,优先队列的出队顺序是按照优先级来的,可能需要找到元素集合中的最小或者最大元素,可以利用优先队列ADT来完成操作,优先队列ADT是一种数据结构,它支持插入和删除最小值操作(返回并删除最小元素)或删除最大值操作(返回并删除最大元素);
|
||||
|
||||
- 这些操作等价于队列的`enQueue`和`deQueue`操作,区别在于,对于优先队列,元素进入队列的顺序可能与其被操作的顺序不同,作业调度是优先队列的一个应用实例,它根据优先级的高低而不是先到先服务的方式来进行调度;
|
||||
- 如果最小键值元素拥有最高的优先级,那么这种优先队列叫作**升序优先队列**(即总是先删除最小的元素),类似的,如果最大键值元素拥有最高的优先级,那么这种优先队列叫作**降序优先队列**(即总是先删除最大的元素);由于这两种类型时对称的,所以只需要关注其中一种,如升序优先队列;
|
||||
- 优先队列的应用
|
||||
- 数据压缩:赫夫曼编码算法;
|
||||
- 最短路径算法:Dijkstra算法;
|
||||
- 最小生成树算法:Prim算法;
|
||||
- 事件驱动仿真:顾客排队算法;
|
||||
- 选择问题:查找第k个最小元素;
|
||||
- 等等等等....
|
||||
Reference in New Issue
Block a user