-
Notifications
You must be signed in to change notification settings - Fork 0
/
106-linear_skip.c
46 lines (40 loc) · 1.3 KB
/
106-linear_skip.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
46
#include "search_algos.h"
/**
* linear_skip - Searches for an algorithm in a sorted singly
* linked list of integers using linear skip.
* @list: A pointer to the head of the linked list to search.
* @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.
*/
skiplist_t *linear_skip(skiplist_t *list, int value)
{
skiplist_t *node, *jump;
if (list == NULL)
return (NULL);
for (node = jump = list; jump->next != NULL && jump->n < value;)
{
node = jump;
if (jump->express != NULL)
{
jump = jump->express;
printf("Value checked at index [%ld] = [%d]\n",
jump->index, jump->n);
}
else
{
while (jump->next != NULL)
jump = jump->next;
}
}
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);
}