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;
}
+3
View File
@@ -0,0 +1,3 @@
# Hashmap examples
Currently none
+3
View File
@@ -0,0 +1,3 @@
# Hashset examples
* set_ops.c - Demonstrates common set operations like *union*, *intersection*, *difference* and *symmetric difference*.
@@ -1,9 +1,25 @@
/**
* set_ops.c
*
* Creation Date: 03/04/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
// Demonstration of set operations like:
// - Union
// - Intersection
// - Difference
// - Symmetric Difference
#include <stdio.h>
#include <stdlib.h>
#include "../src/macro_collections.h"
#include "hashset.h"
// change to hashset to have the same effects
COLLECTION_GENERATE(TREESET, PUBLIC, set, set, , , int)
HASHSET_GENERATE(set, set, /* static */, int)
void print_set(set *s)
{
@@ -42,10 +58,8 @@ size_t inthash(int t)
int main(int argc, char const *argv[])
{
set *set1 = set_new(intcmp);
set *set2 = set_new(intcmp);
//set *set1 = set_new(50, 0.9, intcmp, inthash);
//set *set2 = set_new(50, 0.9, intcmp, inthash);
set *set1 = set_new(50, 0.9, intcmp, inthash);
set *set2 = set_new(50, 0.9, intcmp, inthash);
for (int i = 1; i <= 20; i++)
set_insert(set1, i);
+3
View File
@@ -0,0 +1,3 @@
# Heap examples
* heapsort.c - Uses the heap to sort the elements in a given array.
@@ -1,5 +1,14 @@
/**
* heapsort.c
*
* Creation Date: 25/03/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <stdio.h>
#include "../src/heap.h"
#include "heap.h"
static int intcmp(int a, int b)
{
+4
View File
@@ -0,0 +1,4 @@
# Linkedlist examples
* iterator.c - Example of using a linked list iterator.
* linked_list_node.c - Example of using functions that add or remove elements relative to a node.
@@ -1,10 +1,16 @@
/**
* iterator.c
*
* Creation Date: 22/03/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include "../src/macro_collections.h"
#include "linkedlist.h"
COLLECTION_GENERATE(LINKEDLIST, PUBLIC, ll, linked, static, , int)
LINKEDLIST_GENERATE(ll, linked, static, int)
int main(int argc, char const *argv[])
{
@@ -1,8 +1,17 @@
/**
* linked_list_node.c
*
* Creation Date: 26/03/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include <stdio.h>
#include <assert.h>
#include "../src/linkedlist.h"
#include "linkedlist.h"
LINKEDLIST_GENERATE(l, list, , int)
LINKEDLIST_GENERATE(l, list, /* static */, int)
int main(int argc, char const *argv[])
{
+3
View File
@@ -0,0 +1,3 @@
# List examples
Currently none
+5
View File
@@ -0,0 +1,5 @@
# Other examples
* book_store - A book store where its books are loaded onto a list from a csv file. The newly added ones go to a queue of featured books where old ones are removed to give place to new ones. You can find a book by looking up its ISBN using a treemap or traversing the treemap and find all ISBNs of a corresponding title.
* separate_header_source - Example of how to used `PUBLIC` and `PRIVATE` parameters.
* for_each.c - Using the `FOR_EACH`.
File renamed without changes.
File renamed without changes.
Internal Server Error - Gitea: Git with a cup of tea
500 Internal Server Error

An error occurred:

An error occurred

Gitea Version: 28.2.0