diff options
| author | Tyge Løvset <[email protected]> | 2020-09-02 20:56:22 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-09-02 20:56:22 +0200 |
| commit | 7fd50323b46599090d5cd9ade43a7cdb69c1e145 (patch) | |
| tree | ad8db17822f117499356b374edc02a8db239acc3 /examples/queue.c | |
| parent | af12926547b1a09b4513ba149d13ea9f84d4c528 (diff) | |
| download | STC-modified-7fd50323b46599090d5cd9ade43a7cdb69c1e145.tar.gz STC-modified-7fd50323b46599090d5cd9ade43a7cdb69c1e145.zip | |
Added cqueue.h, cstack.h and renamed cpqueue.h to cprique.h
Diffstat (limited to 'examples/queue.c')
| -rw-r--r-- | examples/queue.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/queue.c b/examples/queue.c new file mode 100644 index 00000000..fa8700fe --- /dev/null +++ b/examples/queue.c @@ -0,0 +1,31 @@ +#include <stc/crandom.h>
+#include <stc/cqueue.h>
+#include <stdio.h>
+
+declare_clist(i, int);
+declare_cqueue(i, clist_i); // min-heap (increasing values)
+
+int main() {
+ int n = 10000000;
+ crand_rng32_t gen = crand_rng32_init(1234);
+ crand_uniform_i32_t dist = crand_uniform_i32_init(gen, 0, n);
+
+ cqueue_i queue = cqueue_i_init();
+
+ // Push ten million random numbers onto the queue.
+ for (int i=0; i<n; ++i)
+ cqueue_i_push(&queue, crand_uniform_i32(&dist));
+
+ // Push or pop on the queue ten million times
+ printf("%d\n", n);
+ for (int i=n; i>0; --i) {
+ int r = crand_uniform_i32(&dist);
+ if (r & 1)
+ ++n, cqueue_i_push(&queue, r);
+ else
+ --n, cqueue_i_pop(&queue);
+ }
+ printf("%d\n", n);
+ printf("%zu\n", n, cqueue_i_size(queue));
+ cqueue_i_destroy(&queue);
+}
|
