-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path8-delete_dnodeint.c
More file actions
55 lines (50 loc) · 835 Bytes
/
8-delete_dnodeint.c
File metadata and controls
55 lines (50 loc) · 835 Bytes
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
51
52
53
54
55
#include "lists.h"
/**
* delete_dnodeint_at_index - Delete node at nth index
*
* @head: Head of node
*
* @index: index
*
* Return: 1 succeed, -1 if fail
*/
int delete_dnodeint_at_index(dlistint_t **head, unsigned int index)
{
dlistint_t *node;
unsigned int count;
if (*head == NULL)
return (-1);
node = *head;
if (index == 0)
{
*head = node->next;
if (node->next != NULL)
{
node->next->prev = NULL;
}
free(node);
return (1);
}
for (count = 0; node != NULL && count < index - 1 ; count++)
{
node = node->next;
}
if (node == NULL || node->next == NULL)
{
return (-1);
}
if (node->next->next != NULL)
{
node->next = node->next->next;
free(node->next->prev);
node->next->prev = node;
return (1);
}
else
{
free(node->next);
node->next = NULL;
return (1);
}
return (-1);
}