Custom linked list library in C.
You can use int list and string list in the same application without problem.
Include ./includes/list.h
, copy/paste ./list.c
in your project.
Edit list_init_data
, list_free_data
, list_data_eq
and list_dtoi
according to your need.
all: libs $(NAME)
# [...]
LIBLIST = vendor/liblist/
libs: $(LIBLIST)liblist.a
$(LIBLIST)liblist.a:
make -C $(LIBLIST)
libs_clean:
make -C $(LIBLIST) clean
libs_fclean: libs_clean
make -C $(LIBLIST) fclean
# [...]
fclean: libs_fclean clean
rm -f $(NAME)
Add libs libs_clean libs_fclean
to .PHONY
rule.
typedef struct s_list t_list;
struct s_list
{
t_list *next;
void *data;
};
Create a new list.
t_list *list_new(void *data);
Prepend an element on the list (add at the current position, most of the time at the beginning of the list).
Note: If you go to the second element and you unshift you will add the element between the first and the second element.
int list_unshift(t_list **plist, void *data);
Append an element on the list (add at the end of the list).
int list_push(t_list **plist, void *data);
Add or replace elements from slist to plist at the start position. The number of elements replaced or removed depend on deleteCount value.
t_list *list_splice(t_list **plist, int start, int deleteCount, t_list **slist);
Remove the first element and return the data.
Note: Use the return to free the data. Like: list_free_data(list_shift(plist))
.
void *list_pop(t_list **plist)
Remove the last element and return the data.
Note: Use the return to free the data. Like: list_free_data(list_pop(plist))
.
void *list_pop(t_list **plist);
Remove elements on the list at the start position. The number of elements removed depend on deleteCount value. deleteCount can be negative to delete all element after start position.
Note: Use the return to free the data with list_clear
.
t_list *list_slice(t_list **plist, int start, int deleteCount);
Call a function on every element in the list, and removes element if the given function returns false.
Note: Use the return to free the data with list_clear
.
t_list *list_map(t_list **plist, int (*map)(t_list **item, int index,
t_list **plist));
Remove&Free all the element of the list. You have to call list_create
, if you want a new list. By default, set free_data
to list_free_data
.
int list_clear(t_list **plist, void (*free_data)(void *data));
Returns the number of elements.
int list_length(t_list *plist);
Returns the max value.
int list_max_value(t_list *plist);
Returns the min value.
int list_min_value(t_list *plist);
Returns the t_info structure (min, max, size) of the list.
Note: Don't forget to recall the function after a change on the list.
typedef struct s_list_info
{
int max;
int min;
int size;
} t_info;
t_info list_int_info(t_list *plist);
Push data and returns true only if data are not in the list, or returns false if the element already present.
int list_push_uniq(t_list **plist, void *data);
Get the data in int format (this function call list_dtoi
before returns).
int list_get_int(t_list *list);
Get the string data.
Note: Only use with a string data.
char *list_get_str(t_list *list);
Get the last element of the list.
Note: Only use with a string data.
t_list *list_last(t_list **plist);
#include <stdio.h>
void printf_list(t_list *stack)
{
if (stack)
printf("%d ", *(int *)stack->data);
if (stack && stack->next)
printf_list(stack->next);
}
void list_print_str(t_list *plist)
{
if (!plist)
return ;
ft_putstr(plist->data);
ft_putstr("\n");
list_print_str(plist->next);
}