data_structures
list_unlink.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* list_unlink.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/18 22:16:54 by unite #+# #+# */
10 /* Updated: 2020/09/07 22:48:41 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "list.h"
15 
16 static void *list_unlink_inner(t_list *alst, size_t index)
17 {
18  t_link *link;
19  void *content;
20 
21  if (index < alst->size / 2)
22  {
23  link = alst->head;
24  while (index-- > 0)
25  link = link->next;
26  }
27  else
28  {
29  link = alst->tail;
30  while (++index < alst->size)
31  link = link->prev;
32  }
33  link->prev->next = link->next;
34  link->next->prev = link->prev;
35  content = link->content;
36  free(link);
37  alst->size--;
38  return (content);
39 }
40 
41 void *list_unlink(t_list *alst, size_t index)
42 {
43  if (index >= alst->size)
44  {
45  ds_exit_set(EINVAL);
46  return (NULL);
47  }
48  else if (index == 0)
49  return (list_unlink_first(alst));
50  else if (index + 1 == alst->size)
51  return (list_unlink_last(alst));
52  else
53  return (list_unlink_inner(alst, index));
54 }
s_list::head
t_link * head
The first link.
Definition: list.h:56
s_list
Doubly-linked list of generic items.
Definition: list.h:54
s_list::tail
t_link * tail
The last link.
Definition: list.h:57
s_list::size
size_t size
The number of items in the list.
Definition: list.h:58
list.h
ds_exit_set
void ds_exit_set(int err)
Set errno to the specified value, print the error message, and exit the process.
Definition: ds_exit_set.c:22