head.struct node{
int elt;
struct node *next;
};
int getListSize (struct node *head)
{
if( E1 ) return 1;
return E2;
}E1: head == NULL E2: 1 + getListSize(head)
E1: head->next == NULL E2: 1 + getListSize(head->next)
E1: head == NULL E2: 1 + getListSize(head->next)
E1: head->next == NULL E2: 1 + getListSize(head)
getListSize(head) must return the number of nodes in the list starting at head, and the problem states the list is always non-empty when the function is called from outside. The base case should fire exactly when the list has exactly one node left to count, and the recursive case should count the current node and recurse on the rest of the list.next pointer is NULL, i.e. when head->next == NULL. Testing head == NULL instead is wrong for two reasons: first, the base case would never fire on the very last real node (its next is NULL, but the node itself is not NULL), so the recursion would try to go one node too far; second, if head==NULL is ever reached in a call, the correct count for an empty remainder is 0 nodes, not 1, so returning 1 there overcounts by one. So E1 must be head->next == NULL, ruling out options (A) and (C), whose E1 is head == NULL.getListSize(head->next). So E2 must be 1 + getListSize(head->next). Calling getListSize(head) again with the same, unchanged pointer does not move to the next node at all, so it cannot converge.head->next==NULL, E2: 1+getListSize(head->next), on a 3 node list A to B to C, where C->next is NULL.getListSize(A): A->next is B, not NULL, so return 1+getListSize(B).getListSize(B): B->next is C, not NULL, so return 1+getListSize(C).getListSize(C): C->next is NULL, so return 1.head==NULL is false for node A (A is not NULL), so it executes E2: 1+getListSize(head), calling itself again with the exact same argument head=A. Since head is never advanced to head->next anywhere in this option, every subsequent call also has head=A, E1 stays false forever, and the recursion never terminates, it just keeps calling itself with the same pointer until the call stack overflows. Option (A) is therefore not a valid implementation for any list with more than one node.head->next==NULL, but E2 is 1+getListSize(head), which again passes the unchanged head pointer instead of head->next. Once the recursive branch is taken (list has 2 or more nodes), the argument never changes, head->next is still not NULL on every subsequent call, and the recursion never reaches the base case, again causing infinite recursion and a stack overflow.1+getListSize(head->next), but its base case is head==NULL, so on the 3 node example above it would recurse one call past the last node: getListSize(C) tests C==NULL (false), so it returns 1+getListSize(C->next) = 1+getListSize(NULL); getListSize(NULL) tests NULL==NULL (true) and returns 1. So getListSize(C)=1+1=2 instead of 1, and unwinding gives getListSize(A)=1+(1+2)=4 for a list that actually has only 3 nodes, an off by one overcount caused by counting the NULL terminator as an extra node.Consider the following code:
main() {
int x = 126, y = 105;
{
if (x > y)
x = x - y;
else
y = y - x;
}
while (x != y)
printf("%d", x);
}
Consider the following code:
int a;
int arr[] = {30, 50, 10};
int *ptr = arr[10] + 1;
a = *ptr;
(*ptr)++;
ptr = ptr + 1;
printf("%d", a + arr[1] + *ptr);