-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path7-insert_dnodeint.c
More file actions
41 lines (34 loc) · 883 Bytes
/
7-insert_dnodeint.c
File metadata and controls
41 lines (34 loc) · 883 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
#include "lists.h"
/**
* insert_dnodeint_at_index - Inserts a new node in a dlistint_t
* list at a given position.
* @h: A pointer to the head of the dlistint_t list.
* @idx: The position to insert the new node.
* @n: The integer for the new node to contain.
*
* Return: If the function fails - NULL.
* Otherwise - the address of the new node.
*/
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
{
dlistint_t *tmp = *h, *new;
if (idx == 0)
return (add_dnodeint(h, n));
for (; idx != 1; idx--)
{
tmp = tmp->next;
if (tmp == NULL)
return (NULL);
}
if (tmp->next == NULL)
return (add_dnodeint_end(h, n));
new = malloc(sizeof(dlistint_t));
if (new == NULL)
return (NULL);
new->n = n;
new->prev = tmp;
new->next = tmp->next;
tmp->next->prev = new;
tmp->next = new;
return (new);
}