Work in progress
Implementing generic demand pattern lists. Compiles but does not run.
This commit is contained in:
@@ -108,8 +108,9 @@ void for_each_list(list_t *list, listIterator iterator)
|
||||
|
||||
list_node_t *node = list->head;
|
||||
bool result = true;
|
||||
while(node != NULL && result) {
|
||||
result = iterator(node);
|
||||
|
||||
while(node != NULL && result) {
|
||||
result = (iterator);
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
@@ -120,14 +121,17 @@ list_node_t *head_list(list_t *list, bool removeFromList)
|
||||
//
|
||||
{
|
||||
assert(list->head != NULL);
|
||||
|
||||
list_node_t *node = list->head;
|
||||
if(removeFromList) {
|
||||
// Disconnecting head node
|
||||
list->head = node->next;
|
||||
list->logicalLength--;
|
||||
|
||||
if (list) {
|
||||
list_node_t *node = list->head;
|
||||
if (removeFromList) {
|
||||
// Disconnecting head node
|
||||
list->head = node->next;
|
||||
list->logicalLength--;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
return node;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_node_t *tail_list(list_t *list)
|
||||
@@ -136,6 +140,18 @@ list_node_t *tail_list(list_t *list)
|
||||
return list->tail;
|
||||
}
|
||||
|
||||
list_node_t *get_nth_list(list_t *list, int index)
|
||||
{
|
||||
int n;
|
||||
list_node_t *lnode;
|
||||
|
||||
for (n = 1, lnode = first_list(list); n < index && done_list(lnode); n++, lnode = next_list(lnode));
|
||||
if (n != index)
|
||||
return NULL;
|
||||
else
|
||||
return lnode;
|
||||
}
|
||||
|
||||
int size_list(list_t *list)
|
||||
{
|
||||
return list->logicalLength;
|
||||
@@ -146,6 +162,11 @@ void *get_data(list_node_t *lnode)
|
||||
return lnode->data;
|
||||
}
|
||||
|
||||
list_node_t *get_next(list_node_t *lnode)
|
||||
{
|
||||
return lnode->next;
|
||||
}
|
||||
|
||||
void delete_node(list_t *list, list_node_t *lnode)
|
||||
{
|
||||
if (list->freeFn)
|
||||
@@ -154,29 +175,3 @@ void delete_node(list_t *list, list_node_t *lnode)
|
||||
free(lnode->data);
|
||||
free(lnode);
|
||||
}
|
||||
|
||||
//
|
||||
// Iterator first/done/next operations provide containment for list abstraction
|
||||
// http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Iterators.html
|
||||
// Accessed on April 11, 2019
|
||||
//
|
||||
list_node_t *first_list(list_t *list)
|
||||
{
|
||||
if (list)
|
||||
return list->head;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool done_list(list_node_t *lnode)
|
||||
{
|
||||
return lnode != NULL;
|
||||
}
|
||||
|
||||
list_node_t *next_list(list_node_t *lnode)
|
||||
{
|
||||
if (lnode)
|
||||
return lnode->next;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -28,8 +29,8 @@ extern "C" {
|
||||
typedef struct list_node_s list_node_t;
|
||||
typedef struct list_s list_t;
|
||||
|
||||
typedef void(*freeFunction)(void *);
|
||||
typedef bool(*listIterator)(list_node_t *);
|
||||
typedef void (*freeFunction) (void *);
|
||||
typedef bool (*listIterator) (list_node_t *);
|
||||
|
||||
|
||||
/**
|
||||
@@ -65,6 +66,11 @@ int size_list(list_t *list);
|
||||
*/
|
||||
void *get_data(list_node_t *lnode);
|
||||
|
||||
/**
|
||||
@brief Returns next list node.
|
||||
*/
|
||||
list_node_t *get_next(list_node_t *lnode);
|
||||
|
||||
/**
|
||||
@brief Frees memory associated with a list node.
|
||||
*/
|
||||
@@ -87,21 +93,32 @@ list_node_t *head_list(list_t *list, bool removeFromList);
|
||||
*/
|
||||
list_node_t *tail_list(list_t *list);
|
||||
|
||||
/**
|
||||
@brief Returns nth node of the list or NULL.
|
||||
*/
|
||||
list_node_t *get_nth_list(list_t *list, int index);
|
||||
|
||||
|
||||
//
|
||||
// Iterator first/done/next operations
|
||||
// http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Iterators.html
|
||||
// Accessed on April 11, 2019
|
||||
//
|
||||
|
||||
/**
|
||||
@brief Returns list head node.
|
||||
*/
|
||||
list_node_t *first_list(list_t *list);
|
||||
static inline list_node_t *first_list(list_t *list) { return head_list(list, false); }
|
||||
|
||||
/**
|
||||
@brief Returns true if end of list false otherwise.
|
||||
*/
|
||||
bool done_list(list_node_t *lnode);
|
||||
static inline bool done_list(list_node_t *lnode) { return lnode != NULL; }
|
||||
|
||||
/**
|
||||
@brief Returns next node in the list.
|
||||
*/
|
||||
list_node_t *next_list(list_node_t *lnode);
|
||||
static inline list_node_t *next_list(list_node_t *lnode) { return get_next(lnode); }
|
||||
|
||||
|
||||
#if defined(__cplusplus)
|
||||
|
||||
Reference in New Issue
Block a user