Skip to content

Commit a111fce

Browse files
committed
add tasks 5,6
1 parent fff79dd commit a111fce

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "lists.h"
2+
3+
/**
4+
* get_dnodeint_at_index - get nth node of a dlistint_t linked list
5+
* @head: header of double linked list
6+
* @index: index of the node, starting from 0
7+
* Return: a address of nth node
8+
*/
9+
dlistint_t *get_dnodeint_at_index(dlistint_t *head, unsigned int index)
10+
{
11+
dlistint_t *headcopy;
12+
unsigned int i;
13+
14+
headcopy = head;
15+
if (headcopy != NULL)
16+
{
17+
while (headcopy->prev != NULL)
18+
headcopy = headcopy->prev;
19+
20+
for (i = 0; (i < index) && (headcopy != NULL); i++)
21+
headcopy = headcopy->next;
22+
return (headcopy);
23+
}
24+
else
25+
return (NULL);
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "lists.h"
2+
3+
/**
4+
* sum_dlistint - sum of all the data (n) of a dlistint_t linked list
5+
* @head: header of double linked list
6+
* Return: the sum of all nodes
7+
*/
8+
int sum_dlistint(dlistint_t *head)
9+
{
10+
dlistint_t *headcopy;
11+
int sum = 0;
12+
13+
headcopy = head;
14+
if (headcopy != NULL)
15+
{
16+
while (headcopy->prev != NULL)
17+
headcopy = headcopy->prev;
18+
19+
while (headcopy != NULL)
20+
{
21+
sum += headcopy->n;
22+
headcopy = headcopy->next;
23+
}
24+
return (sum);
25+
}
26+
else
27+
return (0);
28+
}

0 commit comments

Comments
 (0)