Nivaar Exam Prep

25-Comp-A4 Program Design and Data Structures · December 2018

Question 7 of 9: Pointer-based Data Structures — Duplicating Every Node of a Linked List

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated

Notes on this paper

Pseudocode or any high-level language (e.g. C or C++) is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details.

Reference texts for this subject.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree walks (ch. 12), stacks and linear-time scans (ch. 10, 2), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — arrays and dynamic 2-D allocation (ch. 1), linked lists (ch. 3), stacks (ch. 3.3), binary trees (ch. 4).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design, templates and the Rule of Three (ch. 9–12), file streams (ch. 14).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/file I/O idioms (ch. 1, 7), pointers and structures (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — value semantics, const-correctness and templates (ch. 3, 16–18).

The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.

Question 7: Pointer-based Data Structures — Duplicating Every Node of a Linked List (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. A singly linked list of NODEs (int data, struct node *next), possibly empty.

Find. A function that, in place, inserts a copy of every node immediately after the original it copies — so a list of length $n$ becomes a list of length $2n$ with each original value appearing twice in a row.

Approach. Walk the list one original node at a time; at each node, splice a freshly allocated copy in between it and its current next, then advance the walk pointer past the copy just inserted so the loop never revisits (and infinitely re-duplicates) a node it has already handled.

  1. Get the splice order right. For node $A$ with successor $B$, the new node $A'$ must end up between them: $A \to A' \to B$. Because $A'{\to}\text{next}$ is set to $A{\to}\text{next}$ (i.e. $B$) before $A{\to}\text{next}$ is overwritten to point at $A'$, no pointer to $B$ is ever lost.
    #include <stdlib.h>
    
    typedef struct node {
        int data;
        struct node *next;
    } NODE;
    
    void dupl(NODE *head)
    {
        NODE *cur = head;
    
        while (cur != NULL) {
            NODE *copy = malloc(sizeof(NODE));
            copy->data = cur->data;
            copy->next = cur->next;   /* copy points to what A used to point to */
            cur->next  = copy;        /* A now points to its own copy */
    
            cur = copy->next;          /* skip past the copy: resume at the NEXT original */
        }
    }
  2. Confirm the empty-list case. When head == NULL, the while condition is false immediately and the function returns having done nothing — exactly the required behaviour, with no special-case branch needed.
  3. Trace it on a 3-node list. Starting list $3 \to 7 \to 5 \to \text{NULL}$: at node 3, insert a copy to get $3\to3'\to7\to5$, then advance to the original 7 (skipping $3'$); at node 7, insert a copy to get $3\to3'\to7\to7'\to5$, advance to the original 5; at node 5, insert a copy to get the final list. $$\boxed{3\to3\to7\to7\to5\to5\to\text{NULL}}$$ Because the walk pointer always resumes at the next original node, the loop visits exactly the $n$ original nodes once each, never touching a node it just created.
Question 7 — results
Input listOutput list
(empty)(empty)
99, 9
3, 7, 53, 3, 7, 7, 5, 5