libft
Loading...
Searching...
No Matches
Files | Data Structures | Typedefs | Functions
Linked List Utilities

Singly linked list manipulation functions. More...

Files

file  ft_list.c
 Linked list operations for libft.
 
file  ft_list.h
 Linked list for libft.
 

Data Structures

struct  s_list
 

Typedefs

typedef struct s_list t_list
 Node structure for singly linked list.
 

Functions

void ft_del (void *content)
 Deletes content pointer in a list node (utility function).
 
void ft_lstadd_back (t_list **lst, t_list *new)
 Adds a node to the end of a list.
 
void ft_lstadd_front (t_list **lst, t_list *new)
 Adds a node to the front of a list.
 
void ft_lstclear (t_list **lst, void(*del)(void *))
 Clears an entire list, deleting all its nodes.
 
void ft_lstdelone (t_list *lst, void(*del)(void *))
 Deletes a single node using a deletion function.
 
void ft_lstiter (t_list *lst, void(*f)(void *))
 Applies a function to each element of the list.
 
t_listft_lstlast (t_list *lst)
 Returns the last node of the list.
 
t_listft_lstmap (t_list *lst, void *(*f)(void *), void(*del)(void *))
 Creates a new list by applying a function to each node.
 
t_listft_lstnew (void *content)
 Creates a new list node.
 
int ft_lstsize (t_list *lst)
 Counts the number of nodes in a list.
 

Detailed Description

Singly linked list manipulation functions.

This group contains all the functions related to the manipulation of singly linked lists, including creation, insertion, deletion, traversal, and transformation.

This group includes:

These utilities operate on a t_list structure.


Data Structure Documentation

◆ s_list

struct s_list
+ Collaboration diagram for s_list:

Data Fields

void * content
 Pointer to the node's content.
 
struct s_listnext
 Pointer to the next node.
 

Field Documentation

◆ content

void* s_list::content

Pointer to the node's content.

◆ next

struct s_list* s_list::next

Pointer to the next node.

Typedef Documentation

◆ t_list

#include <include/ft_list.h>

Node structure for singly linked list.

Each node contains a generic void* content pointer and a pointer to the next node.

Function Documentation

◆ ft_del()

void ft_del ( void *  content)

#include <include/ft_list.h>

Deletes content pointer in a list node (utility function).

Frees the memory pointed to by content.

Parameters
contentThe content pointer to free.

This function is typically used as a helper for linked list functions that require a function pointer to properly deallocate memory associated with a node's content. It performs a simple free() operation on the pointer passed.

Parameters
contentA pointer to the memory to be freed.

◆ ft_lstadd_back()

void ft_lstadd_back ( t_list **  lst,
t_list new 
)

#include <include/ft_list.h>

Adds a node to the end of a list.

Adds a new node to the end of a linked list.

Parameters
lstThe address of the pointer to the first node.
newThe node to add to the end.

If the list is empty, the new node becomes the first node. Otherwise, it is appended to the end of the list.

Parameters
lstA pointer to the pointer to the first node of the list.
newThe new node to add to the end of the list.
See also
ft_lstlast

◆ ft_lstadd_front()

void ft_lstadd_front ( t_list **  lst,
t_list new 
)

#include <include/ft_list.h>

Adds a node to the front of a list.

Adds a new node to the beginning of a linked list.

Parameters
lstThe address of the pointer to the first node.
newThe new node to add.

If both the list and the new node are valid, the new node is inserted at the front of the list, and becomes the new head.

Parameters
lstA pointer to the pointer to the first node of the list.
newThe new node to add to the beginning of the list.

◆ ft_lstclear()

void ft_lstclear ( t_list **  lst,
void(*)(void *)  del 
)

#include <include/ft_list.h>

Clears an entire list, deleting all its nodes.

Frees all nodes of a list and sets the list pointer to NULL.

Parameters
lstThe address of the pointer to the list.
delThe function to delete each node's content.

This function iterates through each node of a linked list, deletes the content of each node using the provided del function, frees the node itself, and finally sets the list pointer to NULL.

Parameters
lstA pointer to the pointer to the first node of the list.
delA pointer to the function used to delete the content of each node.
See also
ft_lstdelone

◆ ft_lstdelone()

void ft_lstdelone ( t_list lst,
void(*)(void *)  del 
)

#include <include/ft_list.h>

Deletes a single node using a deletion function.

Deletes and frees a single list node.

Parameters
lstThe node to delete.
delThe function to delete the content.

Frees the memory of the given list node and its content using the del function. The next pointer of the node is not accessed or freed.

Parameters
lstA pointer to the node to delete.
delA pointer to the function used to free the content.

◆ ft_lstiter()

void ft_lstiter ( t_list lst,
void(*)(void *)  f 
)

#include <include/ft_list.h>

Applies a function to each element of the list.

Iterates over a list and applies a function to each node's content.

Parameters
lstThe beginning of the list.
fThe function to apply to each node's content.

This function traverses the given linked list and applies the function f to the content of each node. It does not allocate or free any memory, and it does not modify the list structure.

Parameters
lstPointer to the first node of the list.
fFunction to apply to the content of each node.
Note
The function f should not modify the list structure.

◆ ft_lstlast()

t_list * ft_lstlast ( t_list lst)

#include <include/ft_list.h>

Returns the last node of the list.

Retrieves the last node of a linked list.

Parameters
lstThe beginning of the list.
Returns
The last node, or NULL if the list is empty.

Traverses the linked list starting from the head until the last node (the node with next == NULL) is found.

Parameters
lstPointer to the head of the list.
Returns
A pointer to the last node in the list, or NULL if the list is empty.

◆ ft_lstmap()

t_list * ft_lstmap ( t_list lst,
void *(*)(void *)  f,
void(*)(void *)  del 
)

#include <include/ft_list.h>

Creates a new list by applying a function to each node.

Applies a function to each element of a list and builds a new list.

Parameters
lstThe original list.
fThe transformation function.
delThe function to delete content on failure.
Returns
A new list resulting from the transformation, or NULL on error.

Iterates through the input list lst, applies the function f to the content of each node, and creates a new list with the results. If any allocation fails, the entire new list is cleared using the del function.

Parameters
lstPointer to the head of the original list.
fFunction to apply to the content of each node.
delFunction used to delete the content of a node if needed.
Returns
A new list with the transformed content, or NULL if allocation fails.
Note
This function allocates new memory and performs deep copies of the transformed content. You must free the returned list when done.
See also
ft_lstnew
ft_lstadd_back
ft_lstclear

◆ ft_lstnew()

t_list * ft_lstnew ( void *  content)

#include <include/ft_list.h>

Creates a new list node.

Allocates and initializes a new list node.

Parameters
contentThe content to store in the node.
Returns
A pointer to the newly created node, or NULL on failure.

Allocates memory for a new t_list node and sets its content to the provided pointer. The next pointer of the new node is set to NULL.

Parameters
contentThe content to store in the new node.
Returns
A pointer to the newly created node, or NULL if allocation fails.
Note
This function does not copy the content; it stores the pointer as-is. The caller is responsible for managing the memory of the content if necessary.

◆ ft_lstsize()

int ft_lstsize ( t_list lst)

#include <include/ft_list.h>

Counts the number of nodes in a list.

Counts the number of nodes in a linked list.

Parameters
lstThe beginning of the list.
Returns
The number of elements in the list.

Iterates through the list, counting each node until the end is reached.

Parameters
lstA pointer to the head of the list.
Returns
The total number of nodes in the list. Returns 0 if the list is empty.