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

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    List Search
Click For Summary

Discussion Overview

The discussion revolves around finding an efficient method to search for an object in a sorted linked list where each node allows movement of 1, 10, or 100 steps forward. Participants explore various strategies and algorithms to optimize the search process.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests a simple loop for searching but seeks a more efficient method using the 10 and 100 step options.
  • Another participant proposes checking the value of the 100th node to determine if jumping ahead is beneficial, questioning the efficiency of moving one node at a time.
  • A participant points out the limitation of not being able to go back after jumping ahead, emphasizing the need to adhere to the structure of the linked list.
  • There is a suggestion that it is possible to check the value of a node without leaving the current node, prompting further clarification on how to do so.
  • A participant introduces a binary search-like approach, suggesting that if the next 100th node's value is less than the target, one should jump ahead, and if it is greater, to use the 10-step option.
  • One participant shares a code snippet for a search function but expresses confusion about the logic behind it and seeks clarification on specific lines of code.
  • Another participant explains the logic of checking the next 100th pointer while ensuring it exists and its value is less than the target value.

Areas of Agreement / Disagreement

Participants express various strategies for searching the linked list, with some advocating for a binary search approach while others focus on the limitations of the structure. There is no consensus on a single optimal method, and the discussion remains unresolved regarding the best approach.

Contextual Notes

Participants discuss the implications of the linked list structure on search efficiency, including the inability to backtrack after jumping ahead. There are also references to specific code implementations that may require further clarification.

transgalactic
Messages
1,386
Reaction score
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
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?
 
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:
You are not stuck, you can check value of the other node not leaving current one.
 
how can i check the value of the other node without leaving the current one?
 
head->next100->value

You are on your own now.
 
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
 
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;

while (head->next100 &&  head->next100->value<=key)
         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;
}
 
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
7K