Adding key and search to list
This commit is contained in:
@@ -22,6 +22,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
@@ -29,6 +30,7 @@
|
|||||||
|
|
||||||
typedef struct list_node_s {
|
typedef struct list_node_s {
|
||||||
void *data;
|
void *data;
|
||||||
|
int key;
|
||||||
struct list_node_s *next;
|
struct list_node_s *next;
|
||||||
} list_node_t;
|
} list_node_t;
|
||||||
|
|
||||||
@@ -42,6 +44,9 @@ typedef struct list_s {
|
|||||||
} list_t;
|
} list_t;
|
||||||
|
|
||||||
|
|
||||||
|
// local declarations
|
||||||
|
int gen_key();
|
||||||
|
|
||||||
list_t *create_list(size_t elementSize, freeFunction freeFn)
|
list_t *create_list(size_t elementSize, freeFunction freeFn)
|
||||||
{
|
{
|
||||||
list_t *list;
|
list_t *list;
|
||||||
@@ -67,12 +72,14 @@ void delete_list(list_t *list)
|
|||||||
free(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));
|
list_node_t *node = malloc(sizeof(list_node_t));
|
||||||
node->data = malloc(list->elementSize);
|
node->data = malloc(list->elementSize);
|
||||||
memcpy(node->data, element, list->elementSize);
|
memcpy(node->data, element, list->elementSize);
|
||||||
|
|
||||||
|
node->key = gen_key();
|
||||||
|
|
||||||
node->next = list->head;
|
node->next = list->head;
|
||||||
list->head = node;
|
list->head = node;
|
||||||
|
|
||||||
@@ -82,14 +89,18 @@ void prepend_list(list_t *list, void *element)
|
|||||||
}
|
}
|
||||||
|
|
||||||
list->logicalLength++;
|
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));
|
list_node_t *node = malloc(sizeof(list_node_t));
|
||||||
node->data = malloc(list->elementSize);
|
node->data = malloc(list->elementSize);
|
||||||
node->next = NULL;
|
node->next = NULL;
|
||||||
|
|
||||||
|
node->key = gen_key();
|
||||||
|
|
||||||
memcpy(node->data, element, list->elementSize);
|
memcpy(node->data, element, list->elementSize);
|
||||||
|
|
||||||
if(list->logicalLength == 0) {
|
if(list->logicalLength == 0) {
|
||||||
@@ -100,8 +111,11 @@ void append_list(list_t *list, void *element)
|
|||||||
}
|
}
|
||||||
|
|
||||||
list->logicalLength++;
|
list->logicalLength++;
|
||||||
|
|
||||||
|
return node->key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void for_each_list(list_t *list, listIterator iterator)
|
void for_each_list(list_t *list, listIterator iterator)
|
||||||
{
|
{
|
||||||
assert(iterator != NULL);
|
assert(iterator != NULL);
|
||||||
@@ -110,15 +124,13 @@ void for_each_list(list_t *list, listIterator iterator)
|
|||||||
bool result = true;
|
bool result = true;
|
||||||
|
|
||||||
while(node != NULL && result) {
|
while(node != NULL && result) {
|
||||||
result = (iterator);
|
result = iterator(node);
|
||||||
node = node->next;
|
node = node->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
list_node_t *head_list(list_t *list, bool removeFromList)
|
list_node_t *head_list(list_t *list, bool removeFromList)
|
||||||
//
|
|
||||||
// Warning: When node is removed caller is responsible for freeing it.
|
// Warning: When node is removed caller is responsible for freeing it.
|
||||||
//
|
|
||||||
{
|
{
|
||||||
// assert(list->head != NULL);
|
// assert(list->head != NULL);
|
||||||
|
|
||||||
@@ -152,11 +164,31 @@ list_node_t *get_nth_list(list_t *list, int index)
|
|||||||
return lnode;
|
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)
|
int size_list(list_t *list)
|
||||||
{
|
{
|
||||||
return list->logicalLength;
|
return list->logicalLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int get_key(list_node_t *lnode)
|
||||||
|
{
|
||||||
|
return lnode->key;
|
||||||
|
}
|
||||||
|
|
||||||
void *get_data(list_node_t *lnode)
|
void *get_data(list_node_t *lnode)
|
||||||
{
|
{
|
||||||
return lnode->data;
|
return lnode->data;
|
||||||
@@ -175,3 +207,12 @@ void delete_node(list_t *list, list_node_t *lnode)
|
|||||||
free(lnode->data);
|
free(lnode->data);
|
||||||
free(lnode);
|
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);
|
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.
|
@brief Returns the number of items in the list.
|
||||||
@@ -66,6 +66,11 @@ int size_list(list_t *list);
|
|||||||
*/
|
*/
|
||||||
void *get_data(list_node_t *lnode);
|
void *get_data(list_node_t *lnode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Returns list node's key value.
|
||||||
|
*/
|
||||||
|
int get_key(list_node_t *lnode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief Returns next list node.
|
@brief Returns next list node.
|
||||||
*/
|
*/
|
||||||
@@ -98,6 +103,10 @@ list_node_t *tail_list(list_t *list);
|
|||||||
*/
|
*/
|
||||||
list_node_t *get_nth_list(list_t *list, int index);
|
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
|
// Iterator first/done/next operations
|
||||||
|
|||||||
@@ -13,7 +13,9 @@
|
|||||||
******************************************************************************
|
******************************************************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define BOOST_TEST_MODULE list
|
#define BOOST_TEST_MODULE list
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
@@ -35,7 +37,7 @@ int *get_int_data(list_node_t *lnode) {
|
|||||||
|
|
||||||
bool iterate_int(list_node_t *lnode)
|
bool iterate_int(list_node_t *lnode)
|
||||||
{
|
{
|
||||||
printf("Found value: %d\n", *get_int_data(lnode));
|
printf("At Key: %d Found value: %d\n", get_key(lnode), *get_int_data(lnode));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,16 +59,23 @@ BOOST_AUTO_TEST_CASE(test_int_list){
|
|||||||
int i, numbers = 10;
|
int i, numbers = 10;
|
||||||
list_t *list = NULL;
|
list_t *list = NULL;
|
||||||
|
|
||||||
|
int key[10 + 1];
|
||||||
|
|
||||||
|
srand((unsigned int)time(0));
|
||||||
|
|
||||||
list = create_list(sizeof(int), NULL);
|
list = create_list(sizeof(int), NULL);
|
||||||
|
|
||||||
for(i = 1; i <= numbers; i++) {
|
for(i = 1; i <= numbers; i++) {
|
||||||
append_list(list, &i);
|
key[i] = append_list(list, &i);
|
||||||
}
|
}
|
||||||
BOOST_CHECK(size_list(list) == 10);
|
BOOST_CHECK(size_list(list) == 10);
|
||||||
|
|
||||||
listIterator iterator = (listIterator)iterate_int;
|
listIterator iterator = (listIterator)iterate_int;
|
||||||
for_each_list(list, iterator);
|
for_each_list(list, iterator);
|
||||||
|
|
||||||
|
list_node_t *lnode = search_list(list, key[5]);
|
||||||
|
BOOST_CHECK(get_key(lnode) == key[5]);
|
||||||
|
|
||||||
delete_list(list);
|
delete_list(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,9 +188,15 @@ bool iterate_test_data(list_node_t *lnode)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *get_name(list_node_t *lnode)
|
||||||
|
{
|
||||||
|
return get_test_data(lnode)->name;
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(test_struct_list){
|
BOOST_AUTO_TEST_CASE(test_struct_list){
|
||||||
|
|
||||||
|
int key;
|
||||||
|
|
||||||
list_t *list = NULL;
|
list_t *list = NULL;
|
||||||
list = create_list(sizeof(test_data_t *), delete_test_data);
|
list = create_list(sizeof(test_data_t *), delete_test_data);
|
||||||
|
|
||||||
@@ -190,7 +205,7 @@ BOOST_AUTO_TEST_CASE(test_struct_list){
|
|||||||
append_list(list, &data);
|
append_list(list, &data);
|
||||||
|
|
||||||
data = create_test_data(2, "Kevin");
|
data = create_test_data(2, "Kevin");
|
||||||
append_list(list, &data);
|
key = append_list(list, &data);
|
||||||
|
|
||||||
data = create_test_data(3, "Michael");
|
data = create_test_data(3, "Michael");
|
||||||
append_list(list, &data);
|
append_list(list, &data);
|
||||||
@@ -202,16 +217,11 @@ BOOST_AUTO_TEST_CASE(test_struct_list){
|
|||||||
for_each_list(list, iterator);
|
for_each_list(list, iterator);
|
||||||
|
|
||||||
|
|
||||||
list_node_t *lnode;
|
// locate a list node by a key
|
||||||
// Iterate over list while maintaining containment of list abstraction
|
printf("Found %s!\n", get_name(search_list(list, key)));
|
||||||
for (lnode = first_list(list); done_list(lnode); lnode = next_list(lnode)) {
|
|
||||||
test_data_t *test_data = get_test_data(lnode);
|
|
||||||
|
|
||||||
if (test_data->num == 2)
|
|
||||||
printf("Found %s!\n", test_data->name);
|
|
||||||
}
|
|
||||||
|
|
||||||
lnode = head_list(list, true);
|
list_node_t *lnode = head_list(list, true);
|
||||||
delete_node(list, lnode);
|
delete_node(list, lnode);
|
||||||
|
|
||||||
delete_list(list);
|
delete_list(list);
|
||||||
|
|||||||
Reference in New Issue
Block a user