Added more list functions and examples
This commit is contained in:
1 parent
964ececf0b
commit
bc46ace086
5 files changed
+438
-6
No files matched your search
@@ -1,3 +1,4 @@
|
||||
# List examples
|
||||
|
||||
Currently none
|
||||
* list.c - Example of using a list and its iterator.
|
||||
* string.c - How to treat a list as a mutable string.
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* list.c
|
||||
*
|
||||
* Creation Date: 08/04/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "list.h"
|
||||
|
||||
LIST_GENERATE(l, list, /* FMOD */, int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// Create a list with internal capacity of 100
|
||||
list *my_list = l_new(100);
|
||||
|
||||
// - push_front (index: 0) [Complexity: O(N)]
|
||||
// - push (index: give) [Complexity: O(N)*]
|
||||
// - push_back (index: count) [Complexity: O(1)]
|
||||
//
|
||||
// - pop_front (index: 0) [Complexity: O(N)]
|
||||
// - pop (index: given) [Complexity: O(N)*]
|
||||
// - push_back (index: count - 1) [Complexity: O(1)]
|
||||
//
|
||||
// *average
|
||||
//
|
||||
// Average list Input/Output: O(N)
|
||||
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
// Add elements to the list
|
||||
l_push_back(my_list, i);
|
||||
}
|
||||
|
||||
// Erase all values from the list
|
||||
l_clear(my_list);
|
||||
|
||||
// Now that list is empty, add only even numbers
|
||||
for (int i = 0; i < 200; i++)
|
||||
{
|
||||
// Add even numbers to a given index. In this case they are being added
|
||||
// to the end of the list (count index == push_back). With a condition
|
||||
// of being even (i % 2 == 0)
|
||||
l_push_if(my_list, i, l_count(my_list), i % 2 == 0);
|
||||
}
|
||||
|
||||
// Iterator
|
||||
list_iter iter;
|
||||
int result, sum = 0;
|
||||
size_t index;
|
||||
|
||||
// Iterate over each element starting from the start (iter_new positions
|
||||
// the iterator to the start of the list) until the end of the list.
|
||||
for (l_iter_new(&iter, my_list); !l_iter_end(&iter);)
|
||||
{
|
||||
// Here index corresponds to the position of the result relative to all
|
||||
// other elements in the iteration. It is only a coincidence that the
|
||||
// index here is the same as the result's position in the list.
|
||||
l_iter_next(&iter, &result, &index);
|
||||
|
||||
// Do whatever you want with result and index, e.g.
|
||||
sum += result * index;
|
||||
}
|
||||
|
||||
// Release resources
|
||||
l_free(my_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* string.c
|
||||
*
|
||||
* Creation Date: 08/04/2019
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
LIST_GENERATE(str, string, /* FMOD */, char)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// How to treat a list as a mutable string without \0 at the end
|
||||
char *example1 = "Lorem ipsum";
|
||||
char *example2 = "dolor sit amet";
|
||||
char *example3 = "consectetur";
|
||||
|
||||
string *my_str = str_new_from(example2, strlen(example2));
|
||||
|
||||
// Add a single character to the front of the list
|
||||
str_push_front(my_str, ' ');
|
||||
|
||||
// Add a sequence of characters to the front of the list
|
||||
str_prepend(my_str, example1, strlen(example1));
|
||||
|
||||
// Add a charater to the back of the list
|
||||
str_push_back(my_str, ' ');
|
||||
|
||||
// Add a sequence of characters to the back of the list
|
||||
str_append(my_str, example3, strlen(example3));
|
||||
|
||||
// Iterate through each letter and print it
|
||||
string_iter iter;
|
||||
str_iter_new(&iter, my_str);
|
||||
size_t index;
|
||||
char result;
|
||||
|
||||
for (str_iter_tostart(&iter); !str_iter_end(&iter);)
|
||||
{
|
||||
str_iter_next(&iter, &result, &index);
|
||||
putchar(result);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// You can also print it reversed
|
||||
for (str_iter_toend(&iter); !str_iter_start(&iter);)
|
||||
{
|
||||
str_iter_prev(&iter, &result, &index);
|
||||
putchar(result);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
// Now remove the first letter supposing we don't know where it starts
|
||||
for (size_t i = 0; i < str_count(my_str); i++)
|
||||
{
|
||||
// Find first white space
|
||||
if (str_get(my_str, i) == ' ')
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Now that we know where the first word is, remove it, including the
|
||||
// extra space. [from, to] are inclusive.
|
||||
str_remove(my_str, 0, index);
|
||||
|
||||
// Print it once again
|
||||
for (str_iter_tostart(&iter); !str_iter_end(&iter);)
|
||||
{
|
||||
str_iter_next(&iter, &result, &index);
|
||||
putchar(result);
|
||||
}
|
||||
|
||||
printf("\nString length: %lu\n", str_count(my_str));
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user