ProgrammingVery hardProduct company OA

What is printed?

What is printed?
struct N { int v; struct N *nx; };
void del(struct N **h, int x) {
    while (*h) {
        if ((*h)->v == x) *h = (*h)->nx;
        else h = &(*h)->nx;
    }
}
int main(void) {
    struct N e = {3, NULL}, d = {2, &e}, c = {3, &d}, b = {3, &c}, a = {1, &b};
    struct N *head = &a;
    del(&head, 3);
    for (struct N *p = head; p; p = p->nx) printf("%d", p->v);
}
  1. A. 12
  2. B. 132
  3. C. 1323
  4. D. 123

Correct answer: A. 12

h holds the address of the link that points at the current node, so unlinking is just *h = (*h)->nx, and h does not advance after a removal. That lets consecutive matches (b and c) and a match at the tail (e) all be removed: only 1 and 2 remain. Advancing after every removal would skip c, giving 132.

More C Programming questions

Exams that ask this topic