Skip to content

Commit

Permalink
[feature] more linkedlist functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Aug 17, 2024
1 parent 8d1505d commit 8c47a18
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
79 changes: 74 additions & 5 deletions include/ccontainer/cc_linkedlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ typedef struct cc_linkedlist_node {
struct cc_linkedlist_node *next;
} cc_linkedlist_node_t;

typedef struct cc_linkedlist {
cc_linkedlist_node_t *head;
size_t size;
} cc_linkedlist_t;

// =====================================================================
// Library Function Declarations
// =====================================================================
Expand All @@ -72,7 +77,15 @@ typedef struct cc_linkedlist_node {
* @param data_size size of the data for allocating memory for it
* @return cc_linkedlist_node_t*
*/
cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size);
cc_linkedlist_node_t *cc_linkedlist_node_create(void *data, size_t data_size);

/**
* @brief initialize an empty list
*
* @param list list that head node points to NULL
* @param data_size given by user
*/
void cc_linkedlist_init(cc_linkedlist_t *list, size_t data_size);

/**
* @brief gets the current size of linked list
Expand All @@ -82,6 +95,51 @@ cc_linkedlist_node_t *cc_linkedlist_create_node(void *data, size_t data_size);
*/
size_t cc_linkedlist_size(cc_linkedlist_node_t *head);

/**
* @brief insert data beggining of the list
*
* @param list list that data will be instered
* @param data data that will be insert
*/
void cc_linkedlist_inser_front(cc_linkedlist_t *list, void *data);

/**
* @brief insert data end of the list
*
* @param list list that data will be instered
* @param data data that will be insert
*/
void cc_linkedlist_inser_back(cc_linkedlist_t *list, void *data);

/**
* @brief delete the first node with given data
*
* @param list
* @param data
* @param cmp
*/
void cc_linkedlist_node_delete(cc_linkedlist_t *list, void *data,
int (*cmp)(void *, void *));

/**
* @brief search node with given data
*
* @param list
* @param data
* @param cmp
* @return cc_linkedlist_node_t* node that contains requested data
*/
cc_linkedlist_node_t *cc_linkedlist_node_search(cc_linkedlist_t *list,
void *data,
int (*cmp)(void *, void *));

/**
* @brief delete the entire list from heap
*
* @param list to delete
*/
void cc_linkedlist_free(cc_linkedlist_t *list);

// =====================================================================
// Function Definitions
// =====================================================================
Expand Down Expand Up @@ -126,6 +184,20 @@ size_t cc_linkedlist_size(cc_linkedlist_node_t *head)
return counter;
}

void cc_linkedlist_init(cc_linkedlist_t *list, size_t data_size)
{
list->head = NULL;
list->size = data_size;
}

void cc_linkedlist_inser_front(cc_linkedlist_t *list, void *data)
{
cc_linkedlist_node_t *newnode =
cc_linkedlist_create_node(data, list->size);
newnode->next = list->head;
list->head = newnode;
}

#endif // CC_LINKEDLIST_IMPLEMENTATION

#ifdef __cplusplus
Expand Down Expand Up @@ -174,7 +246,4 @@ AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/

// TODO:
// I should make a yasnippet out of this
*/
15 changes: 15 additions & 0 deletions test/source/cc_linkedlist_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#define CC_LINKEDLIST_IMPLEMENTATION
#include "../../include/ccontainer/cc_linkedlist.h"
#include "unity.h"
#include <stdlib.h>

void cc_linkedlist_init_test(void)
{
cc_linkedlist_t *list =
(cc_linkedlist_t *)malloc(sizeof(cc_linkedlist_t));
cc_linkedlist_init(list, 10);

TEST_ASSERT_EQUAL(list->size, 10);
TEST_ASSERT_EQUAL(list->head, NULL);
}
1 change: 1 addition & 0 deletions test/source/cc_vector_test.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#define CC_VECTOR_IMPLEMENTATION
#include "../../include/ccontainer/cc_vector.h"
#include "unity.h"
Expand Down
2 changes: 2 additions & 0 deletions test/source/ccontainer_test_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "unity.h"
#include "cc_vector_test.h"
#include "cc_linkedlist_test.h"

void setUp(void)
{
Expand All @@ -21,6 +22,7 @@ int main(void)
RUN_TEST(cc_vector_iterator_begin_test);
RUN_TEST(cc_vector_front_test);
RUN_TEST(cc_vector_back_test);
RUN_TEST(cc_linkedlist_init_test);

return UNITY_END();
}

0 comments on commit 8c47a18

Please sign in to comment.