How can I efficiently search a sorted linked list using steps of 1, 10, and 100?

  • Thread starter transgalactic
  • Start date
  • Tags
    List Search
In summary, the most effective way to find an object in a linked list is to do a simple loop each time it does one step, and then use the head->next and head->next100 functions to keep going until you find the object or until the list is empty.
  • #1
transgalactic
1,395
0
i got a sorted linked list from the smallest to the biggest
in each node i can go 1 , 10 ,100 steps forward
i need to find an object in this linked list

if the object is found i need to return it if not then null

what is the most affective way to do that

i can do a simple loop each time it does one step
and then i will surely get the answer

how to use those 10 and 100 step to make it more affective?

i got the skeleton for this function and some missing part:

http://img513.imageshack.us/my.php?image=36394139eh1.gif

i need to fill them with something in order to make it work like they told
 
Last edited:
Technology news on Phys.org
  • #2
If you are at the first node you can check what is the number associated with the 100th node. If it is smaller than the number you are looking for, does it make sense to walk all 100 nodes one by one, or will it be faster to jump 100 nodes ahead in one step?
 
  • #3
but i can't go back
if i go to the 100th and it too big i am stuck there

and i got to stick with this structure thing
 
Last edited:
  • #4
You are not stuck, you can check value of the other node not leaving current one.
 
  • #5
how can i check the value of the other node without leaving the current one?
 
  • #6
head->next100->value

You are on your own now.
 
  • #7
I was thinking about binary search algorithm (log(n)).

Check if next 100 == element
return
if next 100 < element
jump next
else
do with 10..

once you are in less than 10 range. make 1-1 jumps
 
  • #8
i found the solution but i can't understand the logic of it

and i can't understand the meaning of this kind of line (marked red)

Code:
Typedef struct forward forward;
Struct forward{
     Int value;
     Forward * next100;
     Forward * next10;
     Forward * next;
};

forward* search_forward(forward * head,ink key){
if(!head)
  return null;

[COLOR="Red"]while (head->next100 &&  head->next100->value<=key)[/COLOR]
         head=head->next100;

while (head->next10 &&  head->next10->value<=key)
head=head->next10;

while (head->next &&  head->next->value<=key)
head=head->next;if (head->value==key) return head;
return null;
}
 
  • #9
while (head->next100 && head->next100->value<= key)

keep on going until there is a next 100th pointer
and it has a value less than key value.


see my thing above.. (it also checking that the next 100 exists)

if next 100 < element
jump next 100
 

Related to How can I efficiently search a sorted linked list using steps of 1, 10, and 100?

What is a Linked List?

A linked list is a data structure that stores a sequence of elements. Each element, called a node, contains a value and a pointer to the next node in the list.

Why would I use a Linked List for searching?

Linked lists are often used for searching because they allow for efficient insertion and deletion of elements at any position in the list, which can be useful for maintaining a sorted list for searching.

How do I search for a specific element in a Linked List?

To search for a specific element in a linked list, you would start at the first node and compare the value with the element you are searching for. If the values match, you have found the element. If not, you would move to the next node and repeat the process until you find the element or reach the end of the list.

What is the time complexity of searching in a Linked List?

The time complexity of searching in a linked list is O(n), where n is the number of elements in the list. This means that the time it takes to search for an element in a linked list increases linearly as the size of the list increases.

Can I perform a binary search on a Linked List?

No, it is not possible to perform a binary search on a linked list. Binary search requires the list to be sorted, and linked lists are not inherently sorted. Additionally, linked lists do not allow for random access, which is necessary for binary search.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
13K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top