▸ Part 03 · Practice 03 / 03

Practice questions

Five short pieces of code to trace by hand. For each one, work out the printed output, the resulting list, or the runtime behaviour against the reference list below.

▸ All questions operate on this list
head 10 20 30 40 NULL
Question 01 · Trace the pointer

What value gets printed?

q1.c
struct Node* p = head->next;
printf("%d", p->next->data);
Question 02 · Loop boundary

What does this loop print?

q2.c
struct Node* p = head;
while (p->next != NULL) {
    printf("%d ", p->data);
    p = p->next;
}
Question 03 · Pointer rewiring

What does the list look like after this single line runs?

q3.c
head->next = head->next->next;
Question 04 · Insertion in place

What does the list look like after this code runs?

q4.c
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = 25;
newNode->next = head->next->next;
head->next->next = newNode;
Question 05 · Spot the bug

Will this loop work correctly? If not, what happens?

q5.c
struct Node* p = head;
while (p != NULL) {
    p = p->next;
    printf("%d ", p->data);
}
Take time on each one before checking — try tracing the pointers on the diagram with your finger.