ProgrammingVery hardProduct company OA
In what order are the nodes linked after the four calls?
In what order are the nodes linked after the four calls?
struct N { int v; struct N *nx; };
void ins(struct N **h, struct N *n) {
while (*h && (*h)->v < n->v) h = &(*h)->nx;
n->nx = *h;
*h = n;
}
int main(void) {
struct N a = {5}, b = {2}, c = {8}, d = {5};
struct N *head = NULL;
ins(&head, &a); ins(&head, &b); ins(&head, &c); ins(&head, &d);
}- A. b, d, a, c
- B. b, a, d, c
- C. b, a, c, d
- D. d, b, a, c
Correct answer: A. b, d, a, c
h walks the address of each link, so inserting at the head and in the middle are the same code. The test is a strict <, so the walk stops at the first node whose value is not less than the new one: d (5) stops at a (5) and goes in front of it. With <= it would go after a, giving b, a, d, c.
More C Programming questions
- A function in a.c is declared static int helper(void) { ... }. Which statement is true?
- What is printed? c #define MAX(a, b) a > b ? a : b int x = 3, y = 7; printf("%d", 2 MAX(x, y));
- Assume IEEE-754 single-precision float with float arithmetic done in float. Which loops terminate? c int c1 = 0, c2 = 0; for (float f = 0…
- What is printed? c struct Shape { int w, h; int (area)(struct Shape ); }; int rect(struct Shape s) { return s->w s->h; } int tri(struct…
- What is printed? c int i = 0, s = 0; top: s += i; if (++i < 5) goto top; printf("%d %d", s, i);
- What is printed? c int calls = 0; int mx(int a, int lo, int hi) { calls++; if (lo == hi) return a[lo]; int m = (lo + hi) / 2; int l =…