Lecture 3
Common questions
The things people most often get confused about in linked list.
When is a linked list better than an array?Basics
When you insert or delete often and don't need random access. A list grows one node at a time and an insert or delete moves no data, only pointers. An array gives instant a[i], but growing it means allocate, copy and delete. Reaching position i in a list costs a walk from head.
What is the difference between head, cursor and prev?Basics
head always points at the first node. cursor is the current node, the one operations act on, and it is nullptr when there is no current node. prev is the node just before cursor, kept so you can delete or insert before the cursor in a singly linked list.
Why does a node need a pointer, not another node, as its next?Basics
A node containing a node containing a node would have infinite size. A pointer has a fixed size, so node* next; is legal inside node and lets the chain be as long as the heap allows.
struct node {
elType e;
node* next; // OK: fixed-size pointer
// node next; // error: infinitely large
};What does p->e.word mean?Basics
p is a pointer, so -> follows it to the node. Then . picks the word field of the e member, which is a plain struct, not a pointer. It is (*p).e.word.
node* p = new node;
p->e.word = "hat"; // arrow for the pointer, dot for the struct
p->e.count = 2;Do I have to write NULL like the slides, or can I use nullptr?Basics
Use nullptr. In this course's code NULL and nullptr both mean "points at nothing", but nullptr is the modern, type-safe spelling and is what Lectures 1 and 2 use.
Why does the order of the pointer assignments matter?Insertion
The old link is the only thing pointing at the rest of the list. If you overwrite it first, the remainder is lost (leaked). Always point the new node at the rest first, then redirect the old pointer to the new node.
// insert p after cursor
p->next = cursor->next; // 1. steal the successor
cursor->next = p; // 2. now it is safe to redirectDoes insert at the head work on an empty list?Insertion
Yes. head is nullptr, so p->next = head; sets the new node's next to nullptr, then head = p; makes it the only node. No special case is needed (but a tail pointer must also be set in that case).
p->next = head; // nullptr on an empty list
head = p;Why keep a tail pointer?Insertion
Without it, appending means walking from head to the last node, which is O(n). With tail, append is O(1): tail->next = p; tail = p;. The price is that every operation that can remove or add the last node (including deleting the only node) must update tail too.
What happens to current after each insert operation?Insertion
In the slides: insertFirst, insertBefore and insertEnd make the new node current, while insertAfter leaves the current position unchanged. The practice sheet's question 14 states its own rule (insertion moves current to the new node), so follow whatever rules the question gives.
Why do I need the previous node to delete a node?Deletion
In a singly linked list the only pointer that refers to a node lives in its predecessor. To remove node X you must change that predecessor's next to X's successor. A node cannot find its predecessor on its own, so you track prev while searching.
prev->next = node->next; // bypass node
delete node;Why do I move head before I delete the first node?Deletion
After delete, the node is gone, so reading head->next is use-after-free. Save the pointer, move head to the second node, then delete the saved one.
node* old = head;
head = head->next;
delete old;What are all the cases removeFirst has to handle?Deletion
Not found (return false), removing the head (prev is nullptr, so set head = node->next), removing a middle node, removing the tail (set tail = prev), and removing the only node, which is both head and tail so both end up nullptr.
if (prev) prev->next = node->next;
else head = node->next;
if (node == tail) tail = prev;What does deleteNode() do to the current position?Deletion
It deletes the current node and the next node becomes current. If the deleted node was the last, the current position becomes empty. deleteEnd() always leaves the current position empty.
Why does orderedInsert use <= instead of < when walking the list?Ordered list
<= keeps walking past nodes with an equal key, so the new node lands after existing duplicates. That keeps equal keys in the order they were inserted (a stable insert). With < the new node would stop in front of them.
while (next && next->key <= key) {
prev = next;
next = next->next;
}When does orderedInsert have to update head and tail?Ordered list
head changes when prev is nullptr, meaning the new key is smallest (or the list was empty). tail changes when next is nullptr, meaning the new node ended up last. An insert into an empty list changes both.
How do I traverse a circular list without looping forever?Variants
There is no nullptr to stop at, because tail->next == head. Visit a node, move on, and stop when you get back to head. A do-while handles the one-node list correctly.
node* p = head;
do {
std::cout << p->key << " ";
p = p->next;
} while (p != head);How many links change when I insert into a doubly linked list?Variants
Four: the new node's prev and next, the left neighbour's next, and the right neighbour's prev. Set the new node's two links first, since they read the neighbours, then update the neighbours.
X->prev = D; X->next = B;
D->next = X; B->prev = X;Why is the node class private inside List?Class design
Users of the list should only use operations like insertFirst and search, never raw nodes. Hiding the node lets the implementation change (for example, to an array of indices) without breaking callers. It is the ADT idea from Lecture 2.
What does the destructor have to do?Class design
Free every node. ~List() calls makeListEmpty(), which walks the list deleting each node. If it only deleted head, or nothing at all, all the other nodes would leak when the list goes out of scope.
Why does listSize() use its own pointer q instead of cursor?Class design
Using cursor would move the user's current position as a side effect of counting. A local pointer lets listSize() be const and leave head, cursor and prev untouched.
NodePointer q = head;
int count = 0;
while (q != NULL) { count++; q = q->next; }My list prints fine but later crashes. What should I check first?Debugging
Check for a pointer used after delete, a next that was never set (garbage from new node), and dereferencing nullptr (test cursor != nullptr before cursor->next). Also confirm head and tail were updated when you inserted or removed at the ends.
No questions match.
Ready to check yourself? Take the quiz →