How can I search for data in a linked list?

Unfortunately, the only way to search a linked list is with a linear search, because the only way a linked list’s members can be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.  

Showing Answers 1 - 1 of 1 Answers

rakesh

  • Oct 7th, 2006
 

hai here am writing a simple ex for searchina linked list for specific value

struct link

{

int data;

struct link *node;

}

if the above struct is a node such that in a node u can store a data and pointer to the next node.

if u have the pointer the beginning of the node and let it be "start"

struct link *tnode;flag=0;

for(tnode=start;tnode->next!=null;tnode=tnode->node)

{

if(tnode->data==key){ flag=1; }

}

if(flag==1){/*  key found and the tnode->data will hold that value */}

else

{/*key not found */}

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions