Chapter 9: The List ADT and Generalized Lists

Data Structures and Algorithms with C
The list ADT contractArray and linked implementationsGeneralized listsCopy · equality · traversalDepth · inversionTrees as listsSharing and reference counts
Sercan KÜLCÜ · Giresun University · Computer Engineering

Introduction

Learning Outcomes

The contract

Define the list ADT by its operations, preconditions, and postconditions; separate the implementation from the interface.

Comparison

Compare array and linked implementations on access, insertion, and memory.

Generalized lists

Define lists that contain atoms and sublists, and draw their memory layout.

Recursive routines

Write and trace recursive copy, equality, traversal, depth, and inversion.

Trees as lists

Turn a tree into a one-line linear form and explain the method's limits.

Sharing

Explain the pitfalls of shared and recursive lists, and the ideas behind reference counting and garbage collection.

The List ADT

The list ADT: what it does, with the how hidden

typedef struct list List;               // opaque type

List  *list_create(void);
void   list_destroy(List *L);
int    list_length(const List *L);
bool   list_get(const List *L, int i, int *x);
bool   list_insert(List *L, int i, int x); // 0<=i<=n
bool   list_delete(List *L, int i, int *x); // 0<=i<n
int    list_find(const List *L, int x);   // -1 if absent
Linear list

L = (a0, a1, …, an−1), n ≥ 0; ai comes before ai+1.

Pre- / postcondition

list_insert(L, i, x): before, 0 ≤ i ≤ n; after, x is at position i, the length is n+1, and the old ai, …, an−1 have each moved one place right.

Opaque type

struct list is defined only in the .c file; client code can't tell whether it is an array or links, so swapping implementations is easy.

The List ADT

Array implementation (sequential mapping)

#define CAPACITY 100
typedef struct {
    int a[CAPACITY];
    int n;                  // item count
} ArrayList;

bool array_delete(ArrayList *L, int i, int *x) {
    int k;
    if (i < 0 || i >= L->n) return false;
    *x = L->a[i];
    for (k = i; k < L->n - 1; k++)
        L->a[k] = L->a[k + 1];      // shift left
    L->n--;
    return true;
}
Access

The address of a[i] is computed: O(1).

Insert / delete

The items after i must shift: O(n) in the worst case (insert or delete at the front).

Capacity

A fixed array fills up; doubling a dynamic array with realloc gives O(1) insertion on average.

The List ADT · Run the code step by step

Code Trace: array_insert(&L, 1, 9)

041722345▲ in = 3L->a

List [4, 7, 2], n = 3; 9 is to be inserted at position 1 (0-based).

041722345▲ in = 3L->a

Check: not full, and 0 ≤ i ≤ n; we can insert.

041722345▲ k▲ in = 3L->a

k = 3: k > i → true, shift.

0417223245▲ k▲ in = 3L->a

a[3] = a[2]: 2 moved one place right.

0417223245▲ k▲ in = 3L->a

k = 2: k > i → true, shift.

0417273245▲ k▲ in = 3L->a

a[2] = a[1]: 7 moved one place right.

0417273245▲ kin = 3L->a

k = 1: k > i → false, the loop ends.

0419273245▲ kin = 3L->a

a[i] = x: 9 is written into the freed slot.

0419273245▲ in = 4L->a

n++: the list is [4, 9, 7, 2]. The number of shifts is n − i: O(n) in the worst case (i = 0).

bool array_insert(ArrayList *L, int i, int x) {
    int k;
    if (L->n == CAPACITY || i < 0 || i > L->n)
        return false;          // full/invalid
    for (k = L->n; k > i; k--)
        L->a[k] = L->a[k - 1];     // shift right
    L->a[i] = x;
    L->n++;
    return true;
}

The List ADT

Linked implementation: a pointer to a pointer

typedef struct node *node_pointer;
struct node { int data; node_pointer link; };
typedef struct { node_pointer head; int n; } LinkedList;

bool ll_insert(LinkedList *L, int i, int x) {
    node_pointer *pp = &L->head, new_node;
    if (i < 0 || i > L->n) return false;
    while (i-- > 0)
        pp = &(*pp)->link;       // walk to link i
    new_node = malloc(sizeof *new_node);
    if (new_node == NULL) return false;
    new_node->data = x;
    new_node->link = *pp;
    *pp = new_node;              // head or middle: same
    L->n++;
    return true;
}
node_pointer *pp

