-
Notifications
You must be signed in to change notification settings - Fork 0
/
105-jump_list.c
45 lines (39 loc) · 1.4 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
44
45
#include "search_algos.h"
/**
* jump_list - Searches for an algorithm in a sorted singly
* linked list of integers 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.
*
* Description: Prints a value every time it is compared in the list.
* Uses the square root of the list size as the jump step.
*/
listint_t *jump_list(listint_t *list, size_t size, int value)
{
size_t step, step_size;
listint_t *node, *jump;
if (list == NULL || size == 0)
return (NULL);
step = 0;
step_size = sqrt(size);
for (node = jump = list; jump->index + 1 < size && jump->n < value;)
{
node = jump;
for (step += step_size; jump->index < step; 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);
}