-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-insert_dnodeint.c
39 lines (38 loc) · 942 Bytes
/
7-insert_dnodeint.c
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
#include "lists.h"
/**
* insert_dnodeint_at_index - inserts a new node at a given position.
* @h: pointer to the head of the list
* @idx: index of the list where the new node should be added
* @n: data of the new node
* Return: the address of the new node, or NULL if it failed
*/
dlistint_t *insert_dnodeint_at_index(dlistint_t **h, unsigned int idx, int n)
{
unsigned int counter = 0;
dlistint_t *new_node = NULL, *tmp = NULL;
if (h == NULL)
return (NULL);
if (idx == 0)
return (add_dnodeint(h, n));
tmp = *h;
while (tmp != NULL)
{
if (counter == idx - 1)
{
if (tmp->next == NULL)
return (add_dnodeint_end(h, n));
new_node = malloc(sizeof(dlistint_t));
if (new_node == NULL)
return (NULL);
new_node->n = n;
new_node->next = tmp->next;
new_node->prev = tmp;
tmp->next->prev = new_node;
tmp->next = new_node;
return (new_node);
}
tmp = tmp->next;
counter++;
}
return (NULL);
}