The address of the link to change: first &L->head, then &prev->link. Inserting at the head stops being a special case.

Cost

Walking to the position is O(i); linking is O(1). No shifting, no capacity limit.

Length

If an n field is kept, list_length is O(1).

The List ADT

Array or linked list?

CriterionArray (sequential)Singly linked list
Access the i-th itemO(1)O(i)
Insert / delete at the frontO(n) shiftingO(1)
Append at the endO(1)* (if room)O(n), O(1) with a tail pointer
Insert at a known positionO(n)O(1) (if the predecessor is known)
Search (unsorted)O(n)O(n)
Search (sorted)O(log n) binary searchO(n)
MemoryData only; unused capacityA pointer per node
Cache behaviorContiguous: very goodScattered: poor
Rule of thumb: frequent access / binary search → array; frequent insertion and deletion in the middle, unknown size → linked list.

Generalized Lists

From linear lists to generalized lists

Linear list

A = (α0, …, αn−1): every element is an atom (arrays, singly linked lists).

Definition

If each αi may be an atom or a sublist, A is a generalized list. A is the list's name and n its length.

Head and tail

α0 is the head of the list and (α1, …, αn−1) its tail. The tail is always a list.

The LISP connection

Head/tail are LISP's car/cdr; LISP programs are themselves generalized lists.

ListLengthDescription
D = ()0empty list
A = (a, (b, c))2first element an atom, second the sublist (b, c); head(A) = a, tail(A) = ((b, c))
B = (A, A, ())3a shared sublist: the first two elements are the same A; the third is the empty list
C = (a, C)2a recursive list: it contains itself, C = (a, (a, (a, …)))

Generalized Lists

Example: a multivariate polynomial

typedef enum { VAR, PTR, NO } tag_t;
typedef struct pnode *poly_pointer;
struct pnode {
    tag_t tag;              // three kinds
    union {
        char var;           // VAR: variable name
        poly_pointer dlink; // PTR: coef is a list
        int coef;           // NO : integer coef
    };
    int exp;                // exponent
    poly_pointer link;
};
Three kinds of node

VAR: list header, the variable's name (exp = 0). PTR: the coefficient is a sublist. NO: the coefficient is an integer.

Grouping

P(x,y,z) is first grouped by z; each z-term's coefficient is a polynomial in y, and theirs are polynomials in x.

Instructor “This is just one way to implement polynomials with generalized lists. There are others. But you must also be able to write the routines that convert to and from it.”

Generalized Lists

The polynomial's memory layout

3x²y = (3x²)·y¹VARy0PTR1VARx0NO32y's coefficient is a polynomial in xNO: integer coefficient 3, exponent 2

VAR: list header, the variable's name in var. PTR: the coefficient is the list in dlink. NO: the coefficient is an integer.

P = ((x¹⁰+2x⁸)y³ + 3x⁸y²)z² + ((x⁴+6x³)y⁴ + 2y)zVARz0PTR2PTR1VARy0PTR3x¹⁰+2x⁸VARy0PTR4x⁴+6x³(the remaining y-list nodes and the x-lists are abbreviated)

P(x,y,z) = x¹⁰y³z² + 2x⁸y³z² + 3x⁸y²z² + x⁴y⁴z + 6x³y⁴z + 2yz, grouped by z and laid out as nested lists.

Lower levels are abbreviated to keep the drawing simple.

Generalized Lists

Node structure and memory layout

FaTFbFcTTTFaTABCB's two elements point to the same A (sharing)C's 2nd element is C itself (recursion)
typedef enum { FALSE, TRUE } boolean;
typedef struct list_node *list_pointer;
struct list_node {
    boolean tag;          // FALSE: atom
    union {
        char data;        // atom value
        list_pointer dlink; // sublist head
    };
    list_pointer link;    // next on this level
};
Node

[tag | data/dlink | link]: F = atom, T = sublist.

In this deck

Every routine uses this structure (nodes with a tag field).

The anonymous union is a C11 feature; for C99, name the union and write L->u.data.

Generalized Lists

Why generalized lists?

Compact

Hierarchical data such as a tree fits in the space of a one-line string: a kind of compression.

Natural hierarchy

File-system directories, popup menus, nested XML/JSON, LISP expressions.

Algorithm space

We move the data into a structure we know well; now every list routine is available.

