data_structures
Data Structures | Typedefs | Functions
list.h File Reference
#include <errno.h>
#include <sys/types.h>
#include "types.h"
#include "utils.h"
+ Include dependency graph for list.h:

Go to the source code of this file.

Data Structures

struct  s_link
 A link in a doubly-linked list. More...
 
struct  s_list
 Doubly-linked list of generic items. More...
 

Typedefs

typedef struct s_link t_link
 
typedef struct s_list t_list
 

Functions

void list_add_first (t_list *alst, const void *data)
 Adds a copy of the specified element at the front of this list. More...
 
void list_add_last (t_list *alst, const void *data)
 Adds a copy of the specified element at the back of this list. More...
 
void list_delete (t_list *alst)
 Deletes the list and frees all memory taken by its contents. More...
 
void * list_unlink_first (t_list *alst)
 Removes the first item in the list and returns it. More...
 
void * list_unlink_last (t_list *alst)
 Removes the last item in the list and returns it. More...
 
void * list_unlink (t_list *alst, size_t index)
 Removes the item at the specified position in the list and returns it. More...
 
void list_remove_first (t_list *alst)
 Removes the first item in the list. More...
 
void list_remove_last (t_list *alst)
 Removes the last item in the list. More...
 
void list_remove (t_list *alst, size_t index)
 Removes the item at the specified position in the list. More...
 
void * list_peek_first (const t_list *alst)
 Returns the first item in the list. More...
 
void * list_peek_last (const t_list *alst)
 Returns the last item in the list. More...
 
void * list_peek (const t_list *alst, size_t index)
 Returns the item at the specified position in the list. More...
 
size_t list_size (const t_list *alst)
 Returns the number of items in this list. More...
 
t_listlist_new (const t_type *type)
 Allocates memory and initializes an empty list. More...
 
void list_merge_sort (t_list *alst)
 Sorts this list in ascending order using in-place merge sort. More...
 
t_listlist_copy (const t_list *alst)
 Copies an list and all it contents. More...
 

Function Documentation

◆ list_add_first()

void list_add_first ( t_list alst,
const void *  data 
)

Adds a copy of the specified element at the front of this list.

Parameters
dataThe item to be copied

Definition at line 16 of file list_add_first.c.

◆ list_add_last()

void list_add_last ( t_list alst,
const void *  data 
)

Adds a copy of the specified element at the back of this list.

Parameters
dataThe item to be copied

Definition at line 16 of file list_add_last.c.

◆ list_copy()

t_list* list_copy ( const t_list alst)

Copies an list and all it contents.

Returns
A copy of the list

Definition at line 16 of file list_copy.c.

◆ list_delete()

void list_delete ( t_list alst)

Deletes the list and frees all memory taken by its contents.

Definition at line 16 of file list_delete.c.

◆ list_merge_sort()

void list_merge_sort ( t_list alst)

Sorts this list in ascending order using in-place merge sort.

Note
For this function to work, the datatype in this list must be comparable (i.e. implement the cmp function).

Definition at line 75 of file list_merge_sort.c.

◆ list_new()

t_list* list_new ( const t_type type)

Allocates memory and initializes an empty list.

Parameters
thetype of items that this list will hold
Returns
An empty list

Definition at line 16 of file list_new.c.

◆ list_peek()

void* list_peek ( const t_list alst,
size_t  index 
)

Returns the item at the specified position in the list.

The item is kept in the list.

Returns
The item, or NULL if the list is empty.
Note
This method has complexity O(N/2) in the number of items in the worst case.

Definition at line 16 of file list_peek.c.

◆ list_peek_first()

void* list_peek_first ( const t_list alst)

Returns the first item in the list.

The item is kept in the list.

Returns
The item, or NULL if the list is empty.

Definition at line 16 of file list_peek_first.c.

◆ list_peek_last()

void* list_peek_last ( const t_list alst)

Returns the last item in the list.

The item is kept in the list.

Returns
The item, or NULL if the list is empty.

Definition at line 16 of file list_peek_last.c.

◆ list_remove()

void list_remove ( t_list alst,
size_t  index 
)

Removes the item at the specified position in the list.

Note
This method has complexity O(N/2) in the number of items in the worst case.

Definition at line 16 of file list_remove.c.

◆ list_remove_first()

void list_remove_first ( t_list alst)

Removes the first item in the list.

Definition at line 16 of file list_remove_first.c.

◆ list_remove_last()

void list_remove_last ( t_list alst)

Removes the last item in the list.

Definition at line 16 of file list_remove_last.c.

◆ list_size()

size_t list_size ( const t_list alst)

Returns the number of items in this list.

Returns
The number of items in this list

Definition at line 16 of file list_size.c.

◆ list_unlink()

void* list_unlink ( t_list alst,
size_t  index 
)

Removes the item at the specified position in the list and returns it.

Returns
The item in the list, or NULL if the list is empty.
Note
This method has complexity O(N/2) in the number of items in the worst case.

Definition at line 41 of file list_unlink.c.

◆ list_unlink_first()

void* list_unlink_first ( t_list alst)

Removes the first item in the list and returns it.

Returns
The first item in the list, or NULL if the list is empty.

Definition at line 16 of file list_unlink_first.c.

◆ list_unlink_last()

void* list_unlink_last ( t_list alst)

Removes the last item in the list and returns it.

Returns
The last item in the list, or NULL if the list is empty.

Definition at line 16 of file list_unlink_last.c.