https://github.com/nomemory/c-generic-pqueue
A generic Priority Queue implementation in C using heaps - nothing fancy.
https://github.com/nomemory/c-generic-pqueue
Last synced: over 1 year ago
JSON representation
A generic Priority Queue implementation in C using heaps - nothing fancy.
- Host: GitHub
- URL: https://github.com/nomemory/c-generic-pqueue
- Owner: nomemory
- Created: 2017-04-03T14:50:27.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-04T09:10:46.000Z (over 8 years ago)
- Last Synced: 2025-03-22T06:32:34.689Z (over 1 year ago)
- Language: C
- Homepage:
- Size: 4.88 KB
- Stars: 12
- Watchers: 1
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Generic Priority Queue in C
"Academical" implementation of a priority queue in old C, nothing fancy.
I guess I (re)did this to keep my CS skills sharp.
### How it works.
The "constructor" method expects a comparator method and the capacity of the Queue:
```C
PQueue *pqueue_new(int (*cmp)(const void *d1, const void *d2), size_t capacity);
```
A simple comparator method for `int` values can look like:
```C
int cmp_ints(const void *int1, const void *int2) {
return *(int*) int1 - *(int*) int2;
}
```
Once we have the comparator method we can create the `PQueue` and elements to it (in our case int numbers):
```C
PQueue* pq = pqueue_new(cmp_ints, 200);
int x = 100, y = 50, z = 300, k = 100, w = 1000;
pqueue_enqueue(pq, &x);
pqueue_enqueue(pq, &y);
pqueue_enqueue(pq, &z);
pqueue_enqueue(pq, &k);
pqueue_enqueue(pq, &w);
int i = 0;
for(;i<5;++i)
printf("%d\n", *(int*) pqueue_dequeue(pq));
// De-allocate the memory (PQueue only, not the elements)
pqueue_delete(pq);
```
Output:
```
1000
300
100
100
50
```