ProgrammingVery hardProduct company OA
What is printed?
What is printed?
struct N { int v; struct N *nx; };
struct N c = {3, NULL}, b = {2, &c}, a = {1, &b};
struct N *prev = NULL, *cur = &a, *nx;
while (cur) {
nx = cur->nx;
cur->nx = prev;
prev = cur;
cur = nx;
}
printf("%d %d %d", prev->v, prev->nx->v, a.nx == NULL);- A. 3 2 1
- B. 3 1 1
- C. 1 2 0
- D. 3 2 0
Correct answer: A. 3 2 1
This is the standard in-place reversal: each node's link is turned to point backwards. Afterwards prev is the old tail c (3), c now links to b (2), and the old head a has become the tail, so a.nx is NULL (1).
More C Programming questions
- After this code, what does buf hold and what is n? c char buf[20]; int n = sprintf(buf, "%04d|%.2s", 7, "xyz");
- What is printed? c int c = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (j == i) continue; if (j > i + 1) break; c++…
- Which of the following is NOT allowed in C?
- What is printed? c int cnt(int n, int last) { if (n == 0) return 1; return cnt(n - 1, 0) + (last ? 0 : cnt(n - 1, 1)); } int main(void) {…
- On a two's-complement machine, what is printed? c int a = 5; printf("%d %d %d", !a, ~a & 0xF, !!a + !~-1);
- What is printed? c const char s = "a1b22c333"; int sum = 0, cur = 0; for (; s; s++) { if (s >= '0' && s <= '9') cur = cur 10 + (s - '0')…