-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-delete_dnodeint.c
More file actions
50 lines (44 loc) · 1.02 KB
/
8-delete_dnodeint.c
File metadata and controls
50 lines (44 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "lists.h"
/**
* delete_dnodeint_at_index - deletes the node at index of a dlistint_t list.
* @head: pointer to the list.
* @index: position of the node to delete.
* Return: 1 if it succeeded, -1 if it failed.
**/
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
{
dlistint_t *aux_node = *head;
dlistint_t *node_to_delete = *head;
unsigned int idx;
unsigned int cont = 0;
/* border case for empty list */
if (!(*head))
return (-1);
/* border case for delete at the beginning */
if (index == 0)
{
*head = node_to_delete->next;
free(node_to_delete);
if (*head)
(*head)->prev = NULL;
return (1);
}
/* search of position to delete */
idx = index - 1;
while (aux_node && cont != idx)
{
cont++;
aux_node = aux_node->next;
}
/* general case */
if (cont == idx && aux_node)
{
node_to_delete = aux_node->next;
if (node_to_delete->next)
node_to_delete->next->prev = aux_node;
aux_node->next = node_to_delete->next;
free(node_to_delete);
return (1);
}
return (-1);
}