-
Notifications
You must be signed in to change notification settings - Fork 0
/
105-jump_list.c
executable file
·43 lines (37 loc) · 1.25 KB
/
105-jump_list.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
40
41
42
43
#include "search_algos.h"
#include <stdio.h>
#include <stdlib.h>
/**
* jump_list - Searches for a for a value in a ssl using jump search
* @list: A pointer to the head of the linked list to search.
* @size: The number of nodes in the list.
* @value: The value to search for.
*
* Return: If the value is not present or the head of the list is NULL, NULL.
* Otherwise, a pointer to the first node where the value is located.
*/
listint_t *jump_list(listint_t *list, size_t size, int value)
{
size_t block, block_size;
listint_t *node, *jump;
if (list == NULL || size == 0)
return (NULL);
block = 0;
block_size = sqrt(size);
for (node = jump = list; jump->index + 1 < size && jump->n < value;)
{
node = jump;
for (block += block_size; jump->index < block; jump = jump->next)
{
if (jump->index + 1 == size)
break;
}
printf("Value checked at index [%ld] = [%d]\n", jump->index, jump->n);
}
printf("Value found between indexes [%ld] and [%ld]\n",
node->index, jump->index);
for (; node->index < jump->index && node->n < value; node = node->next)
printf("Value checked at index [%ld] = [%d]\n", node->index, node->n);
printf("Value checked at index [%ld] = [%d]\n", node->index, node->n);
return (node->n == value ? node : NULL);
}