Saturday, August 15, 2009

finding Nth Node in linked list..

int GetNth(struct node* head, int index) {
struct node* current = head; int count = 0; // the index of the node we're currently looking at while (current != NULL) { if (count == index) return(current->data); count++; current = current->next; } assert(0); // if we get to this line, the caller was asking // for a non-existent element so we assert fail.}

No comments: