Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

List->Dynamic List->Key Search

By this time you know what is a key and how does a sequential search work, otherwise, refer the static list 'key search' section.

Similar to Traversal, here also we will take an auxiliary pointer, current, which initially points to the beginning of the list and then surfs through the list comparing the KEY value with each nodevalue in the list and we return true if the key is found or else false.

Algorithm:-
Step-1: Initialise the Current pointer with the beginning of the List
Step-2: Compare the KEY value with the Current node value; if they match then quit there or else go to step-3
Step-3: Move the Current pointer to point to the next node in the list and go to step-2, till the list is not over or else quit

C implementation:-
struct NODE
{
int nodevalue;
struct NODE *next;
}
struct NODE *list;

#define TRUE 1
#define FALSE 0

main()
{
int key_to_be_searched;
...
/* the list already exists*/
...
keysearch (key_to_be_searched);
}

keysearch ( int key)
{
struct NODE *current = list;
int found = FALSE;
while (current->nodevalue != NULL) 
 {
 if(current->nodevalue == key)
    {
   found = TRUE;
    break;
    }
 current = current->next;   
 };
if (found == TRUE) printf("\nFound");
 else printf("\nNot found");
}

Note:-Generally Key search returns you the position of the Key in the list (if its there) but I have deliberately skipped that to make it simpler.

Related Operations:
Concept
Addition and Deletion of
a node
Traversing of list
Searching for a particular Key
Sorting of list 

Index || Doubts / Clarifications || Related Topics || Web Links