data_structures
list.h
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* list.h :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 17:12:34 by unite #+# #+# */
10 /* Updated: 2020/09/07 22:17:55 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #ifndef LIST_H
15 
16 # define LIST_H
17 
18 # include <errno.h>
19 # include <sys/types.h>
20 # include "types.h"
21 # include "utils.h"
22 
34 typedef struct s_link
35 {
36  void *content;
37  struct s_link *next;
38  struct s_link *prev;
39 } t_link;
40 
54 typedef struct s_list
55 {
58  size_t size;
59  const t_type *type;
60 } t_list;
61 
67 void list_add_first(t_list *alst, const void *data);
68 
74 void list_add_last(t_list *alst, const void *data);
75 
80 void list_delete(t_list *alst);
81 
87 void *list_unlink_first(t_list *alst);
88 
94 void *list_unlink_last(t_list *alst);
95 
103 void *list_unlink(t_list *alst, size_t index);
104 
109 void list_remove_first(t_list *alst);
110 
115 void list_remove_last(t_list *alst);
116 
123 void list_remove(t_list *alst, size_t index);
124 
130 void *list_peek_first(const t_list *alst);
131 
137 void *list_peek_last(const t_list *alst);
138 
147 void *list_peek(const t_list *alst, size_t index);
148 
154 size_t list_size(const t_list *alst);
155 
162 t_list *list_new(const t_type *type);
163 
170 void list_merge_sort(t_list *alst);
171 
177 t_list *list_copy(const t_list *alst);
178 
179 #endif
list_remove
void list_remove(t_list *alst, size_t index)
Removes the item at the specified position in the list.
Definition: list_remove.c:16
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.
Definition: list_add_first.c:16
types.h
list_remove_first
void list_remove_first(t_list *alst)
Removes the first item in the list.
Definition: list_remove_first.c:16
list_peek_first
void * list_peek_first(const t_list *alst)
Returns the first item in the list.
Definition: list_peek_first.c:16
list_copy
t_list * list_copy(const t_list *alst)
Copies an list and all it contents.
Definition: list_copy.c:16
list_merge_sort
void list_merge_sort(t_list *alst)
Sorts this list in ascending order using in-place merge sort.
Definition: list_merge_sort.c:75
utils.h
s_list::head
t_link * head
The first link.
Definition: list.h:56
list_unlink_first
void * list_unlink_first(t_list *alst)
Removes the first item in the list and returns it.
Definition: list_unlink_first.c:16
list_unlink_last
void * list_unlink_last(t_list *alst)
Removes the last item in the list and returns it.
Definition: list_unlink_last.c:16
list_peek_last
void * list_peek_last(const t_list *alst)
Returns the last item in the list.
Definition: list_peek_last.c:16
s_list
Doubly-linked list of generic items.
Definition: list.h:54
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
s_list::tail
t_link * tail
The last link.
Definition: list.h:57
list_new
t_list * list_new(const t_type *type)
Allocates memory and initializes an empty list.
Definition: list_new.c:16
list_remove_last
void list_remove_last(t_list *alst)
Removes the last item in the list.
Definition: list_remove_last.c:16
list_unlink
void * list_unlink(t_list *alst, size_t index)
Removes the item at the specified position in the list and returns it.
Definition: list_unlink.c:41
s_list::size
size_t size
The number of items in the list.
Definition: list.h:58
list_delete
void list_delete(t_list *alst)
Deletes the list and frees all memory taken by its contents.
Definition: list_delete.c:16
s_list::type
const t_type * type
The type of items in this list.
Definition: list.h:59
list_peek
void * list_peek(const t_list *alst, size_t index)
Returns the item at the specified position in the list.
Definition: list_peek.c:16
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.
Definition: list_add_last.c:16
list_size
size_t list_size(const t_list *alst)
Returns the number of items in this list.
Definition: list_size.c:16