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);
}- A. 12
- B. 132
- C. 1323
- 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
- What is printed? c int rot(const char a, const char b) { char buf[32]; if (strlen(a) != strlen(b)) return 0; strcpy(buf, a); strcat(buf…
- What is printed? c int alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) { printf("%d%d%d", alt(5)…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } o = {{{{1, 2}}, {{3, 4}}}, 5}; printf("%d %d"…
- What is printed? c struct In { int a[2]; }; struct Out { struct In in[2]; int k; } q = {1, 2, 3, 4, 5}; printf("%d %d", q.in[1].a[1], q.k);
- What is printed? c int c = 0; int f(int n) { c++; return n <= 1 ? 1 : f(n / 2) + f(n - n / 2); } int main(void) { int r = f(7); printf("%d…
- Which of these loops run their body exactly for i = 4, 3, 2, 1, 0 (once each) with an unsigned i? c for (unsigned i = 5; i-- > 0; ) { /…