Generic Linked List Library (like C++ STL list) - version 1.0

From: Amit
Date: Thu Oct 20 2022 - 02:40:05 EST


Generic Linked List Library (like C++ STL list) - version 1.0

The code is below:

-------------------------------------
generic_linked_list_library.c
-------------------------------------

/*
* License: This file has been released under APACHE LICENSE, VERSION 2.0.
* The license details can be found here:
https://www.apache.org/licenses/LICENSE-2.0
*/

#include <stdlib.h>
#include <string.h>

#include "generic_linked_list_library.h"

struct generic_linked_list_container
*init_generic_linked_list_container(before_deleting_data_callback_function
func)
{

struct generic_linked_list_container *gllc_ptr = malloc(sizeof(*gllc_ptr));

if (!gllc_ptr)
return NULL;

gllc_ptr->first = NULL;
gllc_ptr->last = NULL;
gllc_ptr->bdd_callback_func = func;
gllc_ptr->total_number_of_nodes = 0;

return gllc_ptr;

} // end of init_generic_linked_list_container

int add_new_node_to_end(struct generic_linked_list_container
*gllc_ptr, void *data, long data_len)
{

struct linked_list_node *lln = NULL;
struct node_data *nddt = NULL;

if (!data)
return ARG_DATA_IS_NULL;

if (data_len <= 0)
return DATA_LEN_IS_INVALID;

lln = malloc(sizeof(*lln));
if (!lln) {
return NO_MEMORY;
}

nddt = malloc(sizeof(*nddt));
if (!nddt) {
free(lln);
return NO_MEMORY;
}

nddt->data = malloc((size_t)(data_len));
if (!nddt->data) {
free(nddt);
free(lln);
return NO_MEMORY;
}

nddt->data_len = data_len;
memmove(nddt->data, data, (size_t)(nddt->data_len));

lln->nd_ptr = nddt;
lln->next = NULL;

if (gllc_ptr->total_number_of_nodes == 0) {
gllc_ptr->first = lln;
gllc_ptr->last = lln;
} else {
gllc_ptr->last->next = lln;
}

gllc_ptr->total_number_of_nodes = gllc_ptr->total_number_of_nodes + 1;

return GLLL_SUCCESS;

} // end of add_new_node_to_end

struct node_data *remove_front_node_and_get_data(struct
generic_linked_list_container *gllc_ptr)
{

struct linked_list_node *lln_ptr = NULL;
struct node_data *nddt = NULL;

lln_ptr = remove_front_node(gllc_ptr);
if (!lln_ptr)
return NULL;

nddt = lln_ptr->nd_ptr;

free(lln_ptr);

return nddt;

} // end of remove_front_node_and_get_data

struct linked_list_node *remove_front_node(struct
generic_linked_list_container *gllc_ptr)
{

struct linked_list_node *lln_ptr = NULL;

if (gllc_ptr->total_number_of_nodes == 0)
return NULL;

lln_ptr = gllc_ptr->first;

if (gllc_ptr->total_number_of_nodes == 1) {
gllc_ptr->first = NULL;
gllc_ptr->last = NULL;
} else {
gllc_ptr->first = gllc_ptr->first->next;
}

gllc_ptr->total_number_of_nodes = gllc_ptr->total_number_of_nodes - 1;

lln_ptr->next = NULL;

return lln_ptr;

} // end of remove_front_node

void delete_front_node(struct generic_linked_list_container *gllc_ptr)
{

struct linked_list_node *lln_ptr = NULL;
struct node_data *nddt = NULL;

lln_ptr = remove_front_node(gllc_ptr);
if (!lln_ptr)
return;

nddt = lln_ptr->nd_ptr;

if (gllc_ptr->bdd_callback_func)
gllc_ptr->bdd_callback_func(nddt);

free(nddt);
free(lln_ptr);

return;

} // end of delete_front_node

-------------------------------------
generic_linked_list_library.h
-------------------------------------

/*
* License: This file has been released under APACHE LICENSE, VERSION 2.0.
* The license details can be found here:
https://www.apache.org/licenses/LICENSE-2.0
*/

#ifndef _GENERIC_LINKED_LIST_LIBRARY_H_
#define _GENERIC_LINKED_LIST_LIBRARY_H_

#define GLLL_SUCCESS 0 // everything happened successfully
#define ARG_DATA_IS_NULL -1 // 'data' argument is NULL
#define DATA_LEN_IS_INVALID -2 // 'data_len' argument is <= 0
#define NO_MEMORY -3 // no memory available

struct node_data
{
void *data;
long data_len;
};

struct linked_list_node
{
struct node_data *nd_ptr;
struct linked_list_node *next;
};

typedef void (*before_deleting_data_callback_function)(struct node_data *);

struct generic_linked_list_container
{
struct linked_list_node *first;
struct linked_list_node *last;
before_deleting_data_callback_function bdd_callback_func;
long total_number_of_nodes;
};

struct generic_linked_list_container
*init_generic_linked_list_container(before_deleting_data_callback_function
func);
int add_new_node_to_end(struct generic_linked_list_container
*gllc_ptr, void *data, long data_len);
struct node_data *remove_front_node_and_get_data(struct
generic_linked_list_container *gllc_ptr);
struct linked_list_node *remove_front_node(struct
generic_linked_list_container *gllc_ptr);
void delete_front_node(struct generic_linked_list_container *gllc_ptr);

#endif

---- End of code ----