Sixth update on iterators - LINKEDLIST

This commit is contained in:
Leonardo committed 2019-05-15 18:49:40 -03:00
1 parent 918b73ad51
commit 812d0240e3
11 files changed
+382 -136

No files matched your search

+1
View File
@@ -1,3 +1,4 @@
# Hashset examples
* iterator.c - A complete example of the hashset iterator
* set_ops.c - Demonstrates common set operations like *union*, *intersection*, *difference* and *symmetric difference*.
+1
View File
@@ -1,3 +1,4 @@
# Heap examples
* heapsort.c - Uses the heap to sort the elements in a given array.
* iterator.c - A complete example of the heap iterator
+1 -1
View File
@@ -7,9 +7,9 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "deque.h"
#include <stdio.h>
#include <stdlib.h>
#include "deque.h"
DEQUE_GENERATE(d, deque, static, int)
+1 -1
View File
@@ -7,9 +7,9 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "hashmap.h"
#include <stdio.h>
#include <stdlib.h>
#include "hashmap.h"
HASHMAP_GENERATE(hm, hmap, static, int, double)
+1 -1
View File
@@ -7,9 +7,9 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "hashset.h"
#include <stdio.h>
#include <stdlib.h>
#include "hashset.h"
HASHSET_GENERATE(hs, hset, static, int)
+1 -1
View File
@@ -7,9 +7,9 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "heap.h"
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
int intcmp(int a, int b)
{
+22 -21
View File
@@ -7,52 +7,53 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "linkedlist.h"
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
LINKEDLIST_GENERATE(ll, linked, static, int)
LINKEDLIST_GENERATE(ll, linkedlist, static, int)
int main(int argc, char const *argv[])
{
size_t i;
int r;
// Initialize linked list and add some elements
linked *ll = ll_new();
// Initialize list and add some elements
linkedlist *l = ll_new();
for (int i = 0; i < 10; i++)
ll_push_back(ll, i);
ll_push_back(l, i);
// Initialize iterator
linked_iter iter;
ll_iter_init(&iter, ll);
linkedlist_iter iter;
ll_iter_init(&iter, l);
// Make the iterator go back and forward
for (int j = 0; j < 4; j++)
{
while (ll_iter_next(&iter, &r, &i))
printf("C[%2d] = %2d\n", i, r);
while (!ll_iter_end(&iter))
{
printf("C[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
ll_iter_next(&iter);
}
printf("\n");
while (ll_iter_prev(&iter, &r, &i))
printf("C[%2d] = %2d\n", i, r);
while (!ll_iter_start(&iter))
{
printf("C[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
ll_iter_prev(&iter);
}
printf("\n\n");
}
for (ll_iter_tostart(&iter); !ll_iter_end(&iter); /**/)
for (ll_iter_to_start(&iter); !ll_iter_end(&iter); ll_iter_next(&iter))
{
ll_iter_next(&iter, &r, &i);
printf("LL[%2d] = %2d\n", i, r);
printf("LL[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
}
printf("\n");
for (ll_iter_toend(&iter); !ll_iter_start(&iter); /**/)
for (ll_iter_to_end(&iter); !ll_iter_start(&iter); ll_iter_prev(&iter))
{
ll_iter_prev(&iter, &r, &i);
printf("LL[%2d] = %2d\n", i, r);
printf("LL[%2d] = %2d\n", ll_iter_index(&iter), ll_iter_value(&iter));
}
ll_free(ll);
ll_free(l);
return 0;
}
+1 -4
View File
@@ -7,17 +7,14 @@
* Leonardo Vencovsky (https://github.com/LeoVen)
*
*/
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
LIST_GENERATE(l, list, static, int)
int main(int argc, char const *argv[])
{
size_t i;
int r;
// Initialize list and add some elements
list *l = l_new(10);
for (int i = 0; i < 10; i++)
+1 -1
View File
@@ -1,4 +1,4 @@
# Linkedlist examples
* iterator.c - Example of using a linked list iterator.
* iterator.c - A complete example of the linked list iterator
* linked_list_node.c - Example of using functions that add or remove elements relative to a node.
+172 -23
View File
@@ -1,46 +1,195 @@
/**
* iterator.c
*
* Creation Date: 22/03/2019
* Creation Date: 10/05/2019
*
* Authors:
* Leonardo Vencovsky (https://github.com/LeoVen)
*
* Iterator Example
*/
#include <stdio.h>
#include "linkedlist.h"
#include <stdio.h>
#include <inttypes.h>
LINKEDLIST_GENERATE(ll, linked, static, int)
// Creates a linkedlist of integers
LINKEDLIST_GENERATE(ll, linkedlist, /* FMOD */, int)
int main(int argc, char const *argv[])
{
size_t i;
int r;
// Create a new linkedlist
linkedlist *my_linkedlist = ll_new();
// Initliazie linked list and add some elements
linked *ll = ll_new();
// Add elements to the linkedlist
for (int i = 0; i < 20; i++)
ll_push_back(ll, i);
{
ll_push_back(my_linkedlist, i);
}
// Initialize iterator
linked_iter iter;
ll_iter_init(&iter, ll);
// Iterate from the start (index 0) to the end (index count - 1) of the linkedlist
for (linkedlist_iter it = my_linkedlist->it_start(my_linkedlist); // Initialize iterator 'it' to the start of the linkedlist
!ll_iter_end(&it); // Conditional stop (until it is not at the end)
ll_iter_next(&it)) // Iterator goes one position forward
{
// Access the current element
int e = ll_iter_value(&it);
// Move cursor to the end of the list
ll_iter_toend(&iter);
// While there is a previous element on the list
while (ll_iter_prev(&iter, &r, &i))
printf("list[%2d] = %2d\n", i, r);
// Access the current index (index is relative to all the elements in the iteration)
size_t i = ll_iter_index(&it);
printf("\n");
// Print linkedlist
// Move cursor to the start of the list
ll_iter_tostart(&iter);
// While there is a next element on the list
while (ll_iter_next(&iter, &r, &i))
printf("list[%2d] = %2d\n", i, r);
// Index is useful in situations like this
// If it is the first element
if (i == 0)
printf("[ %d, ", e);
// If it is the last element
else if (i == ll_count(my_linkedlist) - 1)
printf("%d ]\n", e);
// For elements at the middle
else
printf("%d, ", e);
}
ll_free(ll);
// Index can also be used to iterate over a certain portion of a collection
size_t limit = ll_count(my_linkedlist) / 2;
for (linkedlist_iter it = my_linkedlist->it_start(my_linkedlist); !ll_iter_end(&it); ll_iter_next(&it))
{
// Access the current element
int e = ll_iter_value(&it);
// Access the current index (index is relative to all the elements in the iteration)
size_t i = ll_iter_index(&it);
// Print linkedlist up until limit
// If it is the first element
if (i == 0)
printf("[ %d, ", e);
// If it is the last element
else if (i == limit - 1) // -1 because index is 0 based and we want half of the linkedlist
{
printf("%d ]\n", e);
break;
}
// For elements at the middle
else
printf("%d, ", e);
}
// Going backwards
for (linkedlist_iter it = my_linkedlist->it_end(my_linkedlist); // Initialize iterator 'it' to the end of the linkedlist
!ll_iter_start(&it); // Conditional stop (until it is not at the start)
ll_iter_prev(&it)) // Iterator goes one position backwards
{
// Access the current element
int e = ll_iter_value(&it);
// Access the current index (index is relative to all the elements in the iteration)
size_t i = ll_iter_index(&it);
// Print linkedlist
// Note how the conditions for the first and last elements are simply swapped
// Because iterating backwards make the first element be the index count - 1
// and the last one be the index 0
// If it is the first element
if (i == ll_count(my_linkedlist) - 1)
printf("[ %d, ", e);
// If it is the last element
else if (i == 0)
printf("%d ]\n", e);
// For elements at the middle
else
printf("%d, ", e);
}
// You can also use iterators in a while loop
// But to do so you must declare the iterator struct outside the loop first
linkedlist_iter it;
// You have three options:
// - Initialize it using the functions provided by the linkedlist [ my_linkedlist->it_start() and my_linkedlist->it_end() ]
// - Initialize it using the init() function (when the iterator is already allocated)
// - Initialize it using the new() function (allocates on the heap, pointers only)
it = my_linkedlist->it_start(my_linkedlist);
it = my_linkedlist->it_end(my_linkedlist);
// This is equivalent to it_start()
ll_iter_init(&it, my_linkedlist);
// For pointers
// Also equivalent to it_start()
linkedlist_iter *iter = ll_iter_new(my_linkedlist);
// The actual while loop
while (!ll_iter_end(iter))
{
// Accessing elements
int e = ll_iter_value(iter);
size_t i = ll_iter_index(iter);
// Print index and element
printf("linkedlist[%2" PRIuMAX "] = %2d\n", i, e);
// Move iterator forwards
ll_iter_next(iter);
}
// Send the iterator to the start of the linkedlist
ll_iter_to_start(iter);
// Send the iterator to the end of the linkedlist
ll_iter_to_end(iter);
// Backwards loop
while (!ll_iter_start(iter))
{
// Accessing elements
int e = ll_iter_value(iter);
size_t i = ll_iter_index(iter);
// Print index and element
printf("linkedlist[%2" PRIuMAX "] = %2d\n", i, e);
// Move iterator backwards
ll_iter_prev(iter);
}
// Certain iterators also allow you to modify the contents it is iterating over
// Exceptions are collections that depend of the current state of the element
// like tree-based collections and heap (collections that have some kind of ordering).
// You also can't change the keys of an associative collection
// Change every element that is divisible by 2 to the number 7
for (ll_iter_to_start(&it); !ll_iter_end(&it); ll_iter_next(&it))
{
// Get a reference to the current value
int *r = ll_iter_rvalue(&it);
if (r != NULL && *r % 2 == 0)
*r = 7;
}
// Now print it to see the difference
for (linkedlist_iter it = my_linkedlist->it_start(my_linkedlist); !ll_iter_end(&it); ll_iter_next(&it))
{
int e = ll_iter_value(&it);
size_t i = ll_iter_index(&it);
if (i == 0)
printf("[ %d, ", e);
else if (i == ll_count(my_linkedlist) - 1)
printf("%d ]\n", e);
else
printf("%d, ", e);
}
// Dispose the iterator called by ll_iter_new()
ll_iter_free(iter);
// Dispose the linkedlist created
ll_free(my_linkedlist);
return 0;
}