Instructor “You could store the data as a tree, but it doesn't compare with text. ‘I'll store a bitmap, I'll store the tree's memory image.’ Fine, you can. But stored this way it takes less space… A computer engineer has to be careful even when storing data this small.”
Instructor “Moving data into another space… Z-transformation… it is moved into a space that is well known and well understood. We took the data and moved it into an algorithm space we know very well.”

Basic Routines

QUESTION: copy a list with no sharing and no recursion

list_pointer copy(list_pointer L) {
    list_pointer q = NULL;
    if (L) {                    // L not NULL?
        q = get_node();         // get a node
        q->tag = L->tag;
        if (!L->tag) q->data = L->data; // atom
        else q->dlink = copy(L->dlink);
        q->link = copy(L->link);
    }
    return q;
}
Structure

For each node, get a new node and copy the tag; for an atom copy the value, otherwise link in a copy of the sublist; then copy the rest.

Limitation

The routine is correct only for unshared lists; a shared sublist is copied separately everywhere it is referenced.

get_node(): a helper doing malloc(sizeof *p) plus a NULL check (it stops the program when memory runs out).

Basic Routines · Run the code step by step

Code Trace: copy(L), L = (a,(b,c))

LcloneFaTFbFcL???q

get_node(): a new node is allocated for a (its fields are still garbage).

LcloneFaTFbFcLFa?q

Atom: q->tag = FALSE, q->data = 'a'.

LcloneFaTFbFcLFa?q

Call for the next node: copy(L->link).

LcloneFaTFbFcLFa????q

get_node(): a new node is allocated for (b,c) (its fields are still garbage).

LcloneFaTFbFcLFa?T??q

Sublist: first ask for a copy of the sublist (a recursive call).

LcloneFaTFbFcLFa?T?????q

get_node(): a new node is allocated for b (its fields are still garbage).

LcloneFaTFbFcLFa?T??Fb?q

Atom: q->tag = FALSE, q->data = 'b'.

LcloneFaTFbFcLFa?T??Fb?q

Call for the next node: copy(L->link).

LcloneFaTFbFcLFa?T??Fb????q

get_node(): a new node is allocated for c (its fields are still garbage).

LcloneFaTFbFcLFa?T??Fb?Fc?q

Atom: q->tag = FALSE, q->data = 'c'.

LcloneFaTFbFcLFa?T??Fb?Fc?q

Call for the next node: copy(L->link).

LcloneFaTFbFcFa?T??Fb?Fc?

copy(NULL) (link): L is NULL → returns q = NULL without allocating anything.

LcloneFaTFbFcLFa?T??Fb?Fcq

Return: q->link = NULL (the last node).

LcloneFaTFbFcLFa?T??FbFcq

Return: q->link ← &clone(c).

LcloneFaTFbFcLFa?T?FbFcq

Return: q->dlink ← the sublist's copy &clone(b).

LcloneFaTFbFcLFa?T?FbFcq

Call for the next node: copy(L->link).

LcloneFaTFbFcFa?T?FbFc

copy(NULL) (link): L is NULL → returns q = NULL without allocating anything.

LcloneFaTFbFcLFa?TFbFcq

Return: q->link = NULL (the last node).

LcloneFaTFbFcLFaTFbFcq

Return: q->link ← &clone((b,c)).

LcloneFaTFbFcFaTFbFc

The copy is done: the same structure, built from new nodes. Nodes are taken front to back and filled in back to front.

list_pointer copy(list_pointer L) {
    list_pointer q = NULL;
    if (L) {                    // L not NULL?
        q = get_node();         // get a node
        q->tag = L->tag;
        if (!L->tag) q->data = L->data; // atom
        else q->dlink = copy(L->dlink);
        q->link = copy(L->link);
    }
    return q;
}

Basic Routines

Copying: a test list and the limits

TFaFbTTFcFdFeL

For testing: L = ((a,b),((c,d),e))

Shared list

Copying B = (A,A,()) copies A twice; the sharing is lost in the copy.

Recursive list

For C = (a,C) the calls never end; the stack overflows and the program crashes.

Instructor “Taken front to back, filled in back to front… Is anyone still saying ‘Professor, I won't write it recursively’? If someone says ‘I'll use a queue,’ well, I have nothing more to say…”

Basic Routines

QUESTION: are two lists (no sharing, no recursion) equal?

