Adding key and search to list
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "list.h"
|
||||
@@ -29,6 +30,7 @@
|
||||
|
||||
typedef struct list_node_s {
|
||||
void *data;
|
||||
int key;
|
||||
struct list_node_s *next;
|
||||
} list_node_t;
|
||||
|
||||
@@ -42,6 +44,9 @@ typedef struct list_s {
|
||||
} list_t;
|
||||
|
||||
|
||||
// local declarations
|
||||
int gen_key();
|
||||
|
||||
list_t *create_list(size_t elementSize, freeFunction freeFn)
|
||||
{
|
||||
list_t *list;
|
||||
@@ -67,12 +72,14 @@ void delete_list(list_t *list)
|
||||
free(list);
|
||||
}
|
||||
|
||||
void prepend_list(list_t *list, void *element)
|
||||
int prepend_list(list_t *list, void *element)
|
||||
{
|
||||
list_node_t *node = malloc(sizeof(list_node_t));
|
||||
node->data = malloc(list->elementSize);
|
||||
memcpy(node->data, element, list->elementSize);
|
||||
|
||||
node->key = gen_key();
|
||||
|
||||
node->next = list->head;
|
||||
list->head = node;
|
||||
|
||||
@@ -82,14 +89,18 @@ void prepend_list(list_t *list, void *element)
|
||||
}
|
||||
|
||||
list->logicalLength++;
|
||||
|
||||
return node->key;
|
||||
}
|
||||
|
||||
void append_list(list_t *list, void *element)
|
||||
int append_list(list_t *list, void *element)
|
||||
{
|
||||
list_node_t *node = malloc(sizeof(list_node_t));
|
||||
node->data = malloc(list->elementSize);
|
||||
node->next = NULL;
|
||||
|
||||
node->key = gen_key();
|
||||
|
||||
memcpy(node->data, element, list->elementSize);
|
||||
|
||||
if(list->logicalLength == 0) {
|
||||
@@ -100,8 +111,11 @@ void append_list(list_t *list, void *element)
|
||||
}
|
||||
|
||||
list->logicalLength++;
|
||||
|
||||
return node->key;
|
||||
}
|
||||
|
||||
|
||||
void for_each_list(list_t *list, listIterator iterator)
|
||||
{
|
||||
assert(iterator != NULL);
|
||||
@@ -110,18 +124,16 @@ void for_each_list(list_t *list, listIterator iterator)
|
||||
bool result = true;
|
||||
|
||||
while(node != NULL && result) {
|
||||
result = (iterator);
|
||||
result = iterator(node);
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
list_node_t *head_list(list_t *list, bool removeFromList)
|
||||
//
|
||||
// Warning: When node is removed caller is responsible for freeing it.
|
||||
//
|
||||
{
|
||||
// assert(list->head != NULL);
|
||||
|
||||
|
||||
if (list) {
|
||||
list_node_t *node = list->head;
|
||||
if (removeFromList) {
|
||||
@@ -141,7 +153,7 @@ list_node_t *tail_list(list_t *list)
|
||||
}
|
||||
|
||||
list_node_t *get_nth_list(list_t *list, int index)
|
||||
{
|
||||
{
|
||||
int n;
|
||||
list_node_t *lnode;
|
||||
|
||||
@@ -152,11 +164,31 @@ list_node_t *get_nth_list(list_t *list, int index)
|
||||
return lnode;
|
||||
}
|
||||
|
||||
list_node_t *search_list(list_t *list, int key)
|
||||
// Naive list search. Will not perform for large lists.
|
||||
{
|
||||
list_node_t *lnode = first_list(list);
|
||||
|
||||
while (done_list(lnode)) {
|
||||
|
||||
if (get_key(lnode) == key)
|
||||
return lnode;
|
||||
|
||||
lnode = next_list(lnode);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int size_list(list_t *list)
|
||||
{
|
||||
return list->logicalLength;
|
||||
}
|
||||
|
||||
int get_key(list_node_t *lnode)
|
||||
{
|
||||
return lnode->key;
|
||||
}
|
||||
|
||||
void *get_data(list_node_t *lnode)
|
||||
{
|
||||
return lnode->data;
|
||||
@@ -175,3 +207,12 @@ void delete_node(list_t *list, list_node_t *lnode)
|
||||
free(lnode->data);
|
||||
free(lnode);
|
||||
}
|
||||
|
||||
|
||||
// local functions
|
||||
|
||||
int gen_key()
|
||||
// Naive key generator. No guarentee of uniqueness
|
||||
{
|
||||
return rand();
|
||||
}
|
||||
|
||||
@@ -46,14 +46,14 @@ with each node’s data pointer.
|
||||
void delete_list(list_t *list);
|
||||
|
||||
/**
|
||||
@brief Adds a node to the head of the list.
|
||||
@brief Adds a node to the head of the list and returns its key.
|
||||
*/
|
||||
void prepend_list(list_t *list, void *element);
|
||||
int prepend_list(list_t *list, void *element);
|
||||
|
||||
/**
|
||||
@brief Adds a node to the tail of the list.
|
||||
@brief Adds a node to the tail of the list and returns its key.
|
||||
*/
|
||||
void append_list(list_t *list, void *element);
|
||||
int append_list(list_t *list, void *element);
|
||||
|
||||
/**
|
||||
@brief Returns the number of items in the list.
|
||||
@@ -67,7 +67,12 @@ int size_list(list_t *list);
|
||||
void *get_data(list_node_t *lnode);
|
||||
|
||||
/**
|
||||
@brief Returns next list node.
|
||||
@brief Returns list node's key value.
|
||||
*/
|
||||
int get_key(list_node_t *lnode);
|
||||
|
||||
/**
|
||||
@brief Returns next list node.
|
||||
*/
|
||||
list_node_t *get_next(list_node_t *lnode);
|
||||
|
||||
@@ -94,10 +99,14 @@ 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.
|
||||
@brief Returns nth node of the list or NULL.
|
||||
*/
|
||||
list_node_t *get_nth_list(list_t *list, int index);
|
||||
|
||||
/**
|
||||
@brief Returns the list node with the given key or NULL.
|
||||
*/
|
||||
list_node_t *search_list(list_t *list, int key);
|
||||
|
||||
//
|
||||
// Iterator first/done/next operations
|
||||
|
||||
Reference in New Issue
Block a user