Restructured the example folder and added new examples

This commit is contained in:
Leonardo committed 2019-04-03 02:32:38 -03:00
1 parent eb16205224
commit 52e105995a
29 files changed
+442 -18

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
# Deque examples
* even_odd.c - separates odd numbers to one end of the deque and even number to the other. Sum only half of it to give the total sum of even numbers.
+51
View File
@@ -0,0 +1,51 @@
/**
* even_odd.c
*
* Creation Date: 03/04/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "deque.h"
DEQUE_GENERATE(d, deque, /* static */, size_t)
int main(int argc, char const *argv[])
{
// 1- Add odd numbers to the start of the deque and even numbers to the end.
// 2- Iterate over the deque, starting from the end, until index is half
// the deque count and stop.
// 3- Sum of all even numbers
deque *numbers = d_new(1000);
for (size_t i = 1; i <= 100000; i++)
{
i % 2 == 0 ? d_push_back(numbers, i) : d_push_front(numbers, i);
}
deque_iter iter;
d_iter_new(&iter, numbers);
size_t result, index, sum = 0;
for (d_iter_toend(&iter); !d_iter_start(&iter);)
{
d_iter_prev(&iter, &result, &index);
if ((double)index < (double)d_count(numbers) / 2.0)
break;
sum += result;
}
printf("Total sum: %lu\n", sum);
assert(sum == 2500050000);
return 0;
}