boolean equal(list_pointer T, list_pointer L) {
    boolean x = TRUE;
    if (!T && !L) return TRUE;
    if (T && L && T->tag == L->tag) {
        if (!T->tag)             // atoms?
            x = (T->data == L->data) ? TRUE : FALSE;
        else                    // sublists
            x = equal(T->dlink, L->dlink);
        if (x == TRUE) return equal(T->link, L->link);
    }
    return FALSE;
}
Logic

Equal if both end together. If the tags match: compare values for atoms and recurse for sublists; then compare the rest.

Limitation

On recursive lists (C = (a,C)) the calls go on forever.

Instructor “This isn't masterful code. Better code can be written. And recursive lists can't be compared because of the infinite calls.”

Basic Routines · Run the code step by step

Code Trace: equal(T, L)

TLFaTFbFcFaTFbFdTL

Both exist and their tag fields match: compare the contents.

TLFaTFbFcFaTFbFdTL

Atom: 'a' == 'a' → x = TRUE.

TLFaTFbFcFaTFbFdTL

x is true: compare the rest of the nodes (the link fields).

TLFaTFbFcFaTFbFdTL

Both exist and their tag fields match: compare the contents.

TLFaTFbFcFaTFbFdTL

Sublist: compare the sublists first.

TLFaTFbFcFaTFbFdTL

Both exist and their tag fields match: compare the contents.

TLFaTFbFcFaTFbFdTL

Atom: 'b' == 'b' → x = TRUE.

TLFaTFbFcFaTFbFdTL

x is true: compare the rest of the nodes (the link fields).

TLFaTFbFcFaTFbFdTL

Both exist and their tag fields match: compare the contents.

TLFaTFbFcFaTFbFdTL

Atom: 'c' == 'd' → x = FALSE.

TLFaTFbFcFaTFbFdTL

x is false: return FALSE; the rest of the nodes is never examined.

TLFaTFbFcFaTFbFdTL

The return value is passed straight up: FALSE.

TLFaTFbFcFaTFbFdTL

From the sublists: x = FALSE.

TLFaTFbFcFaTFbFdTL

x is false: return FALSE; the rest of the nodes is never examined.

TLFaTFbFcFaTFbFdTL

The return value is passed straight up: FALSE.

boolean equal(list_pointer T, list_pointer L) {
    boolean x = TRUE;
    if (!T && !L) return TRUE;
    if (T && L && T->tag == L->tag) {
        if (!T->tag)             // atoms?
            x = (T->data == L->data) ? TRUE : FALSE;
        else                    // sublists
            x = equal(T->dlink, L->dlink);
        if (x == TRUE) return equal(T->link, L->link);
    }
    return FALSE;
}

Basic Routines

A better version, and shared lists

bool same(list_pointer T, list_pointer L) {
    if (T == L) return true;        // same node/both NULL
    if (!T || !L || T->tag != L->tag) return false;
    if (T->tag ? !same(T->dlink, L->dlink)
               : T->data != L->data)
        return false;
    return same(T->link, L->link);
}
The T == L shortcut

If both are NULL or point to the same (shared) sublist, return TRUE without descending into it.

QUESTION: shared lists

The same routine also gives the right answer for non-recursive (acyclic) shared lists; the shortcut avoids walking common sublists again.

Instructor “Look, I'm finding the program's stress points. You pressed a key and it ran; that's not enough! … A proof, on the other hand, shows command of a much wider body of knowledge.”

Basic Routines

QUESTION: traverse the list and print its atoms

A common buggy version

void print_atoms( list_pointer L ){
    if(L)
        if(!L->tag)
            printf("%c", L->data);
        else
            print(L->dlink);
        print( L->link );
}

Fixed

void print_atoms(list_pointer L) {
    if (L) {
        if (!L->tag)
            printf("%c", L->data); // print an atom
        else
            print_atoms(L->dlink);
        print_atoms(L->link);
    }
}
Two bugs: (1) The recursive calls use the name print, but the routine is called print_atoms (it won't compile). (2) No braces: if(L) covers only the inner if-else, so line 7 runs even when L == NULL → crash on NULL->link. The fix is to put both calls inside if (L) { … }.

Basic Routines · Run the code step by step

Code Trace: print_atoms(L), L = (a,(b),c)

FaTFbFcL

Atom: 'a' printed.

FaTFbFcL

Move to the next node on the same level (L->link).

FaTFbFcL

Sublist: descend into it first (L->dlink).

FaTFbFcL

Atom: 'b' printed.

FaTFbFcL

Move to the next node on the same level (L->link).

FaTFbFc

L == NULL (end of the level): return without doing anything.

FaTFbFcL

Move to the next node on the same level (L->link).

FaTFbFcL

Atom: 'c' printed.

FaTFbFcL

Move to the next node on the same level (L->link).

FaTFbFc

L == NULL (end of the level): return without doing anything.

FaTFbFc

All atoms are printed left to right regardless of depth: abc. Each node once: O(n).

void print_atoms(list_pointer L) {
    if (L) {
        if (!L->tag)
            printf("%c", L->data); // print an atom
        else
            print_atoms(L->dlink);
        print_atoms(L->link);
    }
}

Basic Routines

Example: A(B(E(K,L),F),C(G),D(H(M),I,J))

FATFBTFEFKFLFFTFCFGTFDTFHFMFIFJ

Each tree node is a sublist: its head is the node's label, the rest are its children. Nodes on the same level can be followed along one row.

For this list, print_atoms prints ABEKLFCGDHMIJ: the tree's preorder traversal.

Depth and Inversion

QUESTION: the depth of a list, recursively

int depth(list_pointer L) {
    list_pointer p = L;  int m = 0; // m: the max
    if (!L) return 0;
    while (p) {
        if (p->tag) {
            int n = depth(p->dlink);
            if (m < n) m = n;
        }
        p = p->link;
    }
    return m + 1;
}
Definition

depth(()) = 0; otherwise 1 + the largest depth among the sublists. Atoms add no depth.

Examples

(a, b) → 1 · (a, (b, c)) → 2 · (a, (b, (c))) → 3 · (()) → 1.

Cost

Each node is visited once: O(n); the call stack grows at most as deep as the list.

Depth and Inversion · Run the code step by step

Code Trace: depth(L), L = (a,(b,(c)))

FaTFbTFcp

Level 1 call: p = L, m = 0.

FaTFbTFcp

'a' is an atom: no added depth.

FaTFbTFcp

Found a sublist: its depth is computed recursively.

FaTFbTFcp

Level 2 call: p = L, m = 0.

FaTFbTFcp

'b' is an atom: no added depth.

FaTFbTFcp

Found a sublist: its depth is computed recursively.

FaTFbTFcp

Level 3 call: p = L, m = 0.

FaTFbTFcp

'c' is an atom: no added depth.

FaTFbTFc

p = p->link → NULL: this level is done.

FaTFbTFc

return m + 1 = 1: the deepest sublist + this level.

FaTFbTFcp

The sublist returned n = 1 → m = 1.

FaTFbTFc

p = p->link → NULL: this level is done.

FaTFbTFc

return m + 1 = 2: the deepest sublist + this level.

FaTFbTFcp

The sublist returned n = 2 → m = 2.

FaTFbTFc

p = p->link → NULL: this level is done.

FaTFbTFc

return m + 1 = 3: the deepest sublist + this level.

int depth(list_pointer L) {
    list_pointer p = L;  int m = 0; // m: the max
    if (!L) return 0;
    while (p) {
        if (p->tag) {
            int n = depth(p->dlink);
            if (m < n) m = n;
        }
        p = p->link;
    }
    return m + 1;
}

Depth and Inversion

QUESTION: depth, iteratively (a student solution)

int depth( tree_pointer L ){
    tree_pointer pop(); void push(tree_pointer);
    int max = 0;
    for( int current = 1, p = L ; p ; p->link ){
        if( p->tag ){ // enter the sublist
            push(p);
            p = p -> dlink;
            current++;
        }
        if( !p->link ){   // end of list, go up
            p = pop();
            if( max < current ) max = current;
            current--;
        }
    }
    return max;
}
Compiling / the loop

int current = 1, p = L makes p an int; the update p->link is not an assignment → an infinite loop.

Edge cases

It dereferences NULL on an empty sublist, pops an empty stack, and never moves on to T's sibling.

Instructor “This isn't going to be written as a loop that tracks current and max. That's what a first-year student writes. … And global variables are forbidden!”

Depth and Inversion

Iterative depth with a local stack

int iter_depth(list_pointer L) {
    list_pointer stack[100];   // local stack (depth<100)
    int top = 0, d = 1, m = 0;
    list_pointer p = L;
    if (!L) return 0;
    for (;;) {
        if (p == NULL) {             // level done
            if (top == 0) return m;
            p = stack[--top]->link;  // the node after T
            d--;
        } else {
            if (d > m) m = d;
            if (p->tag && p->dlink) {
                stack[top++] = p;    // descend
                p = p->dlink;
                d++;
            } else p = p->link;
        }
    }
}
Method

When descending into a sublist, push its T node; when the level ends, pop it and move on to its sibling.

Rules

No globals: the stack and counters are local. The empty sublist and the empty list are handled separately.

Verification

Checked by simulation to match the recursive version on six sample lists.

Depth and Inversion

On thinking recursively

Instructor “If it were up to me, I'd give this course 15 credits instead of 3, just to build the foundations of recursive thinking.”
Instructor “While eating, walking, watching TV, you have to keep asking yourself: ‘If I do this, what do I return?’ … And use the debugger to see what is returned where.”
Code that works “The skill of writing code that works on the given example and crashes on every other one! Bravo, really! Code has to work correctly on different inputs, including the edge cases.”
The original book “Get 2–3 books for every course. A book you paid for gets read; a photocopy doesn't.”

Edge-case lists: (), (a), (()), ((())), a long one-level list, a deep single chain.

Depth and Inversion

QUESTION: invert an unshared list — L = (a,(b,c)) → ((c,b),a)

void invert(list_pointer *L) {
    list_pointer r, p = *L, q = NULL;
    while (p) {
        if (p->tag)                 // not an atom
            invert(&p->dlink);
        r = q;
        q = p;
        p = p->link;
        q->link = r;
    }
    *L = q;
}
List inversion + 2 lines

The three pointers of the singly linked list_invert routine, unchanged; the addition: if the node is a sublist, invert it first. Use: mirroring an image.

Why &p->dlink?

The sublist's head changes; the T node's dlink field is updated by the inner call's *L = q line before the call returns.

Instructor “If we didn't solve it this way, it would be brute force. That inverts it too, but by bulldozing everything in its path.”

Depth and Inversion · Run the code step by step

Code Trace: invert(&L), (a,(b,c)) → ((c,b),a)

q = NULLLFaTFbFcp

p = *L → &node(a), q = NULL.

LFaTFbFcqp

r = q; q = p; p = p->link; q->link = r; → a's link is reversed.

LFaTFbFcqp

The node is a sublist: invert it first (via the address &p->dlink).

q = NULLLFaTFbFcp

p = *L → &node(b), q = NULL.

LFaTFbFcqp

r = q; q = p; p = p->link; q->link = r; → b's link is reversed.

LFaTFbFcrq

r = q; q = p; p = p->link; q->link = r; → c's link is reversed.

LFaTFbFcrq

p == NULL: this level is done.

LFaTFbFcrq

*L = q: c is this level's new head.

LFaTFbFcqp

Back from the sublist: the T node's dlink already points to the new head.

LFaTFbFcrq

r = q; q = p; p = p->link; q->link = r; → (c,b)'s link is reversed.

LFaTFbFcrq

p == NULL: this level is done.

LFaTFbFcrq

*L = q: (c,b) is this level's new head.

void invert(list_pointer *L) {
    list_pointer r, p = *L, q = NULL;
    while (p) {
        if (p->tag)                 // not an atom
            invert(&p->dlink);
        r = q;
        q = p;
        p = p->link;
        q->link = r;
    }
    *L = q;
}

Depth and Inversion

Data structures: the backbone

Job interview “Have you taken data structures?” “Yes, I have.” “So have I. That's lists, trees and so on, right?” … It's like saying: “Brain surgery? Oh, that's the blood vessels, right?” As if it were that simple. Data structures are the backbone of computer science. Knowing what a list or a tree is doesn't mean you've learned data structures. You have to be able to write the routines that build them.
Reading: Theory and Mathematics “Here, students get programming assignments as soon as they start their first year… the upside is writing code fast and finding a job fast. But there's a downside: theory is neglected… Yet you need a very good mathematical background: discrete math, statistics, stochastic processes… Richard Johnsonbaugh's Discrete Mathematics: get it and read it. Skip it and you'll still pass your courses, but you won't learn this craft.”
Reading “A department has limits; it can't give you everything! A student should realize this by the second year and take courses from the mathematics and statistics departments…”

Trees and Sharing

Building a tree structure with a generalized list

ABCDEFGHIJKLM

A(B(E(K,L),F),C(G),D(H(M),I,J))

Notation

C(G): C is G's parent. A(B…,C…,D…): A is the parent of B, C, and D.

Flexibility

For this much data a one-line string is enough. Dots instead of commas, braces instead of parentheses: the notation is up to you.

Instructor “If you can go from this to that and back, you're fine; nobody can see the structure inside.”

Trees and Sharing

Drawback: no left/right distinction

ABCDGEIABCDGEIABCDGEI

All three trees have the same linear form, A(B(C(D(E),G(I)))): there is no way to write whether B is a left or a right child.

The problem

For a general tree the order of children is enough, but in a binary tree the distinction between left and right children is lost.

The fix

Store L/R information with each node (or write a special symbol for an empty child): A(B(,C(…))).

Trees and Sharing

Assignments

  1. Assignment 1: Draw the memory layout and the tree structure of the list A(B(E(H,I(J,K)),F),C(G),D).
  2. Assignment 2: Write a routine that, given a tree structure, outputs its list notation.
Suggested solutions
ABCDEFGHIJK
void print_tree(list_pointer L) { // L: (root, subtrees...)
    printf("%c", L->data);          // root label
    if (L->link) {
        printf("(");
        for (list_pointer p = L->link; p; p = p->link) {
            if (p->tag) print_tree(p->dlink);
            else printf("%c", p->data);   // leaf
            if (p->link) printf(",");
        }
        printf(")");
    }
}

Assignment 2: if the tree is stored as a list whose first element is the root label, print_tree produces the right text for both examples (checked by simulation).

Trees and Sharing

Shared lists and reference counting

// header ref: how many pointers point to this list
void list_free(list_pointer *L) {
    list_pointer p, q;
    if (*L == NULL) return;
    if (--(*L)->ref > 0) {      // still in use
        *L = NULL; return;
    }
    p = (*L)->link;             // first real node
    free(*L);
    while (p) {
        if (p->tag) list_free(&p->dlink);
        q = p; p = p->link; free(q);
    }
    *L = NULL;
}
Header node

Every list starts with a header node; ref is the number of pointers pointing to it.

The release rule

A list is not freed until its count reaches 0; when it does, its nodes and sublists are released by the same rule.

Horowitz–Sahni, Section 4.9 (header nodes with reference counts).

Trees and Sharing

Reference counting step by step: B = (A, A, ())

ref=3A→ a → (b,c)ref=1BTTTthe name A counts as 1 tooB = (A, A, ())

Each list starts with a header node; ref is the number of pointers to it. A is pointed to by the name A and by two of B's elements: 3.

ref=3A→ a → (b,c)ref=0BTTTthe name A counts as 1 tooB freed

list_free(&B): B's count goes 1 → 0, and B's nodes are released one by one.

ref=1A→ a → (b,c)ref=0BTTTthe name A counts as 1 tooA: 3 → 1

B's two T nodes call list_free on A: 3 → 2 → 1. A is still in use, so it is not freed.

ref=0A→ a → (b,c)ref=0BTTTthe name A counts as 1 tooA freed

Then list_free(&A): 1 → 0, and A is freed. Sharing handled safely.

The same idea works for copying: a shared sublist isn't copied again; its count is incremented.

Trees and Sharing

The limits of reference counting, and garbage collection

The cycle problem

C = (a, C): C points to itself, so its count never reaches 0; even if the name C is deleted, the list stays in memory (a leak).

Mark and sweep

(1) Every node reachable from the root pointers (variables) is marked; (2) memory is scanned and unmarked nodes are swept (returned to avail).

The cost

The program pauses while the collector runs, and the mark bit needs space. LISP, Java, and Python use variants of this idea.

In C

No automatic garbage collection: copying, sharing, and freeing are the programmer's responsibility.

Lab · C code step by step

Lab: List Depth

Wrap-Up

Exam question

Question. A field int count has been added to each node. Write a recursive routine that stores, in the count of every node whose tag is TRUE, the number of nodes below it, and returns the total number of nodes.
Sample solution
int count_nodes(list_pointer L) {
    int total = 0;              // an empty list has 0 nodes
    for (; L; L = L->link) {
        if (L->tag) {           // is there a sublist?
            L->count = count_nodes(L->dlink);
            total += L->count;  // count the sublist's nodes too
        }
        total++;                // the current node
    }
    return total;
}

Example: (a,(b,c)) → the T node's count is 2 and the return value is 4 (the T node counts too). Checked by simulation.

Wrap-Up

Exam question

Question. Write a recursive routine that prints a generalized list in memory in its linear form.
Solution
void print_linear(list_pointer L) {
    printf("(");
    for (; L; L = L->link) {
        if (L->tag) print_linear(L->dlink);
        else printf("%c", L->data);
        if (L->link) printf(",");
    }
    printf(")");
}

An empty sublist prints as () too: (a,(()),b) is reproduced exactly (checked by simulation).

Wrap-Up

Exam question

Question. Draw the generalized list corresponding to the linear form L=(d,(c,(e,(m,k),(()),z,y,(x,(r),b,a,())))). Consecutive elements on the same level must be traceable along one row.
Solution
FdTFcTFeTFmFkTTFzFyTFxTFrFbFaT

(()): a one-element list whose sublist is the empty list; the last element (): a T node with dlink = NULL.

Wrap-Up

Complexity summary

RoutineTimeExtra memoryNote
copyO(n)O(depth + length)*no sharing, no recursion
equal / sameO(n)O(depth + length)*stops at the first difference
print_atomsO(n)O(depth + length)*preorder
depth (recursive)O(n)O(depth)loop over a level, recursion into sublists
iter_depth (iterative)O(n)O(depth)local stack
invertO(n)O(depth)in place, no malloc
array_insert / array_deleteO(n)O(1)shifting
ll_insert (position i)O(i)O(1)no shifting

n: total number of nodes. * For routines that also recurse through link, the stack depth also depends on the number of nodes on a level; using a loop for link brings it down to O(depth).

Wrap-Up

Common mistakes

1Treating a shared list as unshared: copying it, or freeing it twice.
2Calling copy/equality on a recursive list (C = (a,C)): infinite recursion.
3Forgetting the braces on ifs and recursing on NULL too (print_atoms).
4Passing the list by value to a routine that changes its head (inversion needs list_pointer *L).
5Confusing the empty sublist () with the empty list NULL: the T node exists, its dlink is NULL.
6Counting depth in a global variable; the routine breaks once it becomes part of another component.

Wrap-Up

Exercises

  1. Write a routine that counts the atoms in a generalized list.
  2. Write a routine that reads the linear form ("(a,(b,c))") and builds the list.
  3. How would you change copy so it preserves sharing?
  4. Write contains(L, x), which tells whether atom x occurs in the list.
  5. Rewrite the array-based list so that it doubles when full.
Answers / hints
  1. 1 for an atom, the recursive result for a sublist; sum along link.
  2. A recursive parser: on '(' build a sublist, advance on ',', return on ')'.
  3. Keep a table of the sublists already copied (old address → new address).
  4. Compare at an atom, search recursively in a sublist; return on the first hit.
  5. realloc(a, 2*cap*sizeof *a); O(1) insertion on average.
Instructor “The routine that builds the list is a quiz question! Keep asking yourself how you'd write the routine that builds the list, and the one that copies it!”

Wrap-Up

Chapter summary

The list ADT

The contract is fixed; the implementation can change: arrays give fast access, links give fast insertion and deletion.

Generalized lists

A node is an atom or a sublist: [tag | data/dlink | link].

The recursive pattern

Process an atom, recurse into a sublist, then continue along link.

Depth and inversion

The recursive versions are short and correct; the iterative ones need a local stack.

Trees

A one-line linear form; for binary trees, left/right information must be added.

Sharing

Copying and freeing have pitfalls; reference counting fails on cycles, so garbage collection is needed.

Wrap-Up

References and next steps

  • E. Horowitz, S. Sahni, S. Anderson-Freed, Fundamentals of Data Structures in C, 2nd ed., Section 4.9 (generalized lists, reference counts).
  • T. H. Cormen et al., Introduction to Algorithms, Chapter 10 (list implementations).
  • R. Johnsonbaugh, Discrete Mathematics (recommended in the reading).

Singly linked list routines and the list-freeing exam question: Linked Lists.

The End

Chapter 9: The List ADT and Generalized Lists
Next chapter: Chapter 10: Trees · Course page
1 / 1 Data Structures and Algorithms with C