Microsoft Interview Question
Software Engineer in Teststypedef struct node {
int data;
struct node *next;
} node;
int insertLL (node **head, int position, int data){
node *temp;
temp = (node *)malloc(sizeof(node));
temp->data = data;
temp->next = (node *)0;
if ( *(head) == (node *)0 && (position == 0) ){
*head = temp;
return 0;
} else if ( *(head) == (node *)0 && (position != 0) ) {
return errorcode;
} else if ( *(head) != (node *)0 && (position == 0)) {
temp->next = *(head)->next;
*head = temp;
return 0;
} else {
node *p = *(head);
while ( p->next != null && position > 1) {
position--;
p = p->next;
}
if (p->next == null && position > 1)
return errorcode;
else {
temp->next = p->next;
p->next = temp->next;
return 0;
}
}
}
Test Cases :
- Anonymous September 08, 20091> at the start
2>at the end
3> anywhere else