ProAnswers.org

algorithm to Search for a key in a linked list

algorithm Search for a key in a linked list

typedef struct node{

	    int data;

	    struct node *next;

	}node;

	 

	int search_list(node *head, int x){

	    node *move;

	    move = head;

	    int index = 0;

	    for(;move != NULL;move = move->next, index++){

	        if(move->data == x)

	            return index+1;//1st element will have index 1

	    }

	    return -1;   //element not found

	}