-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path7-insert_dnodeint.c
More file actions
55 lines (51 loc) · 966 Bytes
/
7-insert_dnodeint.c
File metadata and controls
55 lines (51 loc) · 966 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"
/**
* insert_dnodeint_at_index - Add node at nth index
*
* @h: Head of node
*
* @idx: index
*
* @n: struct int
*
* Return: dlistint_t
*/
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
{
dlistint_t *new_node = malloc(sizeof(dlistint_t));
dlistint_t *current;
unsigned int count = 0;
if (h == NULL || new_node == NULL)
{
return (NULL);
}
new_node->n = n;
new_node->next = NULL;
new_node->prev = NULL;
current = *h;
if (idx == 0)
{
new_node = add_dnodeint(h, n);
return (new_node);
}
while (current)
{
if (current->next == NULL && count == idx - 1)
{
new_node = add_dnodeint_end(h, n);
return (new_node);
}
else if ((idx - 1) == count)
{
new_node->next = current->next;
new_node->prev = current;
current->next->prev = new_node;
current->next = new_node;
return (new_node);
}
count++;
current = current->next;
}
free(new_node);
return (NULL);
}