linked structures

Chapters five and six are the backbone of the data structure section One builds linear lists with arrays The other builds them with pointers Both represent the same abstract list They are compared with experimental data because the difference only shows up when you measure operations

I used to think linked lists were an interview trick Then I had to splice a chain of transformations where each step depended on the previous node Suddenly the pointer representation was the only clean shape

The node

struct Node {
    int value;
    Node *nxt;
    explicit Node(int v, Node *n = nullptr) : value(v), nxt(n) {}
};

A node holds one value and one raw link The list is the chain of links There is no central array holding positions

In C++ this is where ownership gets loud A raw link says nothing about who frees the node std::unique_ptr<Node> says one owner and cleans up automatically Raw pointers are used because this is teaching representation Production code should not copy that blindly

Traversal is a ritual

Node head(1, new Node(2, new Node(3)));
Node *cur = &head;
while (cur) {
    // use cur->value
    cur = cur->nxt;
}

Every linked structure algorithm is this walk with extra work inside the loop The stop condition is the link becoming nullptr

The failure I keep seeing

A cycle turns that loop into an infinite one It does not throw It just spins until the process is killed

If your traversal never ends, do not blame the loop first Draw the links Ask whether a node points back into the chain

Insertion is the reason this exists

With an array, inserting in the middle shifts elements With a linked representation, if you already hold the previous node, insertion is a couple of link changes

void insert_after(Node *node, int value) {
    node->nxt = new Node(value, node->nxt);
}

Two assignments No shifting One new allocation and a future delete you had better remember

That last sentence is why modern C++ prefers smart pointers Freedom comes with a bill

Doubly linked

Add a previous link and you can walk backwards Deletion becomes easier because you do not need the predecessor You pay one extra link per node

Circular

Make the last link point to the head Useful for round robin schedulers Dangerous if you forget there is no natural end A circular list with a broken exit condition is an infinite loop wearing a friendly smile

Cost table

OperationVectorLinked
access by indexconstantlinear
insert at known positionlinearconstant
append with tail pointerconstantconstant
append without tail pointerconstantlinear
memory per elementvaluevalue plus link

When to use it

  • the data is naturally a chain
  • edits happen at known positions
  • you rarely need indexed access
  • someone maintaining the code understands links

When not to

  • you need random access
  • you iterate far more than you modify
  • cache locality matters
  • the team would rather ship than debug pointer lifetimes

Mistakes

  • losing the head reference before inserting at the front
  • forgetting to update the tail after append
  • mutating links during traversal and skipping nodes
  • mixing new and smart ownership in the same chain

Exercises

  1. Implement insert_after and run it on a three node chain
  2. Add a tail pointer and write append without a walk
  3. Detect a cycle by keeping a set of visited node addresses
  4. Explain why array and linked lists are compared experimentally

What to remember

A linked structure trades random access for cheap edits at known positions That trade is the entire reason it exists Everything else is bookkeeping

